【问题标题】:extra point at boxplot with with jittered points (ggplot2)带有抖动点的箱线图的额外点(ggplot2)
【发布时间】:2020-12-26 06:14:22
【问题描述】:

我使用下面的代码来探索带有ggplot2 的箱线图。

MyData<-data.frame(CASES=c(rep("Good",10),rep("NotGood",10)), NUMBERS=c(2,3,1,5,6,3,2,6,8,3,1,3,6,8,17,3,2,5,7,20))

library(ggplot2)
MyBoxplot <- ggplot(MyData, aes(x=CASES, y=NUMBERS)) + 
  geom_boxplot()
MyBoxplot+ geom_jitter(shape=16, position=position_jitter(0.2))

我注意到,如果我的数据没有任何异常值 (Good),那么箱线图应该有 10 个点。但是如果有一些异常值(NotGood),那么异常值就会加倍。

有什么问题?

【问题讨论】:

  • 如果您的类别“不好”,geom_boxplot 已经绘制了两个异常值。因此,当添加 geom_jitter 时,这两点会加倍。
  • @stefan ,我以为这是问题所在,但我不敢相信......我可以解决这个问题吗?

标签: r ggplot2 boxplot outliers


【解决方案1】:

正如@stefan 所说,geom_boxplot() 会自动将异常值绘制为与您的 x 值对齐的点,因此异常值点被表示两次。没有从geom_boxplot() 中“删除”异常点的函数/参数;但是,您可以通过outlier.color= 使箱线图中的异常点透明化来获得相同的效果。

以下面的数据集为例,它有一些异常值。我调整了geom_jitter() 中的形状,以便更容易看出哪些点来自异常值(而不是geom_jitter)。

library(ggplot2)

set.seed(1234)
df <- data.frame(x=rep(c('A','B'), each=100), y=c(rnorm(100, 10, 20), rnorm(100, 30, 50)))

ggplot(df, aes(x, y)) +
  geom_boxplot() +
  geom_jitter(position=position_jitter(0.2), shape=3)

如果我们设置outlier.color=NA,这会使箱线图中的那些异常值“透明”,因此不再在图中观察到它们:

ggplot(df, aes(x, y)) +
  geom_boxplot(outlier.color=NA) +
  geom_jitter(position=position_jitter(0.2), shape=3)

【讨论】:

  • 非常感谢!最后,我还找到了一些“去除”异常值的其他方法,例如:geom_boxplot(outlier.shape = NA)geom_boxplot(outlier.size = -1)
猜你喜欢
  • 1970-01-01
  • 2017-10-13
  • 1970-01-01
  • 2018-08-06
  • 2017-06-05
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多