Search code examples
rplotpcavegan

R: PCA plot with different colors for Sites


I´m recently trying to analyse my data and want to make the graphs a little nicer but I´m failing at this.

So I have a data set with 144 sites and 5 environmental variables. It´s basically about the substrate composition around an island and the fish abundance. On this island there is supposed to be a difference in the substrate composition between the north and the southside. Right now I am doing a pca and with the biplot function it works quite fine, but I would like to change the plot a bit.

I need one where the sites are just points and not numbered, arrows point to the different variable and the sites are colored according to their location (north or southside). So I tried everything i could find.

Most examples where with the dune data and suggested something like this:

library(vegan)
library(biplot)
data(dune)
mod <- rda(dune, scale = TRUE)
biplot(mod, scaling = 3, type = c("text", "points"))

So according to this I would just need to say text and points and R would label the variables and just make points for the sites. When i do this, however I get the Error:

Error in plot.default(x, type = "n", xlim = xlim, ylim = ylim, col = col[1L],  : 
  formal argument "type" matched by multiple actual arguments

No idea how to get around this.

So next strategy I found, is to make a plot manually like this:

require("vegan")
data(dune, dune.env)
mod <- rda(dune, scale = TRUE)
scl <- 3 ## scaling == 3
colvec <- c("red2", "green4", "mediumblue")
plot(mod, type = "n", scaling = scl)
with(dune.env, points(mod, display = "sites", col = colvec[Use],
                      scaling = scl, pch = 21, bg = colvec[Use]))
text(mod,display="species", scaling = scl, cex = 0.8, col = "darkcyan")
with(dune.env, legend("bottomright", legend = levels(Use), bty = "n",
                      col = colvec, pch = 21, pt.bg = colvec))

This works fine so far as well, I get different colors and points, but now the arrows are missing. So I found that this should be corrected easy, if i just put "display="bp"" in the text line. But this doesn´t work either. Everytime I put "bp" R says:

Error in match.arg(display) : 
  argument "display" is missing, with no default

So I´m kind of desperate now. I looked through all the answers here and I don´t understand why display="bp" and type=c("text","points") is not working for me.

If anyone has an idea i would be super grateful. https://www.dropbox.com/sh/y8xzq0bs6mus727/AADmasrXxUp6JTTHN5Gr9eufa?dl=0 This is the link to my dropbox folder. It contains my R-script and the csv files. The one named environmentalvariables_Kon1 also contains the data about north and southside.

So yeah...if anyone could help me. That would be awesome. I really don´t know what to do anymore.

Best regards, Nancy


Solution

  • You can add arrows with arrows(). See the code for vegan:::biplot.rda to see how it works in the original function.

    With your plot, add

    g <- scores(mod, display = "species")
    len <- 1
    arrows(0, 0, len * g[, 1],  len * g[, 2], length = 0.05, col = "darkcyan")
    

    enter image description here

    You might want to adjust the value of len to make the arrows longer