Adding a legend to GGPlot Lollipop Chart Based on Colour

Viewed 386

I have created a lollipop chart with the following code. I am trying to add a legend to the chart, detailing the orange colour as "2020" and the purple colour as "Average". How would I do so?

ggplot(LGA_top3) +
  geom_segment(aes(x = LGA, xend = LGA, y = 2020, yend = Average)) +
  geom_point(aes(x = LGA, y = 2020), color="orange", size=3) +
  geom_point(aes(x = LGA, y = Average), color="purple", size=3) +
  coord_flip() +
  theme(legend.position="right") +
  facet_wrap(~`Offence Category`, ncol=1, scale="free_y") +
  labs(x = "Local Government Area (LGA)", y = "Total")

enter image description here

1 Answers

I would recomment something like this. It is easier if you provide a reproducible example, with your current code there is no way to give you a more accurate answer. Plea use dput(your_data) and paste re result here.

ggplot(LGA_top3) +
  geom_segment(aes(x = LGA, xend = LGA, y = 2020, yend = Average)) +
  geom_point(aes(x = LGA, y = 2020, color = "2020"), size=3) +
  geom_point(aes(x = LGA, y = Average, color = 'Average'), size=3) +
  coord_flip() +
  scale_color_manual(values = c('orange', 'purple')) +
  theme(legend.position="right") +
  facet_wrap(~`Offence Category`, ncol=1, scale="free_y") +
  labs(x = "Local Government Area (LGA)", y = "Total")
Related