1

Should be a simple question, but I haven't found exactly how to do it so far.

I have a matrix as follow:

sample var1 var2 var3 etc.
1      5    7    3     1
2      0    1    6     8
3      7    6    8     9
4      5    3    2     4

I performed a PCoA using Vegan and plotted the results. Now my problem is that I want to color the samples according to a pre-defined group:

group sample
1     1
1     2
2     3
2     4

How can I import the groups and then plot the points colored according to the group tey belong to? It looks simple but I have been scratching my head over this.

Thanks! Seb

Seb Matamoros
  • 339
  • 2
  • 4
  • 14

1 Answers1

1

You said you used vegan PCoA which I assume to mean wcmdscale function. The default vegan::wcmdscale only returns a scores matrix similarly as standard stats::cmdscale, but if you added some special arguments (such as eig = TRUE) you get a full wcmdscale result object with dedicated plot and points methods and you can do: plot(<pcoa-result>, type="n") # no reproducible example: edit like needed points(<pcoa-result>, col = group) # no reproducible example: group must be visible If you have a modern vegan (2.5.x) the following also works: library(magrittr) plot(<full-pcoa-result>, type = "n") %>% points("sites", col = group)

Jari Oksanen
  • 3,287
  • 1
  • 11
  • 15
  • Thanks for your answer. I tried it, but it still doesn't work. Here is the script I used: distmatrix <- vegdist(dataset[, 2:63],method='bray', na.rm=T) Ordination.model1 <- cmdscale(distmatrix, k=2, eig=T, add=F) rownames(Ordination.model1$points) <- rownames(dataset) plot(scores(Ordination.model1, display='sites', choices=c(1,2), col = dataset$Col.grp)) I am a bit puzzled, I used a similar script previously to color groups (not from vegan output though) and it worked. I can't see what is not working there... – Seb Matamoros Jul 04 '18 at 07:59
  • First thing: do **not** use `plot(scores())` but just `plot`! There is no `plot.scores()` function, and you must at least set `asp=1` (for equal aspect ratio) if you use `plot(scores())`. Second thing: now you give your graphical arguments to the `scores()` function (that you should not use), and the `scores` function does no graphics, but wastes these arguments. You should give these arguments to `plot` **without** using `scores()`. Where did you get the idea of using `scores()`? Certainly not from **vegan** documentation. – Jari Oksanen Jul 04 '18 at 20:33
  • Thanks for the answer. Finally figured it out by building the graph in sequence from the metaMDS function, the `points` variable and a separate vector of colors. It goes something like that: `data.mds <- metaMDS(my.data)` then `col.vect <- c("green", "red")` then `plot(data.mds, type = "n")` then `with(data.grp, points(data.mds, display = "sites", col = col.vect[Groups], bg = col.vect[Groups])` – Seb Matamoros Jul 27 '18 at 09:34