To add a legend to a plot in R using ggplot2
, you can use the scale_color_manual
or scale_fill_manual
functions. Here is an example code block that demonstrates how to use these functions to add a legend to a scatter plot:
main.r282 chars13 lines
In this example, the color
aesthetic is set to the string "Points", which will be used as the label for the legend. The scale_color_manual
function is then used to set the color of the points to blue and to set the legend title as "Legend Title".
You can also use scale_fill_manual
if you are using fill colors rather than point colors.
gistlibby LogSnag