Entering edit mode
Dear Michael and others,
Hope you all are well. Does anyone know whether we can plot PC1 vs. PC3/PC4 in plotPCA?
Kind Regards,
synat
Dear Michael and others,
Hope you all are well. Does anyone know whether we can plot PC1 vs. PC3/PC4 in plotPCA?
Kind Regards,
synat
Use of this site constitutes acceptance of our User Agreement and Privacy Policy.
I edited the plotPCA code a little to do just this! First I added a "nPC" variable to the function:
function (object, intgroup = "condition", ntop = 500, returnData = FALSE, nPC = 2)
It defaults to two, so it behaves just as the original if you don't specify.
Then you need to change the following line:
d <- data.frame(PC1 = pca$x[, 1], PC2 = pca$x[, 2], group = group, intgroup.df, name = colnames(object))
to:
d <- cbind(pca$x[,seq_len(min(nPC, ncol(pca$x))), drop = FALSE], data.frame(group = group, intgroup.df, name = colnames(object)))
It's just pulling more columns from the
pca$x
data frame in theprcomp
object, while keeping everything else in the data frame. I didn't change anything to be able to plot these additional PCs, as I usually use this to get a conveniently formatted data frame usingreturnData=T
. But because the data is there, it wouldn't be too hard to change.Hope this helps!
Thanks, dear for your help.
Kind Regards,
synat
This is so simple and elegant it should be incorporated into the plotPCA function! A small modification to make it run smoothly is:
to