使用geom_boxplot(outlier.shape = NA)
来不显示异常值,并使用scale_y_continuous(limits = c(lower, upper))
来更改轴限制。
一个例子。
n <- 1e4L
dfr <- data.frame(
y = exp(rlnorm(n)), #really right-skewed variable
f = gl(2, n / 2)
)
p <- ggplot(dfr, aes(f, y)) +
geom_boxplot()
p # big outlier causes quartiles to look too slim
p2 <- ggplot(dfr, aes(f, y)) +
geom_boxplot(outlier.shape = NA) +
scale_y_continuous(limits = quantile(dfr$y, c(0.1, 0.9)))
p2 # no outliers plotted, range shifted
实际上,就像Ramnath在他的答案中所示(Andrie在评论中也是如此),更有意义的做法是在计算统计量后通过coord_cartesian
裁剪刻度。
coord_cartesian(ylim = quantile(dfr$y, c(0.1, 0.9)))
(你可能仍然需要使用scale_y_continuous
来修复坐标轴的断点.)
coord_cartesian()
在我使用时与 coord_flip()
不兼容,因此我更喜欢 scale_y_continuous()
。 - PatrickT这里有一个使用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)
ylim <- c(-0.1, 1000) * 1.05
得到 [1] 0.105 1050
。为了获得均匀分布在平均线周围的限制值,您可以使用 ylim + c(-0.05, 0.05) * diff(ylim) / 2
。在我看来更美观。 - Bram Visserfacet_grid()
,该图不起作用。那么你会得到多个箱线图而不是一个,因此你无法获得正确的限制范围。 - WitheShadow我曾经遇到同样的问题,使用boxplot.stats
预先计算了Q1、Q2、中位数、ymin和ymax:
# Load package and generate data
library(ggplot2)
data <- rnorm(100)
# Compute boxplot statistics
stats <- boxplot.stats(data)$stats
df <- data.frame(x="label1", ymin=stats[1], lower=stats[2], middle=stats[3],
upper=stats[4], ymax=stats[5])
# Create plot
p <- ggplot(df, aes(x=x, lower=lower, upper=upper, middle=middle, ymin=ymin,
ymax=ymax)) +
geom_boxplot(stat="identity")
p
结果是一个不包含异常值的箱线图。
一种方法是通过两次操作来进行数据Winsorizing:
首先进行第一遍操作,确定边界,可以在给定百分位数处截断,或者在均值上方N个标准差处截断等等。
第二遍操作,将超出给定边界的值设置为该边界的值。
需要强调的是,这是一种旧式方法,应该被更现代、更健壮的技术所替代,但你仍然会经常遇到它。
DescTools::Winsorize
,statar::winsorize
,robustHD::winsorize
- 我遇到了这些,但我认为还有更多)。 - Konradgg.layers::geom_boxplot2
正是您所需要的。
# remotes::install_github('rpkgs/gg.layers')
library(gg.layers)
library(ggplot2)
p <- ggplot(mpg, aes(class, hwy))
p + geom_boxplot2(width = 0.8, width.errorbar = 0.5)
https://rpkgs.github.io/gg.layers/reference/geom_boxplot2.html
geom_boxplot()
有何不同,除了更改箱体和/或须的宽度选项? - jtr13width
和width.errorbar
。您可以在 https://rpkgs.github.io/gg.layers/reference/geom_boxplot2.html 中找到示例。 - Dongdong Kong如果您想强制将胡须延伸到最大和最小值,您可以调整coef
参数。默认情况下,coef
的值为1.5(即胡须的默认长度是IQR的1.5倍)。
# Load package and create a dummy data frame with outliers
#(using example from Ramnath's answer above)
library(ggplot2)
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)))
# create boxplot where whiskers extend to max and min values
p1 = ggplot(df, aes(y = y)) + geom_boxplot(aes(x = factor(1)), coef = 500)
简单、粗糙而有效。 geom_boxplot(outlier.alpha = 0)
library(ggplot2)
# generate data with outliers:
df = data.frame(x=1, y = c(-10, rnorm(100), 10))
# generate plot with increased cutoff for outliers:
ggplot(df, aes(x, y)) + geom_boxplot(coef=1e30)
fivenum()
以提取箱线图上使用的上下限,并将该输出用于@Ritchie所示的scale_y_continuous()
调用中。使用R和ggplot提供的工具可以轻松自动化此过程。如果您还需要包括whiskers,请考虑使用boxplot.stats()
获取whiskers的上限和下限,并在scale_y_continuous()
中使用它们。 - Gavin Simpson