4

I'm plotting bar plots with error bars but I can't figure out how to suppress the lower part of the error bar. Does anyone has an idea how I could do that?

This is my code:

barplot <- qplot(x=..., y=mean, fill=variable,
             data=dat, geom="bar", stat="identity",
             position="dodge")

barplot + geom_errorbar(aes(ymax=upper, ymin=lower), 
                    position=position_dodge(7),
                    data=dat)

So, the goal is that only the part of the error bar that is defined by "ymax=upper" shows in the graph but "ymin=lower" does not.

I tried with giving each cell in the column "lower" the value zero but this didn't work:

dat<- transform(dat, lower="0", upper=mean+sem)

Well, thanks in advance!

Sheldor
  • 41
  • 1
  • 2
  • 1
    It might be sufficient to change the order of the layers, i.e., print the error bars and then the bars on top of them. – Roland Sep 08 '14 at 12:25
  • A [reproducible example](http://stackoverflow.com/questions/5963269/how-to-make-a-great-r-reproducible-example) would be nice here because we really have no idea that this plot looks like to you right now. – MrFlick Sep 08 '14 at 12:38
  • Does the following transformation work? dat<- transform(dat, lower=mean, upper=mean+sem) – Pierre Sep 08 '14 at 14:00
  • @ Roland: Did you mean like: geom_errorbar(...) + barplot Unfortunately, it does not work. I just get the response "NULL" – Sheldor Sep 08 '14 at 14:02
  • @ Pierre: Nearly, now the lower error bar is exactly on the upper line of the bar. I guess, I could hide it under a black line drawn around each bar. Nice trick! – Sheldor Sep 08 '14 at 14:28
  • @Sheldor; For Rolands suggestion try `ggplot(dat, aes( x=..., y=mean, fill=variable, ymax=upper, ymin=lower)) + geom_errorbar(position="dodge") + geom_bar(stat="identity", position="dodge")` – user20650 Sep 08 '14 at 14:48

1 Answers1

7

I know that the post is old, but it is now when I front this problem. These options work if you want to add a geom_errorbar to a geom_bar, but if you want to plot a geom_point + geom_bar, an horizontal line will appear over your point.

For solve that problem I have found a 'trap'.

ggplot(data, (aes(x...) + geom_point() + 
geom_errorbar(aes(ymin = upper, ymax = upper)) + 
geom_linerange(aes(ymin = mean, ymax = upper))

Using this code you will obtain only the upper line, because the lower line is overlapping the upper, and with geom_linerange you get the vertical line.

Nacho Glez
  • 385
  • 3
  • 15