Question: PCA plot for TPM data
gravatar for tanyabioinfo
25 days ago by
tanyabioinfo10 wrote:


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.




ADD COMMENTlink modified 25 days ago by Andy9130 • written 25 days ago by tanyabioinfo10
gravatar for Andy91
25 days ago by
Andy9130 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).



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


tpm_svd <- svd(tpm_centered)

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


tpm_prcomp <- prcomp(tpm_centered)

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


For more information, you might want to check out this tutorial:

ADD COMMENTlink written 25 days ago by Andy9130

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





ADD REPLYlink written 24 days ago by tanyabioinfo10

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)



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)) +



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

ADD REPLYlink written 24 days ago by Andy9130
Please log in to add an answer.


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