title: How can I change this ggplot code so that it works with my imported excel data?
body: <p>I am using this code and I am trying to implement my dataset AAPL which I have already imported into R from excel:</p>
  <pre><code>library(ggplot2)
  library(quantmod)
  draw_candles &lt;- function(df, title_param, alpha_param = 1){
       df$change &lt;- ifelse(df$Close &gt; df$Open, &quot;up&quot;, ifelse(df$Close &lt; df$Open, &quot;down&quot;, &quot;flat&quot;))
       
       # 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 &lt;- 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 &quot;weekend gaps&quot;
       width_candidates &lt;- c(as.numeric(difftime(df$Date[2], df$Date[1]), units = &quot;secs&quot;),
                             as.numeric(difftime(df$Date[3], df$Date[2]), units = &quot;secs&quot;),
                             as.numeric(difftime(df$Date[4], df$Date[3]), units = &quot;secs&quot;))
  
       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(&quot;down&quot; = &quot;red&quot;, &quot;up&quot; = &quot;green&quot;, &quot;flat&quot; = &quot;blue&quot;)
       candle_colors = c(&quot;down&quot; = rgb(192,0,0,alpha=255,maxColorValue=255), &quot;up&quot; = rgb(0,192,0,alpha=255,maxColorValue=255), &quot;flat&quot; = rgb(0,0,192,alpha=255,maxColorValue=255))
       
       # Candle chart:
       g &lt;- ggplot(df, 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(df$change == &quot;flat&quot;)) g &lt;- g + geom_segment(data = df[df$change == &quot;flat&quot;,], aes(x = Date - width_s / 2 * 0.9, y = Close, yend = Close, xend = Date + width_s / 2 * 0.9, colour = change), alpha = alpha_param)
       
       g
  }
   &gt; print(draw_candles(g,AAPL))
  </code></pre>
  <p>My problem is that every time I try to print out the graph, I get this error:</p>
  <blockquote>
  <p>Error in ifelse(df$Close &gt; df$Open, &quot;up&quot;, ifelse(df$Close &lt; df$Open,
  &quot;down&quot;,  : object 'g' not found</p>
  </blockquote>
  <p>And I have no idea how to fix it or what I am doing wrong.</p>
  <pre><code>dput(head(AAPL, 20))
  structure(list(Date = structure(c(345427200, 345686400, 345772800, 
  345859200, 345945600, 346032000, 346291200, 346377600, 346464000, 
  346636800, 346896000, 346982400, 347068800, 347241600, 347500800, 
  347587200, 347673600, 347760000, 347846400, 348105600), tzone = &quot;UTC&quot;, class 
  = c(&quot;POSIXct&quot;, 
  &quot;POSIXt&quot;)), Open = c(&quot;0.12834799999999999&quot;, &quot;0.12221&quot;, &quot;0.11328100000000001&quot;, 
  &quot;0.115513&quot;, &quot;0.118862&quot;, &quot;0.12611600000000001&quot;, &quot;0.13225400000000001&quot;, 
  &quot;0.13783500000000001&quot;, &quot;0.145089&quot;, &quot;0.15848200000000001&quot;, &quot;0.160714&quot;, 
  &quot;0.15736600000000001&quot;, &quot;0.15290200000000001&quot;, &quot;0.15401799999999999&quot;, 
  &quot;0.151228&quot;, &quot;0.14453099999999999&quot;, &quot;0.13839299999999999&quot;, &quot;0.135603&quot;, 
  &quot;0.14229900000000001&quot;, &quot;0.14229900000000001&quot;), High = 
  c(&quot;0.12890599999999999&quot;, 
  &quot;0.12221&quot;, &quot;0.11328100000000001&quot;, &quot;0.11607099999999999&quot;, &quot;0.11942&quot;, 
  &quot;0.12667400000000001&quot;, &quot;0.13281299999999999&quot;, &quot;0.13839299999999999&quot;, 
  &quot;0.145647&quot;, &quot;0.15903999999999999&quot;, &quot;0.161272&quot;, &quot;0.15736600000000001&quot;, 
  &quot;0.15290200000000001&quot;, &quot;0.15513399999999999&quot;, &quot;0.151228&quot;, 
  &quot;0.14453099999999999&quot;, 
  &quot;0.13839299999999999&quot;, &quot;0.135603&quot;, &quot;0.14285700000000001&quot;, 
  &quot;0.14229900000000001&quot;
  ), Low = c(&quot;0.12834799999999999&quot;, &quot;0.121652&quot;, &quot;0.112723&quot;, &quot;0.115513&quot;, 
  &quot;0.118862&quot;, &quot;0.12611600000000001&quot;, &quot;0.13225400000000001&quot;, 
  &quot;0.13783500000000001&quot;, 
  &quot;0.145089&quot;, &quot;0.15848200000000001&quot;, &quot;0.160714&quot;, &quot;0.156808&quot;, 
  &quot;0.15234400000000001&quot;, 
  &quot;0.15401799999999999&quot;, &quot;0.15067&quot;, &quot;0.14397299999999999&quot;, 
  &quot;0.13783500000000001&quot;, 
  &quot;0.135045&quot;, &quot;0.14229900000000001&quot;, &quot;0.141183&quot;), Close = 
  c(&quot;0.12834799999999999&quot;, 
  &quot;0.121652&quot;, &quot;0.112723&quot;, &quot;0.115513&quot;, &quot;0.118862&quot;, &quot;0.12611600000000001&quot;, 
  &quot;0.13225400000000001&quot;, &quot;0.13783500000000001&quot;, &quot;0.145089&quot;, 
  &quot;0.15848200000000001&quot;, 
  &quot;0.160714&quot;, &quot;0.156808&quot;, &quot;0.15234400000000001&quot;, &quot;0.15401799999999999&quot;, 
  &quot;0.15067&quot;, &quot;0.14397299999999999&quot;, &quot;0.13783500000000001&quot;, &quot;0.135045&quot;, 
  &quot;0.14229900000000001&quot;, &quot;0.141183&quot;), `Adj Close` = c(&quot;0.101261&quot;, 
  &quot;0.095977999999999994&quot;, &quot;0.088933999999999999&quot;, &quot;0.091134999999999994&quot;, 
  &quot;0.093776999999999999&quot;, &quot;0.099500000000000005&quot;, &quot;0.10434300000000001&quot;, 
  &quot;0.108746&quot;, &quot;0.114469&quot;, &quot;0.12503500000000001&quot;, &quot;0.12679599999999999&quot;, 
  &quot;0.12371500000000001&quot;, &quot;0.12019299999999999&quot;, &quot;0.121513&quot;, 
  &quot;0.11887200000000001&quot;, 
  &quot;0.11358799999999999&quot;, &quot;0.108746&quot;, &quot;0.106544&quot;, &quot;0.11226800000000001&quot;, 
  &quot;0.111387&quot;), Volume = c(&quot;469033600&quot;, &quot;175884800&quot;, &quot;105728000&quot;, 
  &quot;86441600&quot;, &quot;73449600&quot;, &quot;48630400&quot;, &quot;37363200&quot;, &quot;46950400&quot;, &quot;48003200&quot;, 
  &quot;55574400&quot;, &quot;93161600&quot;, &quot;68880000&quot;, &quot;35750400&quot;, &quot;21660800&quot;, &quot;35728000&quot;, 
  &quot;45158400&quot;, &quot;55686400&quot;, &quot;39827200&quot;, &quot;21504000&quot;, &quot;23699200&quot;)), row.names = 
  c(NA, 
  -20L), class = c(&quot;tbl_df&quot;, &quot;tbl&quot;, &quot;data.frame&quot;))
  </code></pre>