【问题标题】:Dodging points and error bars with ggplot用 ggplot 躲避点和误差线
【发布时间】:2014-11-05 12:28:41
【问题描述】:

考虑这个数据(注意foo实际上是一个因素。):

foo bar outcome ci
1   a   0.683333333 0.247447165
2   b   0.941666667 0.180356565
3   c   0.783333333 0.335337789
1   d   0.866666667 0.204453706
2   e   0.45    0.303059647
3   f   0.325   0.340780173

我想为每个foo 值绘制多个bars,它们的outcome 和带有CI 的误差线。这是我的工作:

ggplot(ex, aes(foo, outcome, label = bar)) + 
  geom_point(position = position_dodge(.1)) + 
  geom_errorbar(aes(ymin = outcome - ci, ymax = outcome + ci), position = position_dodge(.1)) + 
  geom_text(hjust = 2)

我明白了:

但我希望它避开误差线和点,以便我可以看到重叠。 Using position_jitter did that,但它完全是随机的(或 "clunky")——我不希望这样。

如何抵消个别观察?

或者这是 ggplot 的错误? example here 也有shows this error

【问题讨论】:

    标签: r ggplot2


    【解决方案1】:

    一种可能性是通过'bar'group。注意我也dodgegeom_text

    dodge <- position_dodge(.1)
    
    ggplot(data = df, aes(x = foo, y = outcome, group = bar, label = bar)) + 
      geom_point(position = dodge) + 
      geom_errorbar(aes(ymin = outcome - ci, ymax = outcome + ci), position = dodge) + 
      geom_text(hjust = 2, position = dodge)
    

    【讨论】:

    • 太棒了,谢谢。您能否指出group 的文档或解释在这种情况下它为什么起作用(或它的作用)?
    • @slhck,您可以查看?aes_group_orderhere 的帮助文本,您还可以在其中找到所有示例的输出。
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 2021-10-26
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2015-12-26
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多