geom_boxplot 自定义分位数颜色
geom_boxplot custom color for quantiles
我有一个简单的 ggplot 箱线图,就像这个例子
library(ggplot2)
dat <- ToothGrowth
ToothGrowth$dose <- as.factor(ToothGrowth$dose)
p <- ggplot(ToothGrowth, aes(x=dose, y=len)) +
geom_boxplot() +
facet_grid(~supp)
p
目标:
我想要不同颜色的分位数框,如下图所示。
一个提示可能是这样link:
https://ggplot2.tidyverse.org/reference/geom_segment.html.
但我不确定如何为 ractangles 实现它。此外,我的绘图中有一个 facid_grid,着色也应该有效。你能看到我最终想要达到的效果吗
你有什么建议吗?
您可以这样做:首先,计算 median
,ToothGrowth
的第一个和第三个四分位数(quart1
和 quart3
),按 [=15] 分组=] 和 dose
。之后,您可以填充箱线图的下矩形和上矩形。因为你想要四种不同的颜色,所以我添加了四个不同的矩形。
ToothGrowth <- ToothGrowth %>% mutate(dose = factor(dose)) %>% group_by(supp, dose) %>%
mutate(median = median(len), quart1 = quantile(len, 0.25), quart3 = quantile(len, 0.75))
ggplot(ToothGrowth, aes(dose, len)) +
geom_boxplot(width = 0.6) +
facet_grid(~supp) +
geom_rect(data = subset(ToothGrowth, supp == "OJ"),
aes(xmin = as.numeric(dose) - 0.3, xmax = as.numeric(dose) + 0.3, ymin = quart1, ymax = median),
fill = "#008AA7", color = "black") +
geom_rect(data = subset(ToothGrowth, supp == "VC"),
aes(xmin = as.numeric(dose) - 0.3, xmax = as.numeric(dose) + 0.3, ymin = quart1, ymax = median),
fill = "#005645", color = "black") +
geom_rect(data = subset(ToothGrowth, supp == "OJ"),
aes(xmin = as.numeric(dose) - 0.3, xmax = as.numeric(dose) + 0.3, ymin = median, ymax = quart3),
fill = "#A2EFFD", color = "black") +
geom_rect(data = subset(ToothGrowth, supp == "VC"),
aes(xmin = as.numeric(dose) - 0.3, xmax = as.numeric(dose) + 0.3, ymin = median, ymax = quart3),
fill = "#90AC9E", color = "black")
给出以下情节:
我有一个简单的 ggplot 箱线图,就像这个例子
library(ggplot2)
dat <- ToothGrowth
ToothGrowth$dose <- as.factor(ToothGrowth$dose)
p <- ggplot(ToothGrowth, aes(x=dose, y=len)) +
geom_boxplot() +
facet_grid(~supp)
p
目标:
我想要不同颜色的分位数框,如下图所示。
一个提示可能是这样link: https://ggplot2.tidyverse.org/reference/geom_segment.html.
但我不确定如何为 ractangles 实现它。此外,我的绘图中有一个 facid_grid,着色也应该有效。你能看到我最终想要达到的效果吗
你有什么建议吗?
您可以这样做:首先,计算 median
,ToothGrowth
的第一个和第三个四分位数(quart1
和 quart3
),按 [=15] 分组=] 和 dose
。之后,您可以填充箱线图的下矩形和上矩形。因为你想要四种不同的颜色,所以我添加了四个不同的矩形。
ToothGrowth <- ToothGrowth %>% mutate(dose = factor(dose)) %>% group_by(supp, dose) %>%
mutate(median = median(len), quart1 = quantile(len, 0.25), quart3 = quantile(len, 0.75))
ggplot(ToothGrowth, aes(dose, len)) +
geom_boxplot(width = 0.6) +
facet_grid(~supp) +
geom_rect(data = subset(ToothGrowth, supp == "OJ"),
aes(xmin = as.numeric(dose) - 0.3, xmax = as.numeric(dose) + 0.3, ymin = quart1, ymax = median),
fill = "#008AA7", color = "black") +
geom_rect(data = subset(ToothGrowth, supp == "VC"),
aes(xmin = as.numeric(dose) - 0.3, xmax = as.numeric(dose) + 0.3, ymin = quart1, ymax = median),
fill = "#005645", color = "black") +
geom_rect(data = subset(ToothGrowth, supp == "OJ"),
aes(xmin = as.numeric(dose) - 0.3, xmax = as.numeric(dose) + 0.3, ymin = median, ymax = quart3),
fill = "#A2EFFD", color = "black") +
geom_rect(data = subset(ToothGrowth, supp == "VC"),
aes(xmin = as.numeric(dose) - 0.3, xmax = as.numeric(dose) + 0.3, ymin = median, ymax = quart3),
fill = "#90AC9E", color = "black")
给出以下情节: