本文介绍了用ggplot躲避点和误差线的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

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

  foo bar结果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 值,其中结果和带有CI的错误栏。这是我做的:

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

我得到:



但我希望它避开误差线和点我可以看到重叠。 ) - 我不希望这样。



如何抵消个人观察?



或者这是一个错误与ggplot? 也。

一种可能性是'bar'对 group >。请注意,我也闪避 geom_text



<$ p $ (数据= df,aes(x = foo,y =结果,组=条,标签=条))。 +
geom_point(position = dodge)+
geom_errorbar(aes(ymin =结果 - ci,ymax =结果+ ci),position = dodge)+
geom_text(hjust = 2,position =闪避)

Consider this data (note that foo is actually a factor.):

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

I want to plot multiple bars per foo value, with their outcome and error bars with CI. Here's what I do:

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)

I get:

But I wanted it to dodge the error bars and points so I can see the overlap. Using position_jitter did that, but it was totally random (or "clunky") — I don't want that.

How can I offset the individual observations?

Or is this a bug with ggplot? The example here also shows this error.

解决方案

One possibility is to group by 'bar'. Note that I also dodge the 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)

这篇关于用ggplot躲避点和误差线的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!

10-13 15:02