[R] ggplot, ribbon not showing up properly

ONKELINX, Thierry Thierry.ONKELINX at inbo.be
Tue Sep 15 11:17:43 CEST 2009

Dear Sock,

I'm wondering if that mean_sdl function is return what you are
expecting. I would calculate the statistics outside ggplot.

RibbonData <- ddply(dat.less, "Day", function(x){
	mean(x$Y) + c(ymin = -1, ymax = 1) * sd(x$Y)
p + stat_summary(data=dat.less, aes(group=1), geom="crossbar",
fun.data="mean_sdl", mult=1) + geom_ribbon(data = RibbonData, aes(group
= 1, ymin = ymin, ymax = ymax), fill=alpha("blue", 1/10))



ir. Thierry Onkelinx
Instituut voor natuur- en bosonderzoek / Research Institute for Nature
and Forest
Cel biometrie, methodologie en kwaliteitszorg / Section biometrics,
methodology and quality assurance
Gaverstraat 4
9500 Geraardsbergen
tel. + 32 54/436 185
Thierry.Onkelinx at inbo.be

To call in the statistician after the experiment is done may be no more
than asking him to perform a post-mortem examination: he may be able to
say what the experiment died of.
~ Sir Ronald Aylmer Fisher

The plural of anecdote is not data.
~ Roger Brinner

The combination of some data and an aching desire for an answer does not
ensure that a reasonable answer can be extracted from a given body of
~ John Tukey

-----Oorspronkelijk bericht-----
Van: r-help-bounces at r-project.org [mailto:r-help-bounces at r-project.org]
Namens Sock Cheng
Verzonden: maandag 14 september 2009 20:58
Aan: r-help at r-project.org
Onderwerp: [R] ggplot, ribbon not showing up properly


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.  


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


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))

R-help at r-project.org mailing list
PLEASE do read the posting guide
and provide commented, minimal, self-contained, reproducible code.

Druk dit bericht a.u.b. niet onnodig af.
Please do not print this message unnecessarily.

Dit bericht en eventuele bijlagen geven enkel de visie van de schrijver weer 
en binden het INBO onder geen enkel beding, zolang dit bericht niet bevestigd is
door een geldig ondertekend document. The views expressed in  this message 
and any annex are purely those of the writer and may not be regarded as stating 
an official position of INBO, as long as the message is not confirmed by a duly 
signed document.

More information about the R-help mailing list