PC3 & PC2 using plotPCA function in R
2
0
Entering edit mode
7.1 years ago
User000 ▴ 690

Is there a way to find PC2 PC3 using plotPCA function?

pcaData <- plotPCA(log.norm, intgroup=c("condition", "sizeFactor"), returnData=TRUE)
pcaData

I use ggplot to plot the results. ALSO, is it possible that I get different plots using plotPCA and prcomp, although I am using ntop all my genes?

R • 25k views
ADD COMMENT
6
Entering edit mode
7.1 years ago

You can easily do it by modifying the plotPCA() function. My plotPCA is a modified one, which plots the sampleName along with the bubbles.

At Line 24 to 29 >>

  d <- data.frame(PC1 = pca$x[, 1], PC2 = pca$x[, 2], group = group, 
                  intgroup.df, name = colData(rld)[,1])
  if (returnData) {
    attr(d, "percentVar") <- percentVar[1:2]
    return(d)
  }

Change PC1 and PC2 to pca$x[, 2] and pca$x[, 3] respectively. Accordingly, the percentVar[1:2] to percentVar[2:3]

ADD COMMENT
0
Entering edit mode

Hi, thanks for your answer, it works perfectly. When I plot, on y axis next to PC3, I have NA variance, any idea why is that? Thank you in advance. EDIT: just a second after I posted, I realized what I was doing wrong :)

ADD REPLY
0
Entering edit mode

glad that it helped and your other problem got resolved..

ADD REPLY
2
Entering edit mode
7.1 years ago
James Ashmore ★ 3.4k

From what I can tell there is no way to return PC2/PC3 from the plotPCA function. Instead what you could do is just use the code from within the plotPCA function to return the full results of the principal component analysis:

  # calculate the variance for each gene
  rv <- rowVars(assay(object))

  # select the ntop genes by variance
  select <- order(rv, decreasing=TRUE)[seq_len(min(ntop, length(rv)))]

  # perform a PCA on the data in assay(x) for the selected genes
  pca <- prcomp(t(assay(object)[select,]))

  # the contribution to the total variance for each component
  percentVar <- pca$sdev^2 / sum( pca$sdev^2 )
ADD COMMENT

Login before adding your answer.

Traffic: 1959 users visited in the last hour
Help About
FAQ
Access RSS
API
Stats

Use of this site constitutes acceptance of our User Agreement and Privacy Policy.

Powered by the version 2.3.6