Site icon R-bloggers

Decluttering ordination plots part 4: orditkplot()

[This article was first published on From the Bottom of the Heap - R, and kindly contributed to R-bloggers]. (You can report issue about the content on this page here)
Want to share your content on R-bloggers? click here if you have a blog, or here if you don't.

Earlier in this series I looked at the ordilabel() and then the orditorp() functions, and most recently the ordipointlabel() function in the vegan package as means to improve labelling in ordination plots. In this, the fourth and final post in the series I take a look at orditkplot(). If you’ve created ordination diagrams before or been following the previous posts in the irregular series, you’ll have an appreciation for the problems of drawing plots that look, well, good! Without hand editing the diagrams, there is little that even ordipointlable() can do for you if you want a plot created automagically. orditkplot() sits between the automated methods for decluttering ordination plots I’ve looked at previously and hand-editing in dedicated drawing software like Inkscape or Illustrator, and allows some level of tweaking the locations of labelled points within R.

To use orditkplot(), you’ll need an R installation that can use the Tcl/Tk ecosystem. On Windows this is probably taken care of for you as part of the Windows binaries. Installing Tcl and Tk on a Linux box is pretty straight-forward. The only difficulties I have come across are with MacOS X and that is probably because I don’t own any Apple kit and haven’t used it much. To check if your R can use Tcl/Tk, run capabilities() and look for the tcltk element. on my Linux laptop I have

> capabilities()
   jpeg      png     tiff    tcltk      X11     aqua http/ftp  sockets 
   TRUE     TRUE     TRUE     TRUE     TRUE    FALSE     TRUE     TRUE 
 libxml     fifo   cledit    iconv      NLS  profmem    cairo 
   TRUE     TRUE     TRUE     TRUE     TRUE    FALSE     TRUE

Assuming your R can use Tcl/Tk, we’ll begin.

As with the previous posts, I’ll illustrate orditkplot() through a PCA of the Dutch Dune Meadow data set distributed with vegan. Load the package and the data and fit the PCA; we will use scaling = 3 throughout

## load vegan and the data
require("vegan")
data(dune)

ord <- rda(dune) ## PCA of Dune data

scl <- 3 ## scaling = 3

orditkplot() will work with any of the ordination objects in vegan, but it is best to pass it something that is reasonably close to a good-looking layout. For that, I’ll use the configuration of points and labels that ordipointlabel() achieves. Create the base plot and store the returned object

## base plot
bplot <- ordipointlabel(ord)

This we pass to orditkplot()

## improve this via orditkplot
orditkplot(bplot)

Note that when calling orditkplot(), you can specify a number of extra arguments that control how the configuration will be drawn on the Tk canvas, as well as pass in some graphical parameters from ?par; read ?orditkplot for details of which parameters are currently supported. Not all will affect the look of the rendered plot on the canvas but they do allow another level of control.

Hopefully you will now have a new window on screen that looks like

PCA biplot of the Dutch dune meadow data produced using orditkplot()

You can now move labels around on the plot and edit labels. These features are illustrated in the video below, in which I spend a few minutes editing the base plot so that there is no overlap in the labels.

The row of buttons along the bottom of the canvas window allow you to export the current plot to one of several graphics formats, dump the plot as a new R object in your session or close the canvas window. The buttons, in order, do

At the end of the video I used the Dump to R feature to create object bplot2, which can be plotted on a graphics device

plot(bplot2)

vegan also provides methods for the points() and text() generics, allowing you to build up a plot in layers

## build up plot
plot(ord, type = "n")
points(bplot2, pch = bplot2$args$pch)
text(bplot2, cex = 0.8, col = "navy")

The plot produced is shown below

Plot produced by using points() and text() methods for class orditkplot.

In the example above, note that to distinguish between the species and the sites/sample points I needed to refer to the \(args\)pch component of the bplot2 object. This is because

  1. Once the points are on the Tcl/Tk canvas the correspondence between which are sites/samples and which are species is lost, and
  2. The points() method is inconsistent with the plot() method and uses the same plotting character for all points1.

There is also a scores() method if you need to extract the locations of the labels or the points in the coordinate space of the plot.

And with that, we’ve come to end of the brief tour of the tools that vegan provides to help produce ordination plots. If these don’t meet your needs, then you can export the plot as an EPS, PDF, or another vector format that can be edited in a vector drawing package like Inkscape. vegan provides lots of other functions to enhance ordination plots, and I’ll take a look at some of those next year. Use the comments to let me know if of any particular functions you’d like me to cover first.

< section class="footnotes">
  1. Note to self: I should probably fix that….

To leave a comment for the author, please follow the link and comment on their blog: From the Bottom of the Heap - R.

R-bloggers.com offers daily e-mail updates about R news and tutorials about learning R and many other topics. Click here if you're looking to post or find an R/data-science job.
Want to share your content on R-bloggers? click here if you have a blog, or here if you don't.