Adding and formatting legends

In this recipe, we will learn how to add and format legends to graphs.

Getting ready

First, we need to load the cityrain.csv example data file, which contains monthly rainfall data for four major cities across the world (you can download this file from the code download section of the book's companion website):

rain<-read.csv("cityrain.csv",header=TRUE)

How to do it...

In the bar plots recipe, we already saw that we can add a legend by passing the legend argument to the barplot() function. Now, we see how we can use the legend() function to add and customize a legend for any type of graph.

Let's first draw a graph with multiple lines representing the rainfall in cities:

plot(rain$Tokyo,type="l",col="red",
ylim=c(0,300),
main="Monthly Rainfall in major cities",
xlab="Month of Year",
ylab="Rainfall (mm)",
lwd=2)
lines(rain$NewYork,type="l",col="blue",lwd=2)
lines(rain$London,type="l",col="green",lwd=2)
lines(rain$Berlin,type="l",col="orange",lwd=2)

Now, let's add the legend to mark which line represents which city:

legend("topright",
legend=c("Tokyo","NewYork","London","Berlin"),
col=c("red","blue","green","orange"),
lty=1,lwd=2)

How it works...

In the example code, we first created a graph with multiple lines using the plot() and lines() commands to represent the monthly rainfall in Tokyo, New York, London, and Berlin in four different colors. However, without a legend, one would have no way of telling which line represents which city. So, we added a legend using the legend() function.

The first argument to the legend() function is the position of the legend, which we set to topright. Other possible values are "topleft," "top," "left," "center," "right," "bottomleft," "bottom," and "bottomright." Then, we specify the legend labels by setting the legend argument to a vector of length 4, containing the names of the four cities. The col argument specifies the colors of the legend, which should match the colors of the lines in exactly the same order. Finally, the line type and width inside the legend are specified by lty and lwd, respectively.

There's more...

The placement and look of the legend can be modified in several ways. As a simple example, let's spread the legend across the top of the graph instead of the top-right corner. So first, let's redraw the same base plot:

plot(rain$Tokyo,type="l",col="red",
ylim=c(0,250),
main="Monthly Rainfall in major cities",
xlab="Month of Year",
ylab="Rainfall (mm)",
lwd=2)
lines(rain$NewYork,type="l",col="blue",lwd=2)
lines(rain$London,type="l",col="green",lwd=2)
lines(rain$Berlin,type="l",col="orange",lwd=2)

Now, let's add a modified legend:

legend("top",
legend=c("Tokyo","NewYork","London","Berlin"),
ncol=4,
cex=0.8,
bty="n",
col=c("red","blue","green","orange"),
lty=1,lwd=2)

We changed the legend location from topright to top and added a few other arguments to adjust the look. The ncol argument is used to specify the number of columns over which the legend is displayed. The default value is 1, as we saw in the first example. In our second example, we set ncol to 4 so that all the city names are displayed in one single row. The bty argument specifies the type of box drawn around the legend. We removed it from the graph by setting it to "n." We also modified the size of the legend labels by setting cex to 0.8.

See also

There are plenty of examples of how you can add and customize legends in different scenarios in later chapters of the book.