5

I have an R dataframe called wSubset. I give the dput of this at the bottom of the question. I can generate a candlestick plot with the below line of code without problems:

if(!require("ggplot2")) { install.packages("ggplot2");  require("ggplot2") }
g <- ggplot(data=wSubset, aes(x=1:nrow(wSubset), lower=wSubset$candleLower, middle=wSubset$candleMiddle, 
                         upper=wSubset$candleUpper, ymin=wSubset$low, ymax=wSubset$high)) +  
      geom_boxplot(stat='identity', aes(group=datetime, fill=fill))

enter image description here

Now, within wSubset$cluster I also store class information ranging from 1 to 5. I want to super-impose a geom_ribbon on top of the above g to colour-label different regions.

# the dataframe to be used by the geom_ribbon for colour-labelling
df_bg2 <- data.frame(x = c(0, rep(which(as.logical(diff(wSubset$cluster))), each=2), length(wSubset$cluster)), 
                     ymin = min(scale(wSubset$low), na.rm = TRUE), 
                     ymax = 1.1*max(scale(wSubset$high), na.rm = TRUE), 
                     fill = factor(rep(wSubset$cluster[c(which(as.logical(diff(wSubset$cluster))), 
                                                             length(wSubset$cluster) )], each=2)),
                     grp = factor(rep(seq(sum(as.logical(diff(wSubset$cluster)), na.rm=TRUE)+1), each=2))
                     )

# new plot which has candlesticks and the geom_ribbon colour-labelling
g2 <- ggplot(data=wSubset, aes(x=1:nrow(wSubset), lower=wSubset$candleLower, middle=wSubset$candleMiddle, 
                         upper=wSubset$candleUpper, ymin=wSubset$low, ymax=wSubset$high)) +  
      geom_boxplot(stat='identity', aes(group=datetime, fill=fill)) + 
      geom_ribbon(data = df_bg2, aes(x = x, ymin=ymin, ymax=ymax, fill=fill, group=grp), alpha=.2) +
      xlab("Date-Time") +
      ylab("Levels") +
      labs(title = "States in Temporal Display")

Here df_bg2 is used for filling the geom ribbons and works ok when I use geom_line, however I get the below error when I try to mix geom_boxplot with geom_ribbon.

Error: Aesthetics must either be length one, or the same length as the dataProblems:wSubset$candleLower, wSubset$candleMiddle, wSubset$candleUpper

Any idea where I am going wrong? This question is linked to an earlier question of mine on how to use geom_ribbon.

> dput(wSubset)
structure(list(date = structure(c(16342, 16342, 16342, 16342, 
16342, 16342, 16343, 16343, 16343, 16343, 16343, 16343, 16344, 
16344, 16344, 16344, 16344, 16344, 16345, 16345, 16345, 16345, 
16345, 16345, 16346, 16346, 16346, 16346, 16346, 16346), class = "Date"), 
    datetime = structure(c(1411945200, 1411959600, 1411974000, 
    1411988400, 1412002800, 1412017200, 1412031600, 1412046000, 
    1412060400, 1412074800, 1412089200, 1412103600, 1412118000, 
    1412132400, 1412146800, 1412161200, 1412175600, 1412190000, 
    1412204400, 1412218800, 1412233200, 1412247600, 1412262000, 
    1412276400, 1412290800, 1412305200, 1412319600, 1412334000, 
    1412348400, 1412362800), class = c("POSIXct", "POSIXt"), tzone = ""), 
    open = c(1.62383, 1.62398, 1.62182, 1.62289, 1.62408, 1.62449, 
    1.6242, 1.62363, 1.62573, 1.62001, 1.62161, 1.62084, 1.62149, 
    1.61949, 1.61978, 1.62036, 1.62253, 1.61767, 1.61825, 1.61978, 
    1.62157, 1.61697, 1.61439, 1.6143, 1.61496, 1.61294, 1.61403, 
    1.60669, 1.59768, 1.59739), high = c(1.62517, 1.62426, 1.62618, 
    1.62559, 1.62745, 1.62522, 1.6245, 1.62678, 1.62873, 1.62253, 
    1.62297, 1.62175, 1.62194, 1.62018, 1.6214, 1.62261, 1.62521, 
    1.61873, 1.61999, 1.625, 1.62248, 1.61756, 1.615, 1.61624, 
    1.61591, 1.61405, 1.61403, 1.60844, 1.598, 1.5978), low = c(1.62258, 
    1.62166, 1.62143, 1.62162, 1.6233, 1.62375, 1.62247, 1.62361, 
    1.61936, 1.61664, 1.62026, 1.61998, 1.61886, 1.61792, 1.61618, 
    1.61798, 1.61739, 1.61724, 1.61817, 1.61962, 1.61561, 1.61283, 
    1.61126, 1.61358, 1.6128, 1.61248, 1.60665, 1.59769, 1.59518, 
    1.59594), close = c(1.62401, 1.62184, 1.6229, 1.62412, 1.62448, 
    1.62422, 1.62365, 1.62575, 1.62002, 1.6216, 1.62085, 1.62149, 
    1.61948, 1.61979, 1.62036, 1.62256, 1.61769, 1.61826, 1.61978, 
    1.62153, 1.61698, 1.61435, 1.61429, 1.61496, 1.61294, 1.61405, 
    1.60669, 1.59769, 1.5974, 1.59672), candleLower = c(1.62383, 
    1.62184, 1.62182, 1.62289, 1.62408, 1.62422, 1.62365, 1.62363, 
    1.62002, 1.62001, 1.62085, 1.62084, 1.61948, 1.61949, 1.61978, 
    1.62036, 1.61769, 1.61767, 1.61825, 1.61978, 1.61698, 1.61435, 
    1.61429, 1.6143, 1.61294, 1.61294, 1.60669, 1.59769, 1.5974, 
    1.59672), candleMiddle = c(NA, NA, NA, NA, NA, NA, NA, NA, 
    NA, NA, NA, NA, NA, NA, NA, NA, NA, NA, NA, NA, NA, NA, NA, 
    NA, NA, NA, NA, NA, NA, NA), candleUpper = c(1.62401, 1.62398, 
    1.6229, 1.62412, 1.62448, 1.62449, 1.6242, 1.62575, 1.62573, 
    1.6216, 1.62161, 1.62149, 1.62149, 1.61979, 1.62036, 1.62256, 
    1.62253, 1.61826, 1.61978, 1.62153, 1.62157, 1.61697, 1.61439, 
    1.61496, 1.61496, 1.61405, 1.61403, 1.60669, 1.59768, 1.59739
    ), fill = c("white", "red", "white", "white", "white", "red", 
    "red", "white", "red", "white", "red", "white", "red", "white", 
    "white", "white", "red", "white", "white", "white", "red", 
    "red", "red", "white", "red", "white", "red", "red", "red", 
    "red"), cluster = c(5, 4, 2, 2, 2, 5, 5, 2, 4, 2, 5, 5, 4, 
    5, 4, 2, 4, 1, 1, 2, 4, 4, 4, 5, 4, 5, 3, 3, 5, 5)), .Names = c("date", 
"datetime", "open", "high", "low", "close", "candleLower", "candleMiddle", 
"candleUpper", "fill", "cluster"), row.names = c(NA, 30L), class = "data.frame")
Community
  • 1
  • 1
Zhubarb
  • 11,432
  • 18
  • 75
  • 114

1 Answers1

1

You want to do something like this:

wSubset <- within(wSubset, x<-1:nrow(wSubset))
g2 <- ggplot() +
    geom_boxplot(stat='identity', data=wSubset, aes(x=x, lower=candleLower, middle=candleMiddle, upper=candleUpper, 
        group=datetime, fill=fill, ymin=low, ymax=high)) + 
      geom_ribbon(data = df_bg2, aes(x = x, ymin=ymin, ymax=ymax, fill=fill, group=grp), alpha=.2) +
      xlab("Date-Time") +
      ylab("Levels") +
      labs(title = "States in Temporal Display")

Namely, separate out the global aesthetics to the geom_boxplot layer. Although, after looking at the plot, it would appear you need to adjust your values in df_bg2.

Matthew Plourde
  • 43,932
  • 7
  • 96
  • 113
  • argh! such a newbie error.. Thanks a lot. If I can push my luck tiny bit more while I have your attention. Can I use `wSubset$datetime` as the x axis in this setting? I believe not because `df_bg2` does not have that column. – Zhubarb Mar 18 '15 at 17:29
  • You're not really supposed to use `aes` like that. I would just add that column to `wSubset` like I do here. – Matthew Plourde Mar 18 '15 at 17:30
  • I misread your question. Yeah, use the datetime column, but you'll have to use that in `df_bg2` as well. – Matthew Plourde Mar 18 '15 at 17:32