Adding customized legends for multiple-line graphs

Line graphs with more than one line, representing more than one variable, are quite common in any kind of data analysis. In this recipe, we will learn how to create and customize legends for such graphs.

Getting ready

We will use the base graphics library for this recipe, so all you need to do is run the recipe at the R prompt. It is good practice to save your code as a script for use again later.

How to do it...

Once again we will use the cityrain.csv example dataset that we used in Chapter 1, R Graphics, and Chapter 3, Beyond the Basics – Adjusting Key Parameters:

rain<-read.csv("cityrain.csv")
plot(rain$Tokyo,type="b",lwd=2,
xaxt="n",ylim=c(0,300),col="black",
xlab="Month",ylab="Rainfall (mm)",
main="Monthly Rainfall in major cities")
axis(1,at=1:length(rain$Month),labels=rain$Month)
lines(rain$Berlin,col="red",type="b",lwd=2)
lines(rain$NewYork,col="orange",type="b",lwd=2)
lines(rain$London,col="purple",type="b",lwd=2)

legend("topright",legend=c("Tokyo","Berlin","New York","London"),
lty=1,lwd=2,pch=21,col=c("black","red","orange","purple"),
ncol=2,bty="n",cex=0.8,
text.col=c("black","red","orange","purple"),
inset=0.01)
How to do it...

How it works...

We used the legend() function that we have already come across in earlier chapters. It is quite a flexible function and allows us to adjust the placement and styling of the legend in many ways.

The first argument we passed to legend() specifies the position of the legend within the plot region. We used "topright". Other possible values are "bottomright", "bottom", "bottomleft", "left", "topleft", "top", "right", and "center". We can also specify the location of legend with the x and y coordinates, as we will soon see.

The other important arguments specific to lines are lwd and lty that specify the line width and type drawn in the legend box, respectively. It is important to keep these the same as the corresponding values in the plot() and lines() commands. We also set pch to 21 to replicate the type="b" argument in the plot() command. The cex and text.col parameters set the size and colors of the legend text, respectively. Note that we set the text colors to the same colors as the lines they represent. Setting bty (box type) to "n" ensures no box is drawn around the legend. This is good practice as it keeps the look of the graph clean. The ncol parameter sets the number of columns over which the legend labels are spread and inset sets the inset distance from the margins as a fraction of the plot region.

There's more...

Let's experiment by changing some of the arguments discussed:

legend(1,300,legend=c("Tokyo","Berlin","New York","London"),
lty=1,lwd=2,pch=21,col=c("black","red","orange","purple"),
horiz=TRUE,bty="n",bg="yellow",cex=1,
text.col=c("black","red","orange","purple"))
There's more...

This time we used the x and y coordinates instead of a keyword to position the legend. We also set the horiz argument to TRUE. As the name suggests, horiz makes the legend labels horizontal instead of the default vertical. Specifying horiz overrides the ncol argument. Finally, we made the legend text bigger by setting cex to 1 and did not use the inset argument.

An alternative way of creating the previous plot without have to call plot() and lines() multiple times is to use the matplot() function. To see details on how to use this function, see the help file by running ?matplot or help(matplot) on the R prompt.

See also

Have a look at the next recipe that shows a way to label lines directly instead of using a legend.

..................Content has been hidden....................

You can't read the all page of ebook, please click here login for view all page.
Reset
18.117.142.169