简体   繁体   中英

Adding a legend to a plot with two data frames using ggplot2 (r)

I'm trying to plot the median heart rate of boys & girls per minute in a PE lesson to see if there are some gender differences. I'd like to plot 2 line graphs using ggplot2 in two different colours representing the gender. My problem is, I'm using two different data frames and I'm not able to add a legend that shows "green = boys, red = girls".

Here are my data frames: wDaten2809HR & mDaten2809HR.

That's the current code for my plot which does not show any legend:

ggplot(NULL, aes(x= minute, y= meanHR))+
  geom_line(data= wDaten2809HR, color= "orangered2", size= 0.7)+
  geom_line(data= mDaten2809HR, color= "seagreen3", size= 0.7)+
  scale_x_continuous(expand= c(0, 0), limits = c(0,35), breaks= c
                     (0, 5, 10, 15, 20, 25, 30, 35), name= "Zeit [min]")+
  scale_y_continuous(breaks = c(70, 90, 110, 130, 150, 170, 190, 210), limits= c(70, 210), name = "Herzrate [BPM]")+
  theme_grey()+
  labs(title = "Geschlechtervergleich - durchschnittliche Herzrate",
       subtitle = "28.09.2021")+
  theme(plot.title = element_text(size = 12, color = "black", hjust = 0.5),
        plot.subtitle = element_text(size = 10, color = "grey50", hjust = 0.5), legend.position = "bottom")

I can see the plot showing the two graphs but no legend due to the two data frames. I've tried a lot different things that were explained on coding websites to manually add a legend but I'm lost, nothing works. Can anyone help, please?

I think it's easiest if you do some data manipulation first in order to combine the two data frames. You can use mutate() in this case. Then you can specify that the colour of the lines should represent the gender column.

library(tidyverse)

df_boys <- data.frame(
  time = c(1:10),
  heart_rate = c(100:109)
)
df_girls <- data.frame(
  time = c(1:10),
  heart_rate = c(105:114)
)

df <- bind_rows(
  df_boys %>% mutate(gender = "M"),
  df_girls %>% mutate(gender="F")
)


ggplot(df, aes(x=time, y=heart_rate, group=gender)) + 
  geom_line(aes(colour=gender))

Created on 2022-05-16 by the reprex package (v2.0.1)

This solution intents to create manually the legend specifiyng inside aes() of geom_line() a legend id and then refering them in scale_colour_manual()

ggplot(NULL, aes(x= minute, y= meanHR))+
  geom_line(data= wDaten2809HR, 
            aes(color = "woman"),
            size= 0.7)+
  geom_line(data= mDaten2809HR, 
            aes(color = "man"),
            size= 0.7)+
  scale_x_continuous(expand= c(0, 0), 
                     limits = c(0,35), 
                     breaks= c(0, 5, 10, 15, 20, 25, 30, 35), name= "Zeit [min]")+
  scale_y_continuous(breaks = c(70, 90, 110, 130, 150, 170, 190, 210),
                     limits= c(70, 210),
                     name = "Herzrate [BPM]")+
  scale_color_manual(name='Legend name',
                     breaks=c('woman', 'man'),
                     values=c('woman'='orangered2', 
                              'man'='seagreen3')) +
  theme_grey()+
  labs(title = "Geschlechtervergleich - durchschnittliche Herzrate",
       subtitle = "28.09.2021")+
  theme(plot.title = element_text(size = 12,
                                  color = "black", 
                                  hjust = 0.5),
        plot.subtitle = element_text(size = 10, 
                                     color = "grey50", 
                                     hjust = 0.5), 
        legend.position = "bottom")

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM