473,466 Members | 1,503 Online
Bytes | Software Development & Data Engineering Community
Create Post

Home Posts Topics Members FAQ

How can I change this ggplot code so that it works with my imported excel data?

3 New Member
I am using this code in Rstudio and I am trying to implement my dataset AAPL which I have already imported into Rstudio from excel:

> library(ggplot2)
> library(quantmod)
> draw_candles <- function(df, title_param, alpha_param = 1){
+ df$change <- ifelse(df$Close > df$Open, "up", ifelse(df$Close < df$Open, "down", "flat"))
+
+ # originally the width of the bars was calculated by FXQuantTrader with use of
'periodicity()', which
+ # seems to work ok only with: ‘minute’,‘hourly’, ‘daily’,‘weekly’, ‘monthly’,
+ # ‘quarterly’, and ‘yearly’, but can not do 1 sec bars while we want arbitrary bar size support!-)
+ # df$width <- as.numeric(periodicity(df)[1])
+ # So let us instead find delta (seconds) between 1st and 2nd row and just
+ # use it for all other rows. We check 1st 3 rows to avoid larger "weekend gaps"
+ width_candidates <- c(as.numeric(difftime(df$Date[2], df$Date[1]), units = "secs"),
+ as.numeric(difftime(df$Date[3], df$Date[2]), units = "secs"),
+ as.numeric(difftime(df$Date[4], df$Date[3]), units = "secs"))
+
+ df$width_s = min(width_candidates) # one (same) candle width (in seconds) for all the bars
+
+ # define the vector of candle colours either by name or by rgb()
+ #candle_colors = c("down" = "red", "up" = "green", "flat" = "blue")
+ candle_colors = c("down" = rgb(192,0,0,alpha=255,maxColorValue=255), "up" = rgb(0,192,0,alpha=255,maxColorValue=255), "flat" = rgb(0,0,192,alpha=255,maxColorValue=255))
+
+ # Candle chart:
+ g <- ggplot(AAPL, aes(x=Date))+
+ geom_linerange(aes(ymin=Low, ymax=High, colour = change), alpha = alpha_param) + # candle whiskerss (vertical thin lines:)
+ theme_bw() +
+ labs(title=title_param) +
+ geom_rect(aes(xmin = Date - width_s/2 * 0.9, xmax = Date + width_s/2 * 0.9, ymin = pmin(Open, Close), ymax = pmax(Open, Close), fill = change), alpha = alpha_param) +
# cabdke body
+ guides(fill = FALSE, colour = FALSE) +
+ scale_color_manual(values = candle_colors) + # color for line
+ scale_fill_manual(values = candle_colors) # color for candle fill
+
+ # Handle special cases: flat bar and Open == close:
+ if (any(AAPL$change == "flat")) g <- g + geom_segment(data = df[df$change == "flat",], aes(x = Date - width_s / 2 * 0.9, y = Close, yend = Close, xend = Date + width_s / 2 * 0.9, colour = change), alpha = alpha_param)
+
+ #print(g)
+ g
+ }
> print(draw_candles(g,AAPL))

My problem is that every time I try to print out the graph, I get this error:

Error in ifelse(df$Close > df$Open, "up", ifelse(df$Close < df$Open, "down", : object 'g' not found

And I have no idea how to fix it// what I am doing wrong. Can anyone help?
Oct 21 '20 #1
0 3572

Sign in to post your reply or Sign up for a free account.

Similar topics

3
by: Mads Petersen | last post by:
I'm stuck in this code. Hope you can and will help me. I launch it from excel. I have made the following code work, but not as i whant. I need the ranges to be working with something like xlDown....
3
by: John Marble | last post by:
I have around 400 excel files filled with data that I need to import in ACCESS. The tricky part is that they must be imported one at time, and properly corrected before importing the next one. I...
1
by: Mike P | last post by:
I'm trying to take some data from an Excel spreadsheet, put it into a dataset, and then update a SQL table with the data. I'm not 100% sure I'm doing this right, can somebody tell me what I'm...
1
by: Ramakrishnan Nagarajan | last post by:
Hi, I am converting Excel data into a Dataset in C#. There are around 24 columns in the Excel Sheet. First I tried to insert one row with correct values in the Excel sheet. i.e. for text columns...
6
by: Paul Wagstaff | last post by:
Happy new year...hope someone can help with this.. The nub of this problem is to populate 2 tables in a 1-M relationship from an imported Excel spreadsheet. The problem being that there is...
2
by: ramaraog80 | last post by:
Hi, I want VB Script which converts Excel data into CSV file. Please Reply me ASAP.
0
by: Grip | last post by:
Hi, I have gone throught the group and Microsoft's online help and have seen many suggestions but I am still seeking clarity: 1. I have an excel spreadsheet. Column A contains text that may...
2
by: JHNielson | last post by:
I posted this in the Access forum, but I think the answer may be a VBA script, so I thought I would also post this here... I have an Access database I am importing records into from an Excel...
0
by: prashantdixit | last post by:
Hi, I have beent trying importing Excel data with one column containing PDF/JPEG file name to access 2007. I have a Excel file with few columns One of the columns named as "Reference". The...
1
by: Sonnysonu | last post by:
This is the data of csv file 1 2 3 1 2 3 1 2 3 1 2 3 2 3 2 3 3 the lengths should be different i have to store the data by column-wise with in the specific length. suppose the i have to...
0
by: Hystou | last post by:
There are some requirements for setting up RAID: 1. The motherboard and BIOS support RAID configuration. 2. The motherboard has 2 or more available SATA protocol SSD/HDD slots (including MSATA, M.2...
0
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However,...
0
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven...
1
by: Hystou | last post by:
Overview: Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows...
0
agi2029
by: agi2029 | last post by:
Let's talk about the concept of autonomous AI software engineers and no-code agents. These AIs are designed to manage the entire lifecycle of a software development project—planning, coding, testing,...
0
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 1 May 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM). In this session, we are pleased to welcome a new...
0
by: conductexam | last post by:
I have .net C# application in which I am extracting data from word file and save it in database particularly. To store word all data as it is I am converting the whole word file firstly in HTML and...
0
by: TSSRALBI | last post by:
Hello I'm a network technician in training and I need your help. I am currently learning how to create and manage the different types of VPNs and I have a question about LAN-to-LAN VPNs. The...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.