Search
Question: PCA plot for TPM data
0
gravatar for tanyabioinfo
8 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
ADD COMMENTlink modified 8 months ago by Andy9160 • written 8 months ago by tanyabioinfo20
0
gravatar for Andy91
8 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

ADD COMMENTlink written 8 months ago by Andy9160

Thanks Andy. Can you please help me how can I add colors to this PCA plot based on sample names.

 

Thanks

 

Tanya

ADD REPLYlink written 8 months ago by tanyabioinfo20

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.
 

ADD REPLYlink written 8 months ago by Andy9160
Please log in to add an answer.

Help
Access

Use of this site constitutes acceptance of our User Agreement and Privacy Policy.
Powered by Biostar version 2.2.0
Traffic: 119 users visited in the last hour