472,110 Members | 2,167 Online
Bytes | Software Development & Data Engineering Community
Post +

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 472,110 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 2609

Post your reply

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

Similar topics

1 post views Thread by Mike P | last post: by
reply views Thread by leo001 | last post: by

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.