How to color code scatter-plot of PCoA

Lord Voldemort picture Lord Voldemort · Dec 4, 2011 · Viewed 7.8k times · Source

So I am new to this. I need to run PCoA on the following data matrix. I am able to run my analyses using ADE4, labdsv, Ginko, Aabel softwares. Whats bothering me is how to color code the labels in the scatter plot. My matrix is a presence/absence matrix in the order:

SpecieName Value1 Value2
A1         0      1
A2         1      1
A3         1      1
B1         0      0
B2         0      1
E1         1      0
E2         0      0

What I want is to represent A1, A2, and A3 in red, B1 and B2 in Blue and all the E ones in black. Any help will be appreciated.

Answer

John Colby picture John Colby · Dec 4, 2011

Just pass in a factor that denotes these groups to the plotting command:

data = read.table('data.txt', header=T)

data.pca = prcomp(data[,-1])

groups = factor(gsub('(.).', '\\1', data$SpecieName))

plot(data.pca$x, col=groups)

enter image description here

Also, if you want to set specific colors, you can always index into a custom list the same way:

cols = c('red', 'blue', 'black')[groups]
plot(data.pca$x, col=cols)