Follow

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use
Contact

Adding legends to 3 plots in ggplot2

For a sample dataframe:

date = c("2021-2022", "2020-2021", "2019-2020", "2018-2019", "2017-2018", "2016-2017", "2015-2016", "2014-2015", "2013-2014", "2012-2013", "2011-2012")
cats = c(489701, 502743, 510776, 515904, 477746, 495208, 508466, 475334, 483368, 462693, 429065)
dogs = c(124046, 129445,132384, 140041, 68535, 64516, 63950, 60076, 59650, 59441, 56194)
pigs = c(91651, 85101, 78457, 63734, 45592, 43392, 40970, 38530, 37182, 36256, 34464)

df1 <- data.frame(date, cats, dogs, pigs)

I am trying to bar graph the cats data, and line graph the dogs and pig data.

So far I have:

MEDevel.com: Open-source for Healthcare and Education

Collecting and validating open-source software for healthcare, education, enterprise, development, medical imaging, medical records, and digital pathology.

Visit Medevel

p1<-ggplot(data=df1, aes(x=date, y=cats, colour = "cats")) +
  geom_bar(stat="identity") +
  theme(axis.text.x = element_text(angle = 60, vjust = 0.5)) +
  scale_y_continuous(labels = comma, 
                     limits=c(0,550000), 
                     name = "number") +
  labs(x = "Year") +
  geom_line(data = df1, aes(x=date, y=dogs), color = 'red', size = 0.75, group = 1) +
  geom_point(data = df1, aes(x=date, y=dogs), color = 'red', size = 1.5) +
  geom_line(data = df1, aes(x=date, y=pigs), color = 'blue', size = 0.75, , group = 1, linetype = "dashed") +
  geom_point(data = df1, aes(x=date, y=pigs), color = 'blue', size = 1.5)

p1

I am struggling to get a legend for the dogs and pigs line. I understand this is related to not having an appropriate aes characteristic – but how do I do this if I am already using color to set the line colour? (FYI – I don’t want the red outline but can’t seem to find what’s causing that).

I understand that part of my trouble is an untidy dataset, but I also had trouble converting it to a long dataset:

df2 <- pivot_longer(date, names_to = "animals", values_to = "count")
Error in UseMethod("pivot_longer") : 
  no applicable method for 'pivot_longer' applied to an object of class "character"

>Solution :

The issue is that to get a legend you have to map on aesthetics, i.e. instead of setting the line colors as arguments map on the color aes inside aes() and set your desired colors via scale_color_manual:

library(ggplot2)
library(scales)

ggplot(data = df1, aes(x = date)) +
  geom_col(aes(y = cats, fill = "cats")) +
  theme(axis.text.x = element_text(angle = 60, vjust = 0.5)) +
  scale_y_continuous(
    labels = comma,
    limits = c(0, 550000),
    name = "number"
  ) +
  labs(x = "Year") +
  geom_line(aes(y = dogs, color = "dogs"),
    size = 0.75, group = 1
  ) +
  geom_point(aes(y = dogs, color = "dogs"), size = 1.5) +
  geom_line(aes(y = pigs, color = "pigs"),
    size = 0.75, group = 1,
    linetype = "dashed"
  ) +
  geom_point(aes(y = pigs, color = "pigs"), size = 1.5) +
  scale_color_manual(values = c(pigs = "blue", dogs = "red"))

enter image description here

Add a comment

Leave a Reply

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use

Discover more from Dev solutions

Subscribe now to keep reading and get access to the full archive.

Continue reading