R bar graphs with error bars -
i relatively new r , have been trying figure out how can add error bars bar graphs. use simple example, have prevalence data of bacteria 2 years i'm hoping add error bars to. start, create data frame x , y values standard error 95% confidence interval:
>df<-data.frame(year=factor(c(2011,2012)),ms_prevalence=c(16.02,7.08),se=c(.20750,.10325))
i set upper , lower limits error bars:
>limits<-aes(ymax=ms_prevalence+se,ymin=ms_prevalence-se)
next, set graph p:
>p<-ggplot(df,aes(y=ms_prevalence,x=year))
now add bars graph:
>p+geom_bar(position="dodge",stat="identity")
i select width of bars:
>dodge<-position_dodge(width=0.9)
then, attempt add error bars:
>p+geom_bar(position=dodge)+geom_errorbar(limits,position=dodge,width=0.25)
when add error bars, graph turns bar line. while include error bars, need bar graph appropriately represent data. appreciated!
try:
ggplot(df) + geom_bar(aes(x=year, y=ms_prevalence), stat='identity', color=gray)+ geom_errorbar(aes(x=year, ymin=(ms_prevalence-se),ymax=(ms_prevalence+se)), width=0.25)
Comments
Post a Comment