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
452 views
in Technique[技术] by (71.8m points)

r - geom_bar bars not displaying when specifying ylim

I'm having a problem with geom_bars wherein the bars are not rendered when I specify limits on the y-axis. I believe the following should reproduce the problem:

data <- structure(list(RoleCond = structure(c(1L, 1L, 2L, 2L), .Label = c("Buyer", "Seller"), class = "factor"), 
                   ArgCond = structure(c(1L, 2L, 1L, 2L), .Label = c("No Argument", "Argument"), class = "factor"), 
                   mean = c(2210.71428571429, 2142.70833333333, 2282.40740740741, 2346.2962962963), 
                   se = c(20.1231042081511, 16.7408757749718, 20.1471554637891, 15.708092540868)), 
                   .Names = c("RoleCond", "ArgCond", "mean", "se"), row.names = c(NA, -4L), class = "data.frame")

library(ggplot2)    
ggplot(data=data, aes(fill=RoleCond, y=mean, x=ArgCond)) + 
      geom_bar(position="dodge", stat="identity") + 
      geom_errorbar(limits, position=dodge, width=0.1, size=.75) + 
      scale_y_continuous(limits=c(2000,2500))

which gives me this

no bars

The same code without the limits specified works fine. The geom_errorbar() doesn't seem to be related to the problem, but it does illustrate where the bars should be showing up.

I've tried using coord_cartesian(ylim=c(2000,2500)) which works for limiting the yaxis and getting the bars to display, but the axis labels get messed up and I don't understand what I'm doing with it.

Thanks for any suggestions! (I'm using R 2.15.0 and ggplot2 0.9.0)

See Question&Answers more detail:os

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

1 Reply

0 votes
by (71.8m points)

You could try, with library(scales):

+ scale_y_continuous(limits=c(2000,2500),oob = rescale_none)

instead, as outlined here.


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

...