更改 geom_boxplot 中的晶须定义 [英] Changing whisker definition in geom_boxplot
问题描述
我正在尝试使用 ggplot2/geom_boxplot 生成一个箱线图,其中胡须被定义为第 5 个和第 95 个百分位数,而不是 0.25 - 1.5 IQR/0.75 + IQR,并且像往常一样绘制来自这些新胡须的异常值.我可以看到 geom_boxplot 美学包括 ymax/ymin,但我不清楚我是如何在这里放置值的.好像是:
I'm trying to use ggplot2 / geom_boxplot to produce a boxplot where the whiskers are defined as the 5 and 95th percentile instead of 0.25 - 1.5 IQR / 0.75 + IQR and outliers from those new whiskers are plotted as usual. I can see that the geom_boxplot aesthetics include ymax / ymin, but it's not clear to me how I put values in here. It seems like:
stat_quantile(quantiles = c(0.05, 0.25, 0.5, 0.75, 0.95))
应该可以提供帮助,但我不知道如何将这个统计结果的结果关联起来以设置适当的 geom_boxplot() 美学:
should be able to help, but I don't know how to relate the results of this stat to set the appropriate geom_boxplot() aesthetics:
geom_boxplot(aes(ymin, lower, middle, upper, ymax))
我看过其他帖子,其中人们提到基本上手动构建一个类似箱线图的对象,但我宁愿保持整个箱线图格式塔不变,只是修改绘制的两个变量的含义.
I've seen other posts where people mention essentially building a boxplot-like object manually, but I'd rather keep the whole boxplot gestalt intact, just revising the meaning of two of the variables being drawn.
推荐答案
geom_boxplot with stat_summary 可以做到:
geom_boxplot with stat_summary can do it:
# define the summary function
f <- function(x) {
r <- quantile(x, probs = c(0.05, 0.25, 0.5, 0.75, 0.95))
names(r) <- c("ymin", "lower", "middle", "upper", "ymax")
r
}
# sample data
d <- data.frame(x=gl(2,50), y=rnorm(100))
# do it
ggplot(d, aes(x, y)) + stat_summary(fun.data = f, geom="boxplot")
# example with outliers
# define outlier as you want
o <- function(x) {
subset(x, x < quantile(x)[2] | quantile(x)[4] < x)
}
# do it
ggplot(d, aes(x, y)) +
stat_summary(fun.data=f, geom="boxplot") +
stat_summary(fun.y = o, geom="point")
这篇关于更改 geom_boxplot 中的晶须定义的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!