ggplot2 箱线图空白填充
时间: 2023-09-20 22:13:16 浏览: 290
在 ggplot2 中,你可以使用 `geom_boxplot()` 函数创建箱线图。要为箱线图的空白部分添加填充,可以使用 `fill` 参数来指定填充颜色。
下面是一个示例代码,演示如何创建带有空白填充的箱线图:
```R
library(ggplot2)
# 创建示例数据集
data <- data.frame(
group = rep(c("A", "B", "C"), each = 100),
value = c(rnorm(100), rnorm(100, mean = 2), rnorm(100, mean = -2))
)
# 创建箱线图
ggplot(data, aes(x = group, y = value, fill = group)) +
geom_boxplot() +
scale_fill_manual(values = c("A" = "red", "B" = "green", "C" = "blue")) +
theme_minimal()
```
在这个示例中,我们首先创建了一个示例数据集 `data`,其中包含了三个组(A、B、C)的随机值。然后,我们使用 `ggplot()` 函数创建一个基本的 ggplot 图表,并使用 `geom_boxplot()` 函数添加箱线图。通过在 `aes()` 函数中指定 `fill = group`,我们将每个组的箱线图空白部分填充成不同的颜色。最后,使用 `scale_fill_manual()` 函数可以手动设置填充颜色的映射关
阅读全文
相关推荐




