忽略ggplot2 boxplot中的异常值
问题描述:
我如何忽略ggplot2 boxplot中的异常值?我不想让它们消失(即outlier.size = 0),但我希望它们被忽略,使得y轴缩放以显示第一/第三百分位数。我的异常值导致盒子缩小到几乎一条线。是否有一些技巧来解决这个问题?
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")
答
这是一个使用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)