指定 ylim 时不显示 geom_bar 条 [英] geom_bar bars not displaying when specifying ylim

查看:24
本文介绍了指定 ylim 时不显示 geom_bar 条的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我在使用 geom_bars 时遇到问题,其中当我在 y 轴上指定限制时不呈现条形图.我相信以下内容应该会重现该问题:

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"),平均值 = 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")图书馆(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))

这给了我这个

没有指定限制的相同代码工作正常.geom_errorbar() 似乎与问题无关,但它确实说明了应该显示条形的位置.

我尝试过使用 coord_cartesian(ylim=c(2000,2500)) 来限制 yaxis 并显示条形,但是轴标签弄乱了,我不不明白我在用它做什么.

感谢您的任何建议!(我使用的是 R 2.15.0 和 ggplot2 0.9.0)

解决方案

你可以试试,用library(scales):

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

相反,如此处所述.>

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

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)

解决方案

You could try, with library(scales):

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

instead, as outlined here.

这篇关于指定 ylim 时不显示 geom_bar 条的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆