【问题标题】:Error bars are rendered incorrectly when ymax exceeds Y axis scale当 ymax 超过 Y 轴刻度时,误差线渲染不正确
【发布时间】:2014-12-07 12:41:24
【问题描述】:

我想在scale_y_continuous 中使用limits 来“裁剪”我的Y 轴:

df1 <- data.frame(xx=c('a','b','c'),
                  yy=c(7, 10, 8),
                  se=c(2, 4, 2))
p <- ggplot(data=df1, mapping = aes(x=xx, y=yy)) + 
  geom_bar(data = df1, position=position_dodge(), stat="identity") +
  geom_errorbar(aes(ymin=yy-se, ymax=yy+se), width=0.1, position=position_dodge())

p # left plot

p + scale_y_continuous(limits=c(0,11)) # middle plot
# Warning message:
# Removed 4 rows containing missing values (geom_path).

在这种情况下,一个误差条超过了 Y 轴的上限,我希望它被裁剪(如右图),但实际上误差条的垂直线根本没有呈现(参见.情节在中间)。除了手动绘制误差线之外,还有什么办法可以解决这个问题?

【问题讨论】:

    标签: r ggplot2


    【解决方案1】:

    我相信

    library("scales")
    p + scale_y_continuous(limits=c(0,11),oob=squish)
    

    会做(未测试); “oob”代表“越界”

    更标准的答案是使用

    coord_cartesian(ylim=c(0,11))
    

    但这也会改变刻度间距等。

    【讨论】:

    • 太好了,谢谢! (oob=squish 对我不起作用,但 oob=rescale_none 完成了这项工作)
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2019-01-05
    • 1970-01-01
    • 1970-01-01
    • 2017-05-11
    相关资源
    最近更新 更多