我遇到了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"),
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))
Run Code Online (Sandbox Code Playgroud)
这给了我这个
没有指定限制的相同代码工作正常.geom_errorbar()似乎与问题无关,但它确实说明了条形图应该出现的位置.
我已经尝试过使用coord_cartesian(ylim=c(2000,2500))
哪种方法来限制y轴并显示条形图,但轴标签搞砸了,我不明白我在做什么.
谢谢你的任何建议!(我使用的是R 2.15.0和ggplot2 0.9.0)
jor*_*ran 47
您可以尝试library(scales)
:
+ scale_y_continuous(limits=c(2000,2500),oob = rescale_none)
Run Code Online (Sandbox Code Playgroud)
相反,所概述这里.
Mek*_*lay 11
为我的案例添加一个答案,如果有人遇到这个答案会有所不同:
使用时position="dodge"
,条形会自动水平调整大小以填充通常远远超出数据本身限制的空间.因此,即使您x-axis
和您的y-axis
限制都是limits=c(min-1, max+1
,对于某些数据集,position="dodge"
可能会将其调整到超出该限制范围,导致条形不显示.如果您的限制下限为0,甚至可能发生这种情况,与上述情况不同.
oob=rescale_none
在两个scale_y_continous()
AND中使用scale_x_continuous()
只需切断完成的大小调整就可以解决这个问题position="dodge"
.
根据之前的评论,它需要先package:scales
运行library(scales)
.
希望这可以帮助其他人,上面的答案只会让你分道扬..