Plotting RDA (vegan) in ggplot

Martin Andersen picture Martin Andersen · Aug 25, 2015 · Viewed 15.4k times · Source

I'm still new to R, trying to learn how to use the library vegan, which I can easily plot in R with the normal plot function. The problem arises when I want to plot the data in ggplot. I know I have to extract the right data from the list I've created, but which and how? The dataset I've been practicing on can be downloaded here https://drive.google.com/file/d/0B1PQGov60aoudVR3dVZBX1VKaHc/view?usp=sharing The code I've been using to get the data transformed is this:

library(vegan)
library(dplyr)
library(ggplot2)
library(grid)
data <- read.csv(file = "People.csv", header = T, sep = ",", dec = ".", check.names = F, na.strings=c("NA", "-", "?"))
data2 <- data[,-1]
rownames(data2) <- data[,1]
data2 <- scale(data2, center = T, scale = apply(data2, 2, sd))
data2.pca <- rda(data2)

Which gives me a list I can plot using the basic "plot" and "biplot" function, but I am at a loss as to how to plot both PCA and biplot in ggplot. I would also like to color the data points by group, e.g. sex. Any help would be great.

Answer

Gavin Simpson picture Gavin Simpson · Aug 26, 2015

You can use my ggvegan package for this. It is still in-development though usable for some classes of objects including rda and cca ones.

Assuming the example data and analysis you can simply do:

autoplot(data2.pca, arrows = TRUE)

to get the sort of biplot you want. This produces

enter image description here

You can get site labels via

autoplot(data2.pca, arrows = TRUE, geom = "text", legend = "none")

which also shows how to suppress the legend if required (legend.position takes values suitable for the same theme element in ggplot2).

enter image description here

You don't have a huge amount of control other the look of things with autoplot() methods (yet!), but you can use fortify() to get the data the way ggplot2 requires it and then use ideas from the other answers or study the code for ggvegan:::autoplot.rda for the specifics.

You need to install ggvegan from github as the package is not yet on CRAN:

install.packages("devtools")
devtools::install_github("gavinsimpson/ggvegan")

which will get you version 0.0-6 (or later) which includes some minor tweaks to produce neater plots than previous versions.