在条形图中重新调整y轴会导致条消失:R ggplot2

问题描述:

我尝试使用ggplot2创建一个barplot,y轴从大于零的值开始。

I am trying to create a barplot using ggplot2, with the y axis starting at a value greater than zero.

可以说我有手段和标准错误关于在三个不同农场的胡萝卜长度的假设数据集:

Lets say I have the means and standard errors for hypothetical dataset about carrot length at three different farms:

carrots<-NULL
carrots$Mean<-c(270,250,240)
carrots$SE<-c(3,4,5)
carrots$Farm<-c("Plains","Hill","Valley")
carrots<-data.frame(carrots)

我创建了一个基本情节:

I create a basic plot:

p<-ggplot(carrots,aes(y=Mean,x=Farm)) +
   geom_bar(fill="slateblue") +
   geom_errorbar(aes(ymin=Mean-SE,ymax=Mean+SE), width=0)
p

这很好,但是当比例从0到0时很难看出长度的差异。因此,我想将y轴重新缩放到c(200,300)之类的东西。然而,当我尝试这样做时:

This is nice, but as the scale runs from 0 to it is difficult to see the differences in length. Therefore, I would like to rescale the y axis to something like c(200,300). However, when I try to do this with:

p+scale_y_continuous('Length (mm)', limit=c(200,300))

条形消失,尽管错误条仍然存在。

The bars disappear, although the error bars remain.

我的问题是:是否可以使用ggplot2绘制带有此调整轴的barplot?

My question is: is it possible to plot a barplot with this adjusted axis using ggplot2?

感谢您提供任何帮助或建议。

Thank you for any help or suggestions you can offer.

试试这个

Try this

p + coord_cartesian(ylim=c(200,300))

在坐标系上设置限制执行视觉缩放;
数据没有变化,我们只查看原始图的一小部分。

Setting the limits on the coordinate system performs a visual zoom; the data is unchanged, and we just view a small portion of the original plot.