ggplot:显示具有多层的自定义图例
ggplot: showing custom legend with multiple layers
我有一个带有几层条形图和点的 ggplot。我不知道如何正确显示每一层的图例。这是代码:
country <- c(rep(c("AA"), 4), rep(c("BB"), 4))
score <- c(1.3, 7.2, 4.9, 7.5, 6.8, 4.1, 4.9, 5.8)
y_axis <- c(rep(c("Score 2019", "Score 2020", "Group median", "Cluster average"), 2))
dt <- data.table(country, score, y_axis)
ggplot(, aes(country, score)) +
theme_bw() +
geom_bar(data=dt[y_axis=="Score 2019"], stat="identity", fill="grey", width=0.5) +
geom_point(data=dt[y_axis=="Score 2020"], aes(shape="Score 2020"), color="green4", size=2) +
geom_point(data=dt[y_axis=="Cluster average"], aes(shape="Cluster average"), color="orange", size=2) +
geom_hline(yintercept=unique(dt[y_axis=="Group median"][,score]), size=1, color="red3")
现在我只能在图例中显示“Cluster average”和“Score 2020”(颜色错误)。如何正确显示图例中的所有图层(分数 2019 和 2020,中位数和平均值)?
为了正确显示点的图例,您需要将颜色 和 形状映射到美学。将 hline
分开并将其映射到不同的 non-consequential 美学,例如 linetype
以使其出现在图例中。
ggplot(, aes(country, score)) +
theme_bw(base_size = 20) +
geom_col(data = dt[y_axis == "Score 2019"],
aes(fill = "Score"), width = 0.5) +
geom_point(data = dt[y_axis == "Score 2020"],
aes(shape = "Score 2020", color = "Score 2020"), size = 4) +
geom_point(data = dt[y_axis == "Cluster average"], size = 4,
aes(shape="Cluster average", color = "Cluster average")) +
geom_hline(data = dt[y_axis == "Group median"],
aes(yintercept = score, linetype = "Group median"),
color = "red4", size = 1) +
scale_shape_manual(values = c("Cluster average" = 16,
"Score 2020" = 17), name = "Legend") +
scale_color_manual(values = c("Cluster average" = 'orange',
"Score 2020" = "green4"), name = "Legend") +
scale_fill_manual(values = "gray") +
labs(linetype = "") +
theme(legend.title = element_blank(),
legend.spacing.y = unit(0, "mm"),
legend.margin = margin(0, 0, 0, 0))
我有一个带有几层条形图和点的 ggplot。我不知道如何正确显示每一层的图例。这是代码:
country <- c(rep(c("AA"), 4), rep(c("BB"), 4))
score <- c(1.3, 7.2, 4.9, 7.5, 6.8, 4.1, 4.9, 5.8)
y_axis <- c(rep(c("Score 2019", "Score 2020", "Group median", "Cluster average"), 2))
dt <- data.table(country, score, y_axis)
ggplot(, aes(country, score)) +
theme_bw() +
geom_bar(data=dt[y_axis=="Score 2019"], stat="identity", fill="grey", width=0.5) +
geom_point(data=dt[y_axis=="Score 2020"], aes(shape="Score 2020"), color="green4", size=2) +
geom_point(data=dt[y_axis=="Cluster average"], aes(shape="Cluster average"), color="orange", size=2) +
geom_hline(yintercept=unique(dt[y_axis=="Group median"][,score]), size=1, color="red3")
现在我只能在图例中显示“Cluster average”和“Score 2020”(颜色错误)。如何正确显示图例中的所有图层(分数 2019 和 2020,中位数和平均值)?
为了正确显示点的图例,您需要将颜色 和 形状映射到美学。将 hline
分开并将其映射到不同的 non-consequential 美学,例如 linetype
以使其出现在图例中。
ggplot(, aes(country, score)) +
theme_bw(base_size = 20) +
geom_col(data = dt[y_axis == "Score 2019"],
aes(fill = "Score"), width = 0.5) +
geom_point(data = dt[y_axis == "Score 2020"],
aes(shape = "Score 2020", color = "Score 2020"), size = 4) +
geom_point(data = dt[y_axis == "Cluster average"], size = 4,
aes(shape="Cluster average", color = "Cluster average")) +
geom_hline(data = dt[y_axis == "Group median"],
aes(yintercept = score, linetype = "Group median"),
color = "red4", size = 1) +
scale_shape_manual(values = c("Cluster average" = 16,
"Score 2020" = 17), name = "Legend") +
scale_color_manual(values = c("Cluster average" = 'orange',
"Score 2020" = "green4"), name = "Legend") +
scale_fill_manual(values = "gray") +
labs(linetype = "") +
theme(legend.title = element_blank(),
legend.spacing.y = unit(0, "mm"),
legend.margin = margin(0, 0, 0, 0))