Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
416 views
in Technique[技术] by (71.8m points)

r - Placement of error bars in barplot using ggplot2

Hi i am using ggplot version 0.9.3.2 in RStudio and I am trying to make a bar plot with error bars. The problem is that the error bars are positioned wrong. I need them to go on top of the individual bars.

I have a data frame like this

   concentration variable     value          sd
1              0     AF_B 0.3567126 0.010391001
2           0.5x     AF_B 0.3355766 0.003480245
3             1x     AF_B 0.3001138 0.009104821
4             5x     AF_B 0.2658911 0.016312390
5            10x     AF_B 0.2115522 0.011056590
6           100x     AF_B 0.2655958 0.015092367
7              0      D_B 0.3567126 0.010391001
8           0.5x      D_B 0.3453078 0.011639252
9             1x      D_B 0.3380180 0.004357810
10            5x      D_B 0.3349004 0.018119644
11           10x      D_B 0.3186451 0.014515436
12          100x      D_B 0.3174700 0.016685932

I have the following code

    dodge = position_dodge(width=0.9)
    c = ggplot(data=dm, aes(y=value,x=concentration))
    c + geom_bar(position = dodge, 
         stat="identity",
         aes(fill=variable,colour=variable,group=variable, colour="black")) +
    geom_errorbar(aes(ymin=value-sd,ymax=value+sd),
          position=dodge,
          width=0.1,
          size=0.3) +
    ylab("mu_max [h-1]") +
    scale_x_discrete(limits=c("0","0.5x","1x","5x","10x","100x"))

, and gives me this plot, which is clearly

http://i.imgur.com/1zwbxMv.png

See Question&Answers more detail:os

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Reply

0 votes
by (71.8m points)

You should move the argument fill=variable inside the ggplot() call to ensure that geom_errorbar() also use variable as the dodging variable.

ggplot(data=dm,aes(y=value,x=concentration,fill=variable))+
  geom_bar(position = position_dodge(), 
           stat="identity") +
  geom_errorbar(aes(ymin=value-sd,ymax=value+sd),
                position=dodge,width=0.1,size=0.3)+
  ylab("mu_max [h-1]") +
  scale_x_discrete(limits=c("0","0.5x","1x","5x","10x","100x"))

enter image description here


与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
OGeek|极客中国-欢迎来到极客的世界,一个免费开放的程序员编程交流平台!开放,进步,分享!让技术改变生活,让极客改变未来! Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...