I have a ggplot with several layers of bars and points. I could not figure out how to correctly show the legend for each layer. Here is the code:
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")
Right now I only manage to have "Cluster average" and "Score 2020" to show up in the legend (with wrong colors). How can I correctly display all layers in the legend (score 2019 and 2020, median and average)?
CodePudding user response:
For the legend to appear for the points correctly, you need to map the colors and shapes to aesthetics. Keep the hline
separate and map it to a different, non-consequential aesthetic such as linetype
to get it to appear on the legend.
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))