Question: plotting correlation between samples
gravatar for teresati
21 months ago by
teresati10 wrote:

Dear all,

I would like to plot the correlation between my samples. I have a matrix with genes in rows and samples in columns, and for each gene a logCPM normalized values. If I want to look at the correlation between 2 samples I get an Error:

x <-[,1]))  
y <-[,2]))
correlation <-, y,use="pairwise.complete.obs"))

Error in : figure margins too large
In addition: Warning messages:
1: In min(x) : no non-missing arguments to min; returning Inf
2: In max(x) : no non-missing arguments to max; returning -Inf
3: In min(x) : no non-missing arguments to min; returning Inf
4: In max(x) : no non-missing arguments to max; returning -Inf

It is correct to transpose the columns?

Furthermore, are log cpms usually used to compute correlations between samples?

To solve the margin problem I have tried to adjust the margins with:


Thank you for any suggestion.


ADD COMMENTlink modified 21 months ago by Aaron Lun21k • written 21 months ago by teresati10

What exactly are you trying to do? You are producing a matrix of the pairwise correlations between two single observations, which will be NA for all of those pairs (you can't compute a correlation between just two observations). You could hypothetically compute the correlation between the two samples, but that would be a single number. Neither of these things is interesting, or illuminating.

Are you perhaps trying to show which samples are more similar to each other? If so, the main tool is either MDS or PCA, both of which are useful for showing that sort of thing.

You might want to spend some time reading some tutorials, which could help you get on track. Here is one, if you are using DESeq2. You could also use the DESeq2 vignette, or if you are using edgeR, there is a User's Guide for that package.

ADD REPLYlink written 21 months ago by James W. MacDonald48k
gravatar for Aaron Lun
21 months ago by
Aaron Lun21k
Cambridge, United Kingdom
Aaron Lun21k wrote:

As James says, the correlation between expression profiles of two libraries is rarely interesting in and of itself. More useful is the relative size of the correlation between different pairs. For example, is the correlation between samples in the same group greater than the correlation between samples in different groups? This reasoning motivates the construction of a MDS plot, as demonstrated in the code below:

stuff <- matrix(rnorm(1000), ncol=10) # 100 genes, 10 samples
cor.mat <- cor(stuff)
dist.mat <- sqrt(0.5*(1-cor.mat)) # magic step
coords <- cmdscale(dist.mat, k=2)
plot(coords[,1], coords[,2])

The magic step converts the pairwise correlation into a valid distance metric (see for details). The use of correlations has a couple of advantages over just using the Euclidean distances between the log-expression profiles:

  1. Correlations are insensitive to scaling, so you don't have to worry about normalising the data.
  2. Spearman's rho is robust to outliers that might interfere with estimation of relative differences between samples - though on the flip side, it is less sensitive to differences between libraries that do not involve many genes.

This is why we sometimes use correlation-based (i.e., cosine) distances in single-cell RNA-seq, instead of running PCA on the log-expression profiles or computing Euclidean distances between pairs of profiles for use in "standard" MDS.

As for your other question, I would calculate correlations from log-expression values. The log-transformation avoids domination of the estimated correlation by a handful of genes with large counts. Normalisation doesn't matter so much as the computed correlation isn't affected by scaling (aside from small changes with respect to the prior count). In fact, you could even use the original counts for computing Spearman's rho, which will be unaffected by general monotonic transformations of the data.

ADD COMMENTlink modified 21 months ago • written 21 months ago by Aaron Lun21k

Another way to plot the correlations, would be with a heatmap. To follow up on Aaron's code example:


heatmap.2(cor.mat, trace="none")
ADD REPLYlink written 21 months ago by b.nota300
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: 377 users visited in the last hour