473,405 Members | 2,444 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,405 software developers and data experts.

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

3 2Bits
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 3547

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

Similar topics

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....
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...
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...
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...
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...
by: ramaraog80 | last post by:
Hi, I want VB Script which converts Excel data into CSV file. Please Reply me ASAP.
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...
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...
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...
by: Charles Arthur | last post by:
How do i turn on java script on a villaon, callus and itel keypad mobile phone
by: emmanuelkatto | last post by:
Hi All, I am Emmanuel katto from Uganda. I want to ask what challenges you've faced while migrating a website to cloud. Please let me know. Thanks! Emmanuel
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...
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,...
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can...
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers,...
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...
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,...
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...

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.