This is a test version of Biostars. For the public version, visit https://www.biostars.org.
Help creating coloured and labelled PCA plot

I am making a PCA plot of my data, which should be coloured and labelled according to the 2 data sets (PC1, PC2 and PC3 is for data set A, and PC4, PC5, PC6 is for data set B.)

The data appears as such:

                    Cluster_ID       DA1      DA2      DA3       DB1       DB2      DB
 5  chr5:100947454..100947489,+   3.31322  7.52365  3.67255  21.15730  8.732710 17.42640
 12 chr5:101227760..101227782,+   1.48223  3.76182  5.11534  15.71680  4.426170 13.43560
 29 chr5:102236093..102236457,+  15.60700 10.38260 12.46040   6.85094 15.551400  7.18341
 32 chr5:102249134..102249340,+  11.24750  9.63026  6.82046   3.62697  3.588780  5.58710

I applied PCA by using:

 PCA.CAGE<-prcomp(CAGE_filter_more[,2:7], scale.=TRUE) 
 summary(PCA.CAGE)

The PCA results are as follows:

                      PC1     PC2     PC3     PC4     PC5     PC6
Standard deviation     2.4416 0.14371 0.11054 0.06048 0.04262 0.01854
Proportion of Variance 0.9936 0.00344 0.00204 0.00061 0.00030 0.00006
Cumulative Proportion  0.9936 0.99699 0.99903 0.99964 0.99994 1.00000

I have tried to complete this with the following:

qplot(PCA.CAGE[1:3], PCA.CAGE[4:6], label=colnames(PC1, PC2, PC3), geom=c("point", "text"),
      data=as.data.frame(PCA.CAGE$x))

and

qplot(PCA.CAGE$x[,1:3],PCA.CAGE$x[4:6,], xlab="Data 1", ylab="Data 2")

However, I am getting errors and am not sure how to colour them separately when combining the PCA results for each data set. I am trying to do so with the columns but am not sure how to distinguish the sets and get it working. Could someone please help show me where I am going wrong?

Thank you

cage r cluster pca

I think the issue might be that you're trying to give qplot a range of data frame indices for x and y, I am not as familiar with ggplot::qplot but from what I understand qplot is a wrapper for basic ggplot2::ggplot calls and expects vectors.

If you want to plot PC1, PC2, PC3 vs. PC4, PC5, PC6 I would suggest converting your data into long format (see reshape2::melt), and doing something like this and make separate plots for PC combinations:

ggplot(data=PCS.CAGE, aes(x=PCA1, y=PCA2, colour=data_set, label=data_set)) + geom_point() + geom_text()

PCS.CAGE should look something like this:

PCA1     PCA2     data_set
1     3      data_setA
2     6     data_setB

When I tried that, it says Error: ggplot2 doesn't know how to deal with data of class prcomp.

I would rather have one whole plot, instead of separate plots.

You need to convert your data to an object of class data frame for ggplot2::ggplot(), (that's the only object class ggplot can understand). Try:

data <- as.data.frame(data)
# Check that it has been converted to data frame

class(data)

It was already a data frame.

0 answers

No answers yet.

Log in to answer this question.