Share the Gallery !Share on FacebookShare on Google+Tweet about this on TwitterShare on LinkedInEmail this to someone

#119_Add_a_legend_to_a_plot

 

This page aims to explain how to add a legend to R plot made in base R. It is done using the legend() function. The main arguments are:

 

  • topright : where do you want to add the legend ? You can put : “bottomright”, “bottom”, “bottomleft”, “left”, “topleft”, “top”, “topright”, “right”, “center”).
  • legend = c(“name1”, “name2”) : names you want to show.
  • col = c(“red”, “blue”) : colors of the symbols
  • pch = 15 : type of symbols (see graph # to know what symbol number you need
  • bty = “n” : If you don’t want a box around the legend. Write “o” if you want one
  • pt.cex = 2 : Size of the symbols
  • cex = 0.8 : Size of the text
  • text.col = “black” : color of the text
  • horiz = TRUE : legend in column or in row ?
  • inset = c(0.1, 0.1) : % (from 0 to 1) to draw the legend away from x and y axis
  • You can also give the X and Y coordinate of the legend: legend(3, 5, legend = c(“A”, “B”))

Note that an equivalent page exist concerning legends with ggplot2.

 

 

Related


 

Make a search


 


Leave a Reply

1 Comment on "#119 Add a legend to a plot"

Notify of
avatar
Sort by:   newest | oldest | most voted
Moon
Guest

How to add standard error to this plot? Thank you!

wpDiscuz