I have this dataframe that I made. The columnnames are the years. Let's say you have this dataframe, named fruits:
apples | bananas | apples
2012 | 23 | 32 | 23
2013 | 26 | 29 | 23
2014 | 32 | 43 | 25
2015 | 23 | 43 | 26
2016 | 21 | 46 | 22
How do you make a line graph out of this? I assume the best way is to use ggplot. I used the following code:
years = seq(from = 2012, to = 2016)
ggplot(data = fruits, mapping = aes(x = years)) +
geom_line(aes(y = apples), color = "green")+
geom_line(aes(y = bananas), color = "yellow")+
geom_line(aes(y = apples), color = "indianred")
This gives me a plot without a legend. When I search on the internet, I only find examples of graphs that give a legend right away. Is something wrong with my code? And is there a faster way or a way that isn't as hardcoded to have multiple lines in a graph as my way?