忽略 ggplot2 箱线图中的异常值
如何忽略 ggplot2 箱线图中的异常值?我不只是希望它们消失(即 outlier.size=0),但我希望它们被忽略,以便 y 轴缩放以显示第 1/第 3 个百分位数。我的异常值导致“盒子”缩小得几乎是一条线。有一些技术可以解决这个问题吗?
编辑 这是一个示例:
y = c(.01, .02, .03, .04, .05, .06, .07, .08, .09, .5, -.6)
qplot(1, y, geom="boxplot")
How would I ignore outliers in ggplot2 boxplot? I don't simply want them to disappear (i.e. outlier.size=0), but I want them to be ignored such that the y axis scales to show 1st/3rd percentile. My outliers are causing the "box" to shrink so small its practically a line. Are there some techniques to deal with this?
Edit
Here's an example:
y = c(.01, .02, .03, .04, .05, .06, .07, .08, .09, .5, -.6)
qplot(1, y, geom="boxplot")
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(9)
使用
geom_boxplot(outlier.shape = NA)
来不显示异常值和scale_y_continuous(limits = c(lower, upper))
更改轴限制。一个例子。
实际上,正如 Ramnath 在他的回答中所表明的那样(Andrie也在评论中也表明了这一点),在计算统计数据后通过
coord_cartesian
。(您可能仍然需要使用
scale_y_continuous
来修复轴中断。)Use
geom_boxplot(outlier.shape = NA)
to not display the outliers andscale_y_continuous(limits = c(lower, upper))
to change the axis limits.An example.
Actually, as Ramnath showed in his answer (and Andrie too in the comments), it makes more sense to crop the scales after you calculate the statistic, via
coord_cartesian
.(You'll probably still need to use
scale_y_continuous
to fix the axis breaks.)这是使用 boxplot.stats 的解决方案
Here is a solution using boxplot.stats
我遇到了同样的问题,并使用 boxplot.stats 预先计算了 Q1、Q2、中位数、ymin、ymax 的值:
结果是没有异常值的箱线图。
I had the same problem and precomputed the values for Q1, Q2, median, ymin, ymax using
boxplot.stats
:The result is a boxplot without outliers.
一种想法是在两遍过程中对数据进行winsorize :
运行第一遍,了解界限是什么,例如在给定百分位处切割,或高于平均值的N个标准差,或...
在第二遍中,将超出给定界限的值设置为该界限的值
我应该强调,这是一种老式方法,应该由更现代稳健的技术主导,但您仍然会来跨越它很多。
One idea would be to winsorize the data in a two-pass procedure:
run a first pass, learn what the bounds are, e.g. cut of at given percentile, or N standard deviation above the mean, or ...
in a second pass, set the values beyond the given bound to the value of that bound
I should stress that this is an old-fashioned method which ought to be dominated by more modern robust techniques but you still come across it a lot.
gg.layers::geom_boxplot2 正是您想要的。
https://rpkgs.github.io/gg.layers/reference/geom_boxplot2。 html
gg.layers::geom_boxplot2
is just what you want.https://rpkgs.github.io/gg.layers/reference/geom_boxplot2.html
如果您想强制胡须延伸到最大值和最小值,您可以调整 coef 参数。 coef 的默认值为 1.5(即晶须的默认长度是 IQR 的 1.5 倍)。
If you want to force the whiskers to extend to the max and min values, you can tweak the
coef
argument. Default value forcoef
is 1.5 (i.e. default length of the whiskers is 1.5 times the IQR).ggplot2 3.5.0 中的新增功能是选项
outliers = FALSE
。在计算 y 轴范围时,它不会计算异常值。如果使用
outlier.shape = NA,您仍然可以将数据范围保留在 y 轴上
或异常值.alpha = 0
。创建于 2024 年 2 月 27 日,使用 reprex v2.1.0
New in ggplot2 3.5.0 is the option
outliers = FALSE
. It will not count outliers towards calculating the range of the y-axis.You can still keep the data range on the y-axis if you use
outlier.shape = NA
oroutlier.alpha = 0
.Created on 2024-02-27 with reprex v2.1.0
简单、肮脏、有效。
geom_boxplot(异常值.alpha = 0)
Simple, dirty and effective.
geom_boxplot(outlier.alpha = 0)
geom_boxplot 函数的“coef”选项允许根据四分位数范围更改异常值截止值。此选项记录在函数 stat_boxplot 中。要停用异常值(换句话说,它们被视为常规数据),可以指定一个非常高的截止值,而不是使用默认值 1.5:
The "coef" option of the geom_boxplot function allows to change the outlier cutoff in terms of interquartile ranges. This option is documented for the function stat_boxplot. To deactivate outliers (in other words they are treated as regular data), one can instead of using the default value of 1.5 specify a very high cutoff value: