Home > Software engineering >  Legend does not appear in ggplot
Legend does not appear in ggplot

Time:04-02

Is it possible to add a legend to this ggplot graph? Ideally the legend will list the different age ranges that are specified in my model below.

enter image description here

Here is my code:

df.year <- as.data.frame(2010:2032) 
  colnames(df.year) <- "Year"

ggplot(data = df.year, aes(x = Year))  
  geom_line(data = cdc.fert.train, aes(y = `15-17 years`), color = "blue")  
  geom_line(data = cdc.fert.train, aes(y = `18-19 years`), color = "purple")  
  geom_line(data = cdc.fert.train, aes(y = `20–24 years`), color = "black")   
  geom_line(data = cdc.fert.train, aes(y = `25–29 years`), color = "orange")   
  geom_line(data = cdc.fert.train, aes(y = `30–34 years`), color = "red")   
    geom_line(data = cdc.fert.train, aes(y = `35–39 years`), color = "pink")   
    geom_line(data = cdc.fert.train, aes(y = `40–44 years`), color = "green")

Here is my data:

> dput(cdc.fert.train)
structure(list(Year = c(2010, 2011, 2012, 2013, 2014, 2015, 2016, 
2017, 2018), `Crude Birth Rate` = c(13, 12.7, 12.6, 12.4, 12.5, 
12.4, 12.2, 11.8, 11.6), `15-17 years` = c(17.3, 15.4, 14.1, 
12.3, 10.9, 9.9, 8.8, 7.9, 7.2), `18-19 years` = c(58.2, 54.1, 
51.4, 47.1, 43.8, 40.7, 37.5, 35.1, 32.3), `20–24 years` = c(90, 
85.3, 83.1, 80.7, 79, 76.8, 73.8, 71, 68), `25–29 years` = c(108.3, 
107.2, 106.5, 105.5, 105.8, 104.3, 102.1, 98, 95.3), `30–34 years` = c(96.5, 
96.5, 97.3, 98, 100.8, 101.5, 102.7, 100.3, 99.7), `35–39 years` = c(45.9, 
47.2, 48.3, 49.3, 51, 51.8, 52.7, 52.3, 52.6), `40–44 years` = c(10.2, 
10.3, 10.4, 10.4, 10.6, 11, 11.4, 11.6, 11.8), `45-54 years` = c(0.7, 
0.7, 0.7, 0.8, 0.8, 0.8, 0.9, 0.9, 0.9)), row.names = c(NA, -9L
), class = c("tbl_df", "tbl", "data.frame"))

CodePudding user response:

For the left side of the plot (you only provided data for that side), you can do the following:

  1. pivot the data long
  2. create a named list of colors
  3. use scale_color_manual
fertility_long = cdc.fert.train %>% 
  pivot_longer(cols = 3:10,names_to = "AgeGroup")

colors = c("blue","purple","black","orange","red","pink","green", "white")
names(colors) = unique(fertility_long$AgeGroup)

ggplot(fertility_long %>% filter(AgeGroup!="45-54 years"),aes(Year,value, color=AgeGroup))   
  geom_line(size=1.4) 
  scale_color_manual(values=colors)   
  theme(legend.position = "bottom")

CodePudding user response:

If you transform your data to 'long' format it is easier to plot, i.e.

library(tidyverse)

cdc.fert.train <- structure(list(Year = c(2010, 2011, 2012, 2013, 2014, 2015, 2016, 
                              2017, 2018), `Crude Birth Rate` = c(13, 12.7, 12.6, 12.4, 12.5, 
                                                                  12.4, 12.2, 11.8, 11.6), `15-17 years` = c(17.3, 15.4, 14.1, 
                                                                                                             12.3, 10.9, 9.9, 8.8, 7.9, 7.2), `18-19 years` = c(58.2, 54.1, 
                                                                                                                                                                51.4, 47.1, 43.8, 40.7, 37.5, 35.1, 32.3), `20–24 years` = c(90, 
                                                                                                                                                                                                                             85.3, 83.1, 80.7, 79, 76.8, 73.8, 71, 68), `25–29 years` = c(108.3, 
                                                                                                                                                                                                                                                                                          107.2, 106.5, 105.5, 105.8, 104.3, 102.1, 98, 95.3), `30–34 years` = c(96.5, 
                                                                                                                                                                                                                                                                                                                                                                 96.5, 97.3, 98, 100.8, 101.5, 102.7, 100.3, 99.7), `35–39 years` = c(45.9, 
                                                                                                                                                                                                                                                                                                                                                                                                                                      47.2, 48.3, 49.3, 51, 51.8, 52.7, 52.3, 52.6), `40–44 years` = c(10.2, 
                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                       10.3, 10.4, 10.4, 10.6, 11, 11.4, 11.6, 11.8), `45-54 years` = c(0.7, 
                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                        0.7, 0.7, 0.8, 0.8, 0.8, 0.9, 0.9, 0.9)), row.names = c(NA, -9L
                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                        ), class = c("tbl_df", "tbl", "data.frame"))
df.year <- as.data.frame(2010:2032) 
colnames(df.year) <- "Year"

cdc.fert.train_longer <- cdc.fert.train %>%
  pivot_longer(-c(`Crude Birth Rate`, Year))

ggplot(cdc.fert.train_longer, aes(x = Year, y = value, color = name))  
  geom_line()

# To specify your own colors
ggplot(cdc.fert.train_longer, aes(x = Year, y = value, color = name))  
  geom_line()  
  scale_color_manual(values = c(`15-17 years` = "blue",
                                `18-19 years` = "purple",
                                `20–24 years` = "black",
                                `25–29 years` = "orange",
                                `30–34 years` = "red",
                                `35–39 years` = "pink",
                                `40–44 years` = "green"))

Created on 2022-04-02 by the reprex package (v2.0.1)

  • Related