[R] ggplot, ribbon not showing up properly

Sock Cheng sockcheng at hailmail.net
Mon Sep 14 20:57:42 CEST 2009


Hi,

I'm trying to plot a longitudinal data set, using ggplot and adding some
summary info (eg. mean, 1 sd bounds) using geom=ribbon.  The summary
info is based on a subset of the original data (eg. less an outlier). 
But I'm having trouble getting the ribbons to show up correctly.  It's
probably something obvious that I'm missing as a novice at ggplot2, and
any help is much appreciated!
 
Here's a simple example.  I tried several things.  
- if I use geom=crossbar, everything is ok
- if Day is set as rep(c(1,2,3,8,9), each=8), then everything is ok,
which makes me wonder if the problem has to do with the ordering of Day?
 Day is supposed to be numeric.  

Thanks!
Sock

### Example data.  Ran using R version 2.9.2, ggplot2 version 0.8.3 ###

set.seed(13)

Day <- rep(c(1, 2, 3, 8, 20), each=8)      
# The plot is ok if Day <- rep(c(1,2,3,8,9), each=8) 

ID <- rep(LETTERS[1:8], 5)
Y <- rnorm(length(Day), 100, 5)
dat <- data.frame(Day=Day, ID=ID, Y=Y)

 # outlier
dat$Y[dat$ID=="A" & dat$Day==8] <- 150         
dat.less <- dat[!(dat$ID=="A" & dat$Day==8),]   

# Longitudinal data plot.  Obs for each subject is connected by a line
over time

p <- ggplot(dat, aes(x=Day, y=Y, group=ID)) +
scale_x_continuous(breaks=sort(unique(dat$Day))) +
geom_line(colour=alpha("blue", 5/10))

# Adding mean, 1 sd bounds using crossbar geom is ok.  But the same info
using ribbon geom doesn't work. 
 
p + stat_summary(data=dat.less, aes(group=1), geom="crossbar",
fun.data="mean_sdl", mult=1) + stat_summary(data=dat.less, aes(group=1),
geom="ribbon", fun.data="mean_sdl", mult=1, fill=alpha("blue", 1/10))




More information about the R-help mailing list