简体   繁体   中英

Adding Error Bars in R with ggplot

I would like to make a plot in R plotting two categorical variables (treat) by LWP.

I would like to change the color of the dots (the fill) to correspond with the variable rootpatH (one dot in each treatment).

I would also like to add a notation if the differences are significant or not..

Plants.means<-aggregate(Plants[10:44],Plants[c(6,9)],mean,na.rm=TRUE)

SE<-function(x)(sd(x,na.rm=TRUE)/ (length(x)-sum(is.na(x)))^0.5)


ggplot(Plants.means, aes(x=Treat, y=Yield, group=1)) +
geom_errorbar(width=.1, aes(ymin=Yield-SE(Plants.means$Yield), ymax=Yield+SE(Plants.means$Yield))) +
geom_point(shape=21, size=3, fill="white") +
ylim(0,1750)

I also want to change the error bars to represent 95% CI instead of SE.

Treat is categorical rootpatH is categorical LWP_meas2 is continuous

在此处输入图片说明

As Ben says you probably want something like this (of necessity not tested since no data was offered:

with( Plants.means,
               errbar(x =Treat, y = LWP_Meas2, 
                                yplus =  LWP_Meas2 + SE( LWP_Meas2), 
                                yminus = LWP_Meas2 - SE( LWP_Meas2), 
       add=T,na.rm=TRUE, col=c("blue", "hotpink"))
      )
error <- function(x)(qt(0.975,df=length(x)-1)*sd(x)/sqrt(length(x)))

ggplot(Plants.means,aes(x=Treat, y=Yield, colour=rootpatH, group=rootpatH)) +
geom_errorbar(aes(ymin=Yield-error(Plants.means$Yield),ymax=Yield+error(Plants.means$Yield)), colour="black",width=.1, position=pd) +
geom_point(position=pd, size=4) 

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM