standard error with updated ggplot2

1,875 views
Skip to first unread message

Sol Lago

unread,
Dec 23, 2015, 6:19:54 PM12/23/15
to ggplot2
Hi there,

I am running into a problem since I updated ggplot2 to version 2.0.0. 

Before, I normally calculated standard error bars using stat_summary with mean_cl_normal with mult=1 (see here for rationale). However, since I updated ggplot2, this yields and error: 


ggplot(df, aes(x=condition, y=rt, fill=condition)) +
  stat_summary(fun.y = mean, geom = "bar", colour="black",size=.3) + 
  stat_summary(fun.data = mean_cl_normal, mult = 1, geom = "errorbar")

Error in print(ggplot(df, aes(x = condition, y = amplitude, colour = condition)) +  : 
  error in evaluating the argument 'x' in selecting a method for function 'print': Error: Unknown parameters: mult

Any ideas of what the problem is? And if setting mult is no longer an option in ggplot2, do you know of any quick way of calculating error bars that does not involve pre-averaging my data frame before creating a plot? 

Thanks a bunch!


hoai...@hcmuaf.edu.vn

unread,
Dec 24, 2015, 7:00:11 PM12/24/15
to ggplot2
Hope it will be helpful,

To do the error plots with GGPLOT2, I used summarySE function to get statistical summary.

Then I graphed with ggplot(),

These are my referred codes

summarySE=function(data=NULL,measurevar,groupvars=NULL,na.rm=FALSE,conf.interval=.95, .drop=TRUE){

library(plyr)

length2=function(x,na.rm=FALSE){

if(na.rm)sum(!is.na(x))

else length(x)

 }

datac=ddply(data,groupvars,.drop=.drop,.fun=function(xx,col,na.rm){

c(n=length2(xx[,col],na.rm=na.rm),

mean=mean(xx[,col],na.rm=na.rm),

sd=sd(xx[,col],na.rm=na.rm))

},

 measurevar,

na.rm)

datac <-rename(datac,c("mean" =measurevar))

datac$se <-datac$sd /sqrt(datac$n)

ciMult <-qt(conf.interval/2 + .5,datac$n-1)

 datac$ci <-datac$se *ciMult

return(datac)

}

> sm=summarySE(data,measurevar="height",groupvars=c("DAS","treatment"))

> sm


p=ggplot(sm,aes(x=DAS,y=height,colour=treatment))

> p+geom_errorbar(aes(ymin=height-se,ymax=height+se),width=.1,position=pd)+geom_line(position=pd)+geom_point(position=pd)

result in attached file,

forgive me if have something not so clear, I'm just learning to use R (beginner) :)

Best regards,
height v3.jpeg

Sol Lago

unread,
Dec 25, 2015, 6:49:26 AM12/25/15
to ggplot2
Hi!

Thanks for your response and the code! This might get the job done, but what I meant was whether someone knew a way of plotting standard error bars that does not inlvoving pre-averaging the data frame (to calculate the standard error) as a previous step to plotting. This used to be possible in previous versions of ggplot (see my sample code above) but it is no longer the case.

best wishes,

Pedro Aphalo

unread,
Dec 26, 2015, 2:03:28 PM12/26/15
to ggplot2
Hi!

mean_se is now defined in the ggplot2 source code and exported. 

Have you tried this function with stat_summary?

The following modified example from the help for stat_summary works for me.

d <- ggplot(mtcars, aes(cyl, mpg)) + geom_point()
d
+ stat_summary(fun.data = "mean_se", colour = "red", size = 1)

Cheers,

Pedro.
Message has been deleted

Pedro Aphalo

unread,
Jan 3, 2016, 2:30:46 AM1/3/16
to ggplot2

A more general answer: in gglot2 2.0.0 the arguments to the function fun.data are no longer passed through ... but instead as a list through formal parameter fun.args. The code below is the exact equivalent to that in the original question.

ggplot(df, aes(x=condition, y=rt, fill=condition)) +
  stat_summary(fun.y = mean, geom = "bar", colour="black",size=.3) + 
  stat_summary(fun.data = mean_cl_normal, fun.args = list(mult = 1), geom = "errorbar")

or using code that can be run:

d <- ggplot(mtcars, aes(cyl, mpg)) + geom_point()
d + stat_summary(fun.data = "mean_cl_normal", fun.args = list(mult = 1), colour = "red", size = 1)
Reply all
Reply to author
Forward
0 new messages