忽略 ggplot2 boxplot 中的异常值
问题描述:
我如何忽略 ggplot2 boxplot 中的异常值?我不只是希望它们消失(即 outlier.size=0),但我希望它们被忽略,以便 y 轴缩放以显示第 1/3 个百分位数.我的异常值导致盒子"缩小到几乎是一条线.有什么技巧可以解决这个问题吗?
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?
编辑举个例子:
y = c(.01, .02, .03, .04, .05, .06, .07, .08, .09, .5, -.6)
qplot(1, y, geom="boxplot")
答
这里是一个使用 boxplot.stats 的解决方案
Here is a solution using boxplot.stats
# create a dummy data frame with outliers
df = data.frame(y = c(-100, rnorm(100), 100))
# create boxplot that includes outliers
p0 = ggplot(df, aes(y = y)) + geom_boxplot(aes(x = factor(1)))
# compute lower and upper whiskers
ylim1 = boxplot.stats(df$y)$stats[c(1, 5)]
# scale y limits based on ylim1
p1 = p0 + coord_cartesian(ylim = ylim1*1.05)