箱线图的图例定制
Legend customization for boxplots
我有这个箱线图,然后我试图在它上面绘制 2 个不同的点,颜色不同,以便用户可以看到它们落在箱线图上的位置,并将它们相互比较。问题是,我希望图例显示具有相应颜色的两个点及其 ID。相反,图例显示的是箱线图 categories/colors。我如何覆盖图例以仅显示我想要的内容?这是我的代码:
library(datasets)
library(ggplot2)
data(airquality)
airquality$Month <- factor(airquality$Month,
labels = c("May", "Jun", "Jul", "Aug", "Sep"))
airquality$ID <-seq(1:nrow(airquality))
dataPoint <- airquality[11,]
dataPoint2 <- airquality[17,]
plt <- ggplot(airquality, aes(x = Month, y = Ozone, color = Month)) +
geom_boxplot(show.legend=TRUE,outlier.shape = NA) +
geom_point(data = dataPoint, color='darkblue', aes(x = Month, y = Ozone), size = 3,show.legend=TRUE) +
geom_point(data = dataPoint2, color='darkred', aes(x = Month, y = Ozone), size = 3,show.legend=TRUE) +
theme(legend.position = "bottom")
plt
我会通过将这些点映射到不同的美学来实现这一点。如果在这种情况下 show.legend
设置为 FALSE
,则图例将单独显示。您还可以映射到点形状或任何其他美学。或者,您可以映射箱线图 geom 的 fill
,并映射点 geom 的 color
。
例如:
library(datasets)
library(ggplot2)
data(airquality)
airquality$Month <- factor(airquality$Month,
labels = c("May", "Jun", "Jul", "Aug", "Sep"))
airquality$ID <-seq(1:nrow(airquality))
points <- c(11, 17)
airquality$Points <- NA
airquality$Points[points] <- c("Point a", "Point b")
plt <- ggplot(airquality, aes(x = Month, y = Ozone, color = Month)) +
geom_boxplot(outlier.shape = NA) +
geom_point(data = airquality[!is.na(airquality$Points), ],
mapping=aes(x = Month, y = Ozone, fill = Points), size = 3, shape = 21, inherit.aes=FALSE) +
theme(legend.position = "bottom")
plt
像这样绘制图形的最简单方法是:
- 合并
dataPoint
个数据集(使用 rbind
)。这样你只需要调用一个 geom_point
- 对于箱线图,使用填充而不是颜色
- 使用
scale_color_manual
定义点颜色
代码:
# Combine datasets
dataPoints <- rbind(dataPoint, dataPoint2)
# Plot
ggplot(airquality, aes(Month, Ozone, fill = Month)) +
geom_boxplot(outlier.shape = NA) +
geom_point(data = dataPoints,
aes(Month, Ozone, color = factor(ID)),
size = 3) +
labs(color = "ID",
fill = "Month") +
scale_color_manual(values = c("darkblue", "darkred")) +
theme(legend.position = "bottom")
结果:
PS:我不会为月份添加调色板(填充),因为此信息已经显示在 x 轴上(冗余信息)。要删除填充图例,您可以添加 guides(fill = FALSE)
.
在 OP 评论后编辑以使用形状:
如果你想要形状而不是颜色
ggplot(airquality, aes(Month, Ozone, fill = Month)) +
geom_boxplot(outlier.shape = NA) +
geom_point(data = dataPoints,
aes(Month, Ozone, shape = factor(ID)),
size = 3) +
labs(shape = "ID",
fill = "Month") +
scale_shape_manual(values = c(15, 17)) +
theme(legend.position = "bottom")
我有这个箱线图,然后我试图在它上面绘制 2 个不同的点,颜色不同,以便用户可以看到它们落在箱线图上的位置,并将它们相互比较。问题是,我希望图例显示具有相应颜色的两个点及其 ID。相反,图例显示的是箱线图 categories/colors。我如何覆盖图例以仅显示我想要的内容?这是我的代码:
library(datasets)
library(ggplot2)
data(airquality)
airquality$Month <- factor(airquality$Month,
labels = c("May", "Jun", "Jul", "Aug", "Sep"))
airquality$ID <-seq(1:nrow(airquality))
dataPoint <- airquality[11,]
dataPoint2 <- airquality[17,]
plt <- ggplot(airquality, aes(x = Month, y = Ozone, color = Month)) +
geom_boxplot(show.legend=TRUE,outlier.shape = NA) +
geom_point(data = dataPoint, color='darkblue', aes(x = Month, y = Ozone), size = 3,show.legend=TRUE) +
geom_point(data = dataPoint2, color='darkred', aes(x = Month, y = Ozone), size = 3,show.legend=TRUE) +
theme(legend.position = "bottom")
plt
我会通过将这些点映射到不同的美学来实现这一点。如果在这种情况下 show.legend
设置为 FALSE
,则图例将单独显示。您还可以映射到点形状或任何其他美学。或者,您可以映射箱线图 geom 的 fill
,并映射点 geom 的 color
。
例如:
library(datasets)
library(ggplot2)
data(airquality)
airquality$Month <- factor(airquality$Month,
labels = c("May", "Jun", "Jul", "Aug", "Sep"))
airquality$ID <-seq(1:nrow(airquality))
points <- c(11, 17)
airquality$Points <- NA
airquality$Points[points] <- c("Point a", "Point b")
plt <- ggplot(airquality, aes(x = Month, y = Ozone, color = Month)) +
geom_boxplot(outlier.shape = NA) +
geom_point(data = airquality[!is.na(airquality$Points), ],
mapping=aes(x = Month, y = Ozone, fill = Points), size = 3, shape = 21, inherit.aes=FALSE) +
theme(legend.position = "bottom")
plt
像这样绘制图形的最简单方法是:
- 合并
dataPoint
个数据集(使用rbind
)。这样你只需要调用一个geom_point
- 对于箱线图,使用填充而不是颜色
- 使用
scale_color_manual
定义点颜色
代码:
# Combine datasets
dataPoints <- rbind(dataPoint, dataPoint2)
# Plot
ggplot(airquality, aes(Month, Ozone, fill = Month)) +
geom_boxplot(outlier.shape = NA) +
geom_point(data = dataPoints,
aes(Month, Ozone, color = factor(ID)),
size = 3) +
labs(color = "ID",
fill = "Month") +
scale_color_manual(values = c("darkblue", "darkred")) +
theme(legend.position = "bottom")
结果:
PS:我不会为月份添加调色板(填充),因为此信息已经显示在 x 轴上(冗余信息)。要删除填充图例,您可以添加 guides(fill = FALSE)
.
在 OP 评论后编辑以使用形状:
如果你想要形状而不是颜色
ggplot(airquality, aes(Month, Ozone, fill = Month)) +
geom_boxplot(outlier.shape = NA) +
geom_point(data = dataPoints,
aes(Month, Ozone, shape = factor(ID)),
size = 3) +
labs(shape = "ID",
fill = "Month") +
scale_shape_manual(values = c(15, 17)) +
theme(legend.position = "bottom")