9. Adding legends

The function legend() is all you need to set up the legend in your chart. It can contain a lot of arguments due to the amount of details such a field may require… The essential arguments are location, title, bg (for background color), cex (for size), text.col (for text color…), legend (a text vector with the labels of the data series). For the location, use parameters such as “bottom”, “bottomleft”, “left”, “topleft”, “top”, “topright”, “right”, “bottomright”, or “center” to place the legend correctly. You might need to add inset= followed by a number to push the legend away from the borders and into the graph; the number to use corresponds to a fraction of the plot region. Here is an example:

[code language=”r”]
plot(z~x, pch=10, col="red")
legend("topleft", inset=0.05, title="Legend", pch=10,
col="red", bg="lightgrey", cex=1, text.col="navyblue",



  Fant du det du lette etter? Did you find this helpful?
[Average: 5]