Seb Matamoros
Seb Matamoros

Reputation: 339

Color the individuals of a R PCoA plot by groups

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

Upvotes: 1

Views: 1883

Answers (1)

Jari Oksanen
Jari Oksanen

Reputation: 3682

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)

Upvotes: 1

Related Questions