Legends

As in lines() and points(), legend() is a function that needs to be called after the plot creation. legend() contains numerous arguments and most of them are optional.

The only mandatory arguments in legend() are x and legend, that is, the position and the texts that are passed to the legend. It is important to keep in mind that if y is not passed and a value is passed in x, this will be considered as a value in the vertical axis and the position with regard to the horizontal axis will be 1. If both the values (x and y) were specified, then the first will be the position in the horizontal axis and the second will be the position in the vertical axis.

The following two examples illustrate this difference. In the first one, as only one value is passed, this is interpreted as the value in the y axis. The height is specified by the vertical axis range, that is, the box's top of the legend will be drawn at the specified height. In the second one, two values are passed. In this case, the first value sets the position of the box's left-hand side while the second one determines the position of the top line of the box:

> plot(iris$Sepal.Length)
> legend(7.5,c("aaa","bbb"))

The legend on the top-left corner is as follows:

Legends

The legend on the bottom-right corner is as follows:

> plot(iris$Sepal.Length)
> legend(120,5.5,c("aaa","bbb"))

The output will be this:

Legends

As it will be observed, most of the time using only the default arguments to generate a legend is not enough; legends are intended to give useful information to the reader to interpret the plot. The clearest example could be when we want to plot two series. Back to the iris example, if we would like to do a scatterplot of Sepal.Length and Petal.Length by Species, we would need to differentiate the series by species in some way.

The easiest method would be by color. In this case, for example, a legend would be needed to clarify to which series each color belongs. Although this task is very easy to fulfill in classical commercial software, in R, it is a bit more complicated. Of course, in this case, we would need to specify inside the legend what color each species belongs to.

In the following example, three series have been plotted. As it was explained before, col could be a character vector, so a new variable is added to the iris dataset. Using switch() along with sapply(), a color is assigned to every observation according to its species. After this, the plot is drawn by passing iris$color in the col argument.

Finally, the legend is added. In this example, its values (mainly, the legends and the colors) are passed as static strings. Of course, this code could be written in other ways to avoid, for example, by passing hardcoded legend and col:

data(iris)

iris$color <- sapply(iris$Species, function(x) switch(as.character(x),
    setosa = "red",
    versicolor = "green",
    virginica = "blue"))

plot(iris$Sepal.Length,iris$Petal.Length, col= iris$color, pch = 16)

legend(7.2,3,legend=c("setosa","versicolor","virginica"), pch=16, col=c("red","green","blue"),cex=0.7)

This should be the output:

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

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