Dr. Mark Gardener


Statistics for Ecologists Edition 2 Cover
Available soon from
Pelagic Publishing

Statistics for Ecologists Using R and Excel (Edition 2)

Data Collection, Exploration, Analysis and Presentation

by: Mark Gardener

Available soon from Pelagic Publishing

Welcome to the support pages for Statistics for Ecologists. These pages provide information and support material for the book. You should be able to find an outline and table of contents as well as support datafiles and additional material.

Support Index | Exercises Index | Outline & TOC | Data files


Exercise 6.5.1

Pelagic Publishing Logo

Table of Contents

Section 6.5.1


Get exercise data here:



6.5.1 Interactive labels in pie() charts

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.


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.

The pie() command produces labels around the outslide.

To place labels in a different place in a plot you need the locator() command.

locator() allows you to pass co-ordinates to plotting commands interactively (using the mouse).


Example 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).

Pie chart with default labels
A pie() chart with default labels (taken from names attribute of data)

The default labels positions are okay; R tries to place then neatly around the edge. If you want the labels in some other position then you'll need to use the locator() command to place them using the mouse.

The locator() command reads clicks of the mouse in a plot as co-ordinates.

You can use locator() instead of x, y co-ordinates in any command.

Use locator() as part of a text() command for custom label placement in a plot.


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.

Basic pie() chart labels added with locator()
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.

Use pos parametrer in text() to alter text alignment relative to the co-ordinate.


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 (centered).
  • 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 centered 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 fisrt character to the right of the click-point. The fonal (fifth) click will be centred just below the click-point.

Use prop.table() to get proportions from a matrix:

margin = 1 for rows
margin = 2 for columns

Use round() to reduce displayed decimal places.

Use paste() to combine items and make custom labels.

Custom labels can be placed intercatively using locator() and the text() command.


Custom labels

You can use any label you like by specifying the text explicilty. 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 therfore 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 apropriate 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.

Use cex parameter in text() to make text smaller.

Use radius parameter in pie() to make pie smaller and allow more room for labels around the edge.


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:

Custom labels placed interactivdely
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.

Use "\n" as a newline character in a paste() command to split a label over more than one line.


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.

Start with your labels as before but use "\n" in the paste() command like so:

> 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:

A pie() chart with custom labels split over multiple lines
Placing "\n" in a custom label acts as a newline character

This gives you another way to display the labels.

Use locator(2, type = "l") to add leader lines on a plot.

You can add other graphical parameters to alter line appearence.


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 appearernce of the line, examples might be col (colour), lwd (line width), lty (line type).

Support Index | Exercises Index | Outline & TOC | Data files
My Publications

My Publications

Managing Data Using Excel, Cover

See my personal pages at GardenersOwn

Follow me...
Facebook Twitter Google+ Linkedin Amazon

See also:

Writer's Bloc
Tips & Tricks

More information

For more information see also these pages:

Writer's Bloc - additional notes on my various writing projects
MonogRaphs - longer articles on using R
Tips & Tricks - for R and Excel


Top DataAnalytics Home
Contact GardenersOwn Homepage