Welcome to OStack Knowledge Sharing Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

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

ggplot2 - R ggplot: suppress bottom of error bar on geom_bar

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!

See Question&Answers more detail:os

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

1 Answer

0 votes
by (71.8m points)

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.


与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome to OStack Knowledge Sharing Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...