'ggplot: showing custom legend with multiple layers

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)?

enter image description here



Sources

This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.

Source: Stack Overflow

Solution Source