忽略 ggplot2 boxplot 中的异常值 [英] Ignore outliers in ggplot2 boxplot

查看:46
本文介绍了忽略 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)

这篇关于忽略 ggplot2 boxplot 中的异常值的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆