用ggplot躲避点和误差线

时间:2014-09-11 13:58:51

标签: r ggplot2

考虑这些数据(注意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

我想根据bar绘制多个foo的{​​{1}}个值,并使用CI绘制误差线。这是我的所作所为:

outcome

我明白了:

但是我想让它躲避错误条和点,这样我才能看到重叠。 Using position_jitter did that,但它完全是随机的(或"clunky") - 我不希望这样。

如何抵消个别观察?

或者这是ggplot的错误吗? example hereshows this error

1 个答案:

答案 0 :(得分:9)

一种可能性是group by' bar'。请注意,我还dodge geom_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)

enter image description here

相关问题