3

I have a problem, when i try to do a PCA plot on some gene expression data, i use the code below to plot it, but i would like to make different colors depending on which category a Tissue belongs to.

data <- read.table("rmaFinal.txt", row.names=1, sep="\t",header=TRUE, dec=".")
pca <- prcomp(t(data), cor=TRUE)
plot(pca$x, pch=20) 

My data is formatted as

      Tissue1 tissue2 tissue3
Gene1 1        2       3
Gene2 2        3       4
Gene3 3        4       5

I have a total of 116 different tissues, that all can be classified into a total of 12 categories. I therefore have a list like this, with the category of each of the 116 tissue types.

category = c( "Seed","Seed","Seed","Stem","Seed","Seed","Seed","Mesocotyl","Spikelets")

I would like to color my PCA plot depending on which of the 12 categories a given sample is present in. I have tried to read around, but none of the solutions i could find did work for this. How do i combine the category list with the PCA plot?

joran
  • 169,992
  • 32
  • 429
  • 468

1 Answers1

4

Are you looking to do something like this?

library(FactoMineR)
iris.pca <- PCA(iris, quali.sup=5)
plot(iris.pca, habillage = 5, 
     col.hab = c("green", "blue", "red"), 
     title = "Dataset projected onto PC1-2 Subspace")

enter image description here

hat-tip: http://benmabey.com/presentations/pca-tutorial/#34

Ben
  • 41,615
  • 18
  • 132
  • 227
  • Hi Ben, yes it is, but if i don't want to include the categories in the same matrix, is it then possible to have it as a vector like i have put above? and how do i handle that with the quali_sup? – Bjørn Øst Hansen May 22 '13 at 21:57
  • Probably best if you asked another question showing what you've tried among these lines. I'll keep my eye out for it! – Ben May 22 '13 at 22:29
  • 1
    Yes, sorry, have been on holidays without internet. I ended up using the ggplot library. type=c("Seed","Seed","Seed","Stem"... ) qplot(pca$x[,1],pca$x[,2], colour=type, xlab="PCA 1", ylab="PCA 2") No idea why it doesn't mark it as code, sorry. Thanks for the help – Bjørn Øst Hansen Jun 05 '13 at 13:31