Here is an answer based on the tidyverse package. Where can I use pipe, %>% to bind functions together. The plot creation continues in one way, eliminating the need for temporary variables. You can read more about pipe in this post. What does the% & gt;% function in R mean?
As far as I know, legends in ggplot2 are based only on aesthetic variables. Thus, to add a discrete legend, use the category column and change the aesthetics to suit the category. In ggplot, this is done, for example, aes(color=category) .
Thus, to add two (or more) different data frame variables to the legends, you need to convert the data frame so that we have a category column telling us which column (variable) is displayed, and the second column that actually contains the Value. The tidyr::gather function, which was also loaded by tidyverse , does just that.
You can then create a legend by simply specifying which aesthetics variables should be different. In this example, the code will look like this:
source("http://www.openintro.org/stat/data/arbuthnot.R") library(tidyverse) arbuthnot %>% rename(Year=year,Men=boys,Women=girls) %>% gather(Men,Women,key = "Sex",value = "Rate") %>% ggplot() + geom_point(aes(x = Year, y=Rate, color=Sex, shape=Sex)) + scale_color_manual(values = c("Men" = "#3399ff","Women"= "#ff00ff")) + scale_shape_manual(values = c("Men" = 16, "Women" = 17))
Note that the tidyverse package tidyverse also automatically loaded into the ggplot2 package. An overview of installed packages can be found on their website tidyverse.org .
In the above code, I also used the dplyr::rename function (also loaded by tidyverse ) to first rename the columns to the desired labels. Because the legend automatically accepts labels equal to category names.
There is a second way to rename legend labels, which involves explicitly specifying labels in the scale_aesthetic_manual functions using the labels = argument. See examples in the cookbook of legends . But not recommended, as it quickly gets confused with a lot of variables.