PC3 & PC2 using plotPCA function in R
3
0
Entering edit mode
7.7 years ago
User000 ▴ 710

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 plotPCA PCA • 26k views
ADD COMMENT
6
Entering edit mode
7.7 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.7 years ago
James Ashmore ★ 3.5k

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
1
Entering edit mode
28 days ago
nuriagaralon ▴ 10

I landed here looking to plot other PCs and the plotPCA function probably got updated at some point.

You can now pass the pcsToUse argument to return whichever PCs you want. So, for PC2/PC3:

plotPCA(log.norm, intgroup=c("condition", "sizeFactor"), pcsToUse = 2:3)
ADD COMMENT

Login before adding your answer.

Traffic: 2277 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