Hi Hadley,
I am sorry, I thought this is a well-known feature/bug of ggplot2 I
was showing, this is why I provided no real example. Here it goes:
N <- 100
df <- data.frame(time=1:N, LC=rnorm(N, 70, 20), HC=rnorm(N, 20, 20),
corr=seq(-1, 0.98, 0.02))
ggplot(df, aes(x=time)) + geom_ribbon(aes(ymin=HC, ymax=LC,
fill=corr))
The above doesn't work ("Aesthetics can not vary with a ribbon")
This simulates it but is not ideal:
ggplot(df, aes(x=time)) + geom_linerange(aes(x=time, ymin=HC, ymax=LC,
color=corr), size=2)
Finally, I found a solution how to do it (thanks to James McCreight
for turning my attention to geom_polygon()!), but it needs some more
playing around:
mdf <- melt (df, id.vars=c("time", "corr"))
mdf <- mdf[order (mdf$time, mdf$variable),]
mdf <- rbind (mdf[1:(nrow (mdf) - 2),], mdf[3:nrow (mdf),])
mdf$grouping <- rep (rep (1:(N-1), each=2), 2)
mdf <- mdf[order (mdf$grouping, mdf$time),]
mdf$reorder <- rep (c (1, 2, 4, 3), (N-1))
mdf <- mdf[order (mdf$grouping, mdf$reorder),]
ggplot (mdf, aes (x = time, y=value)) + geom_polygon (aes (fill=corr,
group=grouping))
I'm new to R, so tt could be probably written a little better. Anyway,
the final effect is exactly what I wanted to get. But of course it
would be much more natural to do it with geom_ribbon().
BTW - thanks for a great library - it really rocks!
Jakub