仅更改 `geom_boxplot` 中一个因子的一个水平的晶须定义
Change whisker definition for only one level of a factor in `geom_boxplot`
我正在尝试更改胡须定义以扩展到最小值和最大值(即,不要将任何东西视为异常值),如this question ,但仅适用于映射到 x 轴的 单个 水平的因子。该答案中的代码将更改整个图的晶须定义。
解决这个问题的正确方法是什么?
扩展问题中链接的示例,您可以执行以下操作:
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(data = subset(d, x == 1), fun.data = f, geom = "boxplot") +
geom_boxplot(data = subset(d, x == 2))
在这种情况下,因子 x == 2
得到 "regular" geom_boxplot
,但因子 x == 1
是 "extended".
在你的情况下,更抽象一点,你可能想做这样的事情:
ggplot(d, aes(x, y)) +
stat_summary(data = subset(d, x == "special_factor"), fun.data = f, geom = "boxplot") +
geom_boxplot(data = subset(d, x != "special_factor"))
我正在尝试更改胡须定义以扩展到最小值和最大值(即,不要将任何东西视为异常值),如this question ,但仅适用于映射到 x 轴的 单个 水平的因子。该答案中的代码将更改整个图的晶须定义。
解决这个问题的正确方法是什么?
扩展问题中链接的示例,您可以执行以下操作:
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(data = subset(d, x == 1), fun.data = f, geom = "boxplot") +
geom_boxplot(data = subset(d, x == 2))
在这种情况下,因子 x == 2
得到 "regular" geom_boxplot
,但因子 x == 1
是 "extended".
在你的情况下,更抽象一点,你可能想做这样的事情:
ggplot(d, aes(x, y)) +
stat_summary(data = subset(d, x == "special_factor"), fun.data = f, geom = "boxplot") +
geom_boxplot(data = subset(d, x != "special_factor"))