Question: PCA plot for TPM data
0
23 months ago by
tanyabioinfo20
tanyabioinfo20 wrote:

Hi

I am having a TPM count matrix. The columns are the samples wt0hr wt6hr wt24hr kd0hr kd6hr kd24 and have four replicates for each one of them.

Can some one help me with the correct R package to plot the PCA for samples when I have the TPM data.

Thanks

Tanya

pca • 683 views
modified 23 months ago by Andy9160 • written 23 months ago by tanyabioinfo20
Answer: PCA plot for TPM data
0
23 months ago by
Andy9160
Netherlands
Andy9160 wrote:

You can do it with base R. One way of doing it is first transposing the TPM count matrix (assuming you want to run PCA on the samples rather than the genes), centering it, then doing an SVD and subsequently plotting the first and second columns of the u matrix (assuming you are interested in the first and second principal components. Alternatively, use the prcomp() function instead of SVD and plot the first and second column of the x matrix. Both should yield the same pattern (note that the values will not be the same, but the pattern will).

Example:

tpm_centered <- t(tpm-rowMeans(tpm))

#SVD

tpm_svd <- svd(tpm_centered)

plot(tpm$u[,1], tpm$u[,2])

#prcomp

tpm_prcomp <- prcomp(tpm_centered)

plot(tpm_prcomp$x[,1], tpm$x[,2])

For more information, you might want to check out this tutorial: http://genomicsclass.github.io/book/pages/pca_svd.html

Thanks

Tanya

I would recommend you do some reading on plotting in R.

Example tutorials:

Pertaining the question at hand, you could use base R plot() function, or ggplot2::ggplot(). My preference goes to the latter as it does make a lot of nice plots once you get the hang of it.

#base R

cols <- as.factor(as.numeric(sample_names))

plot(tpm$u[,1], tpm$u[,2], col = cols)

#ggplot2

library(ggplot2)

plot_df <- data.frame(PC1 = tpm$u[,1], PC2 = tpm$u[,2], Samples = sample_names)

ggplot(plot_df, aes(x = PC1, y = PC2, col = Samples)) +

geom_point()

Assuming you run this in R/RStudio that should work. If not, you would need to setup a plotting device.