4

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.

Josh O'Brien
  • 159,210
  • 26
  • 366
  • 455
Lord Voldemort
  • 199
  • 1
  • 2
  • 8

1 Answers1

6

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)
John Colby
  • 22,169
  • 4
  • 57
  • 69
  • Thanks for your reply. Actually, my matrix is like a distance matrix (n*n). I am using cmdscale(distance_matrix, k) to run a Principal Coordinate Analysis (PCoA). So in my distance matrix, I have specie names on both the first row and first column. I use scatter to show me the PCoA object returned by cmdscale and I see my species plotted in the way I want. But do not know how to color code them. Will the answer you provided work for distance matrices as well? I am an absoulte beginner in R – Lord Voldemort Dec 03 '11 at 23:35
  • Ahh gotcha. Yes, this is the standard way to supply colors to most R plotting commands. Good luck! – John Colby Dec 03 '11 at 23:46
  • What does this line do? data.pca = prcomp(data[,-1]) – tommy.carstensen Apr 14 '15 at 10:03
  • Is it possible to add a legend? – tommy.carstensen Apr 14 '15 at 11:17