Search code examples
rggplot2plotlegendgeom

How to add a legend to ggplot in R?


I'm trying to add a legend to my graph which consists of two lines. Unfortunately, I don't understand how. Could you help me out, please? My dataframe can be found below. Thank you.

# dput(df)
structure(list(t = c(1, 2, 3, 4, 5, 6, 7, 8, 9, 10), Beta_Treatment = c(0.999550283587599, 
1.1284848818908, 1.3173797973744, 1.381630131904, 1.42558575749304, 
1.598454651784, 1.68182321476, 1.908913645904, 1.9830406566956, 
2.0704834391088), Beta_Control = c(0.822042496772398, 0.914170557749195, 
0.812776816705598, 0.829879870335997, 0.906381000106958, 0.891107911295998, 
0.885094437239998, 0.810487561695997, 0.816917509704399, 0.881900118891192
), treatment_stderr = c(0.0466078055173255, 0.0451188571313495, 
0.0473857585047991, 0.0465872903741648, 0.0436607368165115, 0.0479028140228299, 
0.0445418661900949, 0.0451947124622918, 0.0474264037378467, 0.0464878615840165
), control_stderr = c(0.0329566953375247, 0.0319038498369643, 
0.0335067911704116, 0.0329421889406788, 0.0308728030745565, 0.0338724046334612, 
0.03149585562972, 0.0319574876558628, 0.0335355316903225, 0.0328718821689198
)), class = "data.frame", row.names = c(NA, -10L))

enter image description here

#create and save graph as pdf
p1 <- ggplot(data=df, mapping=aes(y=Beta_Treatment, x=t)) +
  geom_line(linetype="dashed", colour = "blue") + 
  geom_point(shape = 15) + 
  geom_errorbar(aes(ymin=(Beta_Treatment-1.96*treatment_stderr), ymax=(Beta_Treatment+1.96*treatment_stderr)), width=1) + 
  ylim(c(0,2.5)) + labs(y = "Beta") +
  geom_vline(xintercept = 6) +
  scale_x_continuous(limits = c(0, 11), breaks = c(0, 2, 4, 6, 8, 10)) +
  theme_bw() 

ggsave(last_plot(),filename = "plot.pdf")
p2 <- p1 + geom_line(aes(y=Beta_Control, x=t),linetype="dashed", colour = "green")+ 
  geom_point(aes(y=Beta_Control, x=t), shape = 19) +
  geom_errorbar(aes(ymin=(Beta_Control-1.96*control_stderr), ymax=(Beta_Control+1.96*control_stderr )), width=1) +
  ylim(c(0,2.5)) +
  geom_vline(xintercept = 6) +
  theme_bw() 

ggsave(last_plot(),filename = "plot2.pdf")

Solution

  • If you want a legend then you have to map on aesthetics, i.e. move color inside aes() and you will automatically get a legend. However, to set your desired colors in that case requires to set them via scale_color_manual.

    To this end I map some "speaking" category labels on the color aes and assign colors and labels to these categories inside scale_color_manual. Additionally I used the limits argument to set the order of the categories:

    ggplot(data = df, mapping = aes(y = Beta_Treatment, x = t)) +
      geom_line(aes(colour = "treat"), linetype = "dashed") +
      geom_point(shape = 15) +
      geom_errorbar(aes(ymin = (Beta_Treatment - 1.96 * treatment_stderr), 
                        ymax = (Beta_Treatment + 1.96 * treatment_stderr)), width = 1) +
      ylim(c(0, 2.5)) +
      labs(y = "Beta", color = NULL) +
      geom_vline(xintercept = 6) +
      scale_x_continuous(limits = c(0, 11), breaks = c(0, 2, 4, 6, 8, 10)) +
      theme_bw() +
      geom_line(aes(y = Beta_Control, color = "control"), linetype = "dashed") +
      geom_point(aes(y = Beta_Control), shape = 19) +
      geom_errorbar(aes(ymin = (Beta_Control - 1.96 * control_stderr), 
                        ymax = (Beta_Control + 1.96 * control_stderr)), width = 1) +
      scale_color_manual(values = c(treat = "blue", control = "green"),
                         labels = c(treat = "Treatment", control = "Control"),
                         limits = c("treat", "control"))
    

    enter image description here