Adding a Legend in ggplot2Ryan NagelkirkOctober 6, 2015Description: Adding a legend in ggplot is surprisingly difficult, with counterintuitive settings. Here is how you can plot two lines from the same dataframe and label them in a legend. First, load ggplot2 and create a dataframe.
Plot the data. Notice that “colour”, when used within aesthetics (aes()), creates the data title. If you use “colour” outside of aes() and within one of the geom_ settings, “colour” sets the color of the line. Don’t use “colour” for setting the color. Instead, use scale_color_manual() to add the legend and manually set the colors. This should give you a legend with the correct labels and colors.
Comments are closed.
|
Spatial Ecology @ MSUClick on "Category" below to search for R code compiled by the Zarnetske Spatial & Community Ecology Lab and students in MSU's Spatial Ecology graduate course (FOR870/FW870) Category
All
Archive
October 2016
|