0

I'm trying to manipulate my legend in ggplot. My data looks like this: data

Using ggplot I plotted "time" on x axis and both "C16.conc." and "C18.conc" on y axis.

ggplot(df, aes(x=Time)) +                    
geom_point(aes(y=C16.conc), size=3, shape = 16, color="red") +
geom_smooth(aes(y=C16.conc), method = "lm", color= "red", linetype = "dashed", lwd=.8, se = FALSE) +
geom_point(aes(y=C18.conc), size=3, shape = 16, color="black") +  
geom_smooth(aes(y=C18.conc), method = "lm", color= "black", linetype = "dashed", lwd=.8, se = FALSE)

I can't figure out how to add a legend for this plot, the only command I know is to use

aes(fill=column name) 

but since I'm plotting data from two separate columns (C16.conc and C18.conc) I can't get the aes command above to make a legend. How should I add a legend with this data frame?

lmo
  • 37,904
  • 9
  • 56
  • 69
Jen
  • 331
  • 2
  • 11
  • 1
    "I'm plotting data from two separate columns" - don't. Gather, or melt, the columns into a single column. – Jack Brookes Mar 24 '18 at 20:53
  • Bravo Jack, Stepping aside from answer duplicates is the right thing to do (even if the gods of SO give you no credit for it), so I went and upvoted a couple of your other worthy answers. – IRTFM Mar 24 '18 at 22:50
  • 1
    Thank you @JackBrookes for referring me to the post! I'm still new to learning R and I'm struggling with finding the right key word to search for the right posts. – Jen Mar 25 '18 at 00:34
  • This is a very common issue with people learning ggplot2 - read about "tidy data" and it will be more clear. Good luck – Jack Brookes Mar 25 '18 at 00:37

0 Answers0