Interactive labels in R pie() charts
Exercise 6.5.1.
Statistics for Ecologists (Edition 2) Exercise 6.5.1
These notes relate to Chapter 6, exploring data using graphs. They are especially relevant to Section 6.5.1, which is about using pie charts to show association data. However, the notes are generally relevant as they show how you can place text onto an existing R plot in an interactive manner, using your mouse as a pointer.
Interactive labels in R pie() charts
Introduction
The locator() command is used to “read” the mouse position and generate x, y co-ordinates. These can be used in various ways, in commands that require those x, y co-ordinates. For example, sometimes the default placement of labels on a plot is not quite what you want. You can use the text() command with locator() to place the labels exactly where you want.
In this exercise you’ll see the locator() command used to place labels on a pie() chart. You can download the example data file birds.RData and use the data to follow along.
A basic pie() chart
Here are some data on bird species and habitat selection:
birds Garden Hedgerow Parkland Pasture Woodland Blackbird 47 10 40 2 2 Chaffinch 19 3 5 0 2 Great Tit 50 0 10 7 0 House Sparrow 46 16 8 4 0 Robin 9 3 0 0 2 Song Thrush 4 0 6 0 0
The data are in matrix form, which you can see using the class() command:
class(birds) [1] "matrix"
We’ll plot a pie() chart of the 2nd row:
pie(birds[2,])
The resulting plot uses the names attribute for the labels (here the colnames of the original matrix).
Simple locator() placement
The locator() command accepts x, y co-ordinates from a mouse click. You can use the locator() command in text() to place labels where you click the mouse. You need to state in locator() how many “clicks” you want.
In the current example there are 5 categories (one has a value of zero), so we want 5 labels.
First of all, you need to suppress the default labels. Each plotting command has a slightly different way of doing this, in the pie() command you use labels = “”.
pie(birds[2,], labels = "")
Now you can add the labels separately. There are 5 categories so you’ll need locator(5) in this example.
text(locator(5), colnames(birds))
Note that the labels are centred over the spot you click, and they are not displayed until the command is finished.
In this pie() chart the labels have been placed with the aid of the locator() command.
It can be a bit tricky to get the placement exactly how you want but there are some additional tools to help you.
Alignment with the text() command
The text() command allows you to tweak the position of the text, relative to the co-ordinates. The pos parameter allows you to specify an integer (or vector of integers) which align the text like so:
- pos = 1 text is placed below the point (centred).
- pos = 2 text is placed to the left of the point (the last character next to the point).
- pos = 3 text is placed above the point (centred).
- pos = 4 text is placed to the right of the point (the first character next to the point).
In our example the labels need a vector of pos values, as the 2nd is best aligned by the final character and the 4th by the first. The following commands should do the job:
pie(birds[2,], labels = "") # no labels text(locator(5), colnames(birds), pos = c(1, 2, 1, 4, 1))
To place the labels, you need to click in the plot. The first click will place the “Garden” label, which will be centred just below where you click. The second click will align the last character of the “Hedgerow” label to the left of where you click. The third click is below (centred). The fourth click places the “Pasture” label aligned with the first character to the right of the click-point. The final (fifth) click will be centred just below the click-point.
Custom labels
You can use any label you like by specifying the text explicitly. In a pie() chart you’ll generally want the category label and the frequency or percentage. You can use the prop.table() command to get the proportions (and therefore percentages). The paste() command is useful to join things together to make custom labels.
Use prop.table() to get row or column proportions (margin = 1 for rows, margin = 2 for columns):
prop.table(birds, margin = 1) Garden Hedgerow Parkland Pasture Woodland Blackbird 0.4653465 0.0990099 0.3960396 0.01980198 0.01980198 Chaffinch 0.6551724 0.1034483 0.1724138 0.00000000 0.06896552 Great Tit 0.7462687 0.0000000 0.1492537 0.10447761 0.00000000 House Sparrow 0.6216216 0.2162162 0.1081081 0.05405405 0.00000000 Robin 0.6428571 0.2142857 0.0000000 0.00000000 0.14285714 Song Thrush 0.4000000 0.0000000 0.6000000 0.00000000 0.00000000
Use the round() command to display fewer decimal places and let’s x100 to get percentage values:
round(prop.table(birds, margin = 1)*100,1) Garden Hedgerow Parkland Pasture Woodland Blackbird 46.5 9.9 39.6 2.0 2.0 Chaffinch 65.5 10.3 17.2 0.0 6.9 Great Tit 74.6 0.0 14.9 10.4 0.0 House Sparrow 62.2 21.6 10.8 5.4 0.0 Robin 64.3 21.4 0.0 0.0 14.3 Song Thrush 40.0 0.0 60.0 0.0 0.0
Since we are only plotting the 2nd row let’s display only the 2nd row data:
round(prop.table(birds, margin = 1)*100,1)[2,] Garden Hedgerow Parkland Pasture Woodland 65.5 10.3 17.2 0.0 6.9
Now you can use the paste() command to make a custom label containing the name and percentage:
pie.perc <- round(prop.table(birds, margin = 1)*100,1) pie.labels <- paste(colnames(pie.perc), ", ", pie.perc[2,], "%", sep = "")
The paste() command combines items, which you separate with commas. The sep parameter determines which character (if any) is used to separate the items (none in this case. A comma or space would not be appropriate as we want the % to be adjacent to the percentage value).
Look at the labels you made:
pie.labels [1] "Garden, 65.5%" "Hedgerow, 10.3%" "Parkland, 17.2%" "Pasture, 0%" [5] "Woodland, 6.9%"
You can now use the locator() command as before but specifying your new custom labels.
Making room for labels
Sometimes the labels are simply too large to fit. You may be able to make room by shrinking the text with the cex parameter:
pie(birds[2,], labels = "") text(locator(5), pie.labels, pos = c(1, 2, 1, 4, 1), cex = 0.8)
The resulting pie() chart resembles the following:
You can make custom labels and use the locator() command to help place them.
In this case the cex parameter was used to shrink the text, allowing it to fit better. This does not always work out and you may need to shrink the pie in its frame. To do this you use the radius parameter (default: radius = 0.8), in the pie() command.
Labels over multiple lines
Another way to make labels fit better is to split them over more than one line. In our current example the percentage value could be placed below the category labels.
The “\n” text is treated as a “newline” character. So, if you use “\n” in your paste() command you can make a custom label spread over more than one line.
pie.perc <- round(prop.table(birds, margin = 1)*100,1) pie.labels <- paste(colnames(pie.perc), "\n", pie.perc[2,], "%", sep = "")
This is what they look like in the console:
pie.labels [1] "Garden\n65.5%" "Hedgerow\n10.3%" "Parkland\n17.2%" "Pasture\n0%" [5] "Woodland\n6.9%"
Now you proceed as before and place the labels:
pie(birds[2,], labels = “”) text(locator(5), pie.labels, pos = c(1, 2, 1, 4, 1), cex = 0.8)
The resulting pie() chart looks like this:
Placing “\n” in a custom label acts as a newline character.
This gives you another way to display the labels.
Leading lines
If you have labels around the outside of your pie() chart you may want to add leader lines to “join” the label to the appropriate segment. The locator() command can do this by itself, but you’ll have to create one line at a time.
You’ll need to specify two co-ordinates; the start and end point of the line.
locator(2, type = "l")
Note that you need type = “l” (that’s a lower case L not a number 1). Then click the plot at the start and end points. You can use additional graphics parameters to alter the appearance of the line, examples might be col (colour), lwd (line width), lty (line type).
Comments are closed.