忽略 ggplot2 geom_violin 中的异常值

2024-04-12

有没有办法忽略异常值geom_violiny 轴图是否与 Q1 和 Q3 分位数相关? (range=1.5以 R 为基数)。如果这可以自动化(即不只是调用特定的 y 轴限制),那就太好了。

我在这里看到使用 geom_boxplot 的解决方案:忽略 ggplot2 箱线图中的异常值 https://stackoverflow.com/questions/5677885/ignore-outliers-in-ggplot2-boxplot

但是有没有办法复制这种类型的解决方案geom_violin?提前致谢!

下面的示例代码具有期望的结果

library(ggplot2)
Result <- as.numeric(c(.2, .03, .11,  .05, .2, .02, .22, 1.1, .02, 120))
Group <- as.factor(c("a", "a", "a", "b", "b", "b", "c", "c", "c", "c"))
x <- data.frame(Result, Group)

plot = ggplot(x, aes(x=Group, y=Result)) +
  geom_violin()

print(plot)

这是上面的输出(不是超级有用的图形):

I'd like something like the plot below using the above data: enter image description here


我认为与您链接到的方法类似的方法将在这里工作,除了您需要计算每个组的这些统计数据并使用最小 Q1 和最大 Q3 作为coord_cartesian:

library(dplyr)
# compute lower and upper whiskers for each group
ylims <- x %>%
  group_by(Group) %>%
  summarise(Q1 = quantile(Result, 1/4), Q3 = quantile(Result, 3/4)) %>%
  ungroup() %>%
  #get lowest Q1 and highest Q3
  summarise(lowQ1 = min(Q1), highQ3 = max(Q3))

plot + coord_cartesian(ylim = as.numeric(ylims)*1.05)

请注意,您可以在调用中更改缩放比例coord_cartesian分位数在计算 Q1 和 Q3 范围的管道代码位中中断。

本文内容由网友自发贡献,版权归原作者所有,本站不承担相应法律责任。如您发现有涉嫌抄袭侵权的内容,请联系:hwhale#tublm.com(使用前将#替换为@)

忽略 ggplot2 geom_violin 中的异常值 的相关文章

随机推荐