Dr. Mark Gardener

Home
About

Providing training for:

  • Ecology
  • Data analysis
  • Statistics
  • R The statistical programming language
  • Data management
  • Data mining

Tips and Tricks - for R and Excel

On these pages you can find tips, tricks and hints for using both R and Excel. At the end of each tip there are links forwards and backwards as appropriate. There is also an index of R tips and an index of Excel tips.

For most analytical purposes the combination of Excel and R is unbeatable! Excel is great as a data management tool and for preparing data for analysis. You can also use it to get an overview of your data or to make simple (and not so simple) graphs. R is an analytical "swiss army knife" and can carry out a mind-boggling array of analytical routines as well as producing great graphics.

Tips & Tricks for R | Tips & Tricks for Excel | An Introduction to R | MonogRaphs | Writer's Bloc


Use the locator() command to place labels interactively.

Top

Use locator() to place labels interactively

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 the following example you'll see the locator() command used to place labels on a pie() chart.


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

Top

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 and 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.

Top

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!


See more about custom labels

Extra refinement over placement

If you use the text() command there are a few additional tweaks you can use to help you place your labels more "accurately". See the support notes Exercise 6.5.1 for Statistics for Ecologists Using R and Excel for more details.


Top << Previous tip: Incomplete final line error in read.csv() >> Next tip: Read column names as numbers when importing
 
Follow me...
Facebook Twitter Google+ Linkedin Amazon
Top Tips & Tricks Home Index of R Tips Index of Excel Tips  
More links:

An introduction to R

See my Publications about Excel, R, statistics and data analysis Courses in R, data analysis, data management and statistics Visit the R Project website
 

See my Publications about statistics and data analysis.

MonogRaphs: random topics in R

Writer's Bloc – my latest writing project includes R scripts

Courses in data analysis, data management and statistics.

My Publications about statistics and data analysis

Managing Data Using Excel, Cover

See my personal pages at GardenersOwn


Top Home
Data Analysis
Contact GardenersOwn Homepage