Sorry for the broad question, but I am trying to generate a correlation plot that is coloured by the correlation values but where the tiles (preferably circles) are sized by another variable. I would like the correlation matrix to be ordered by hclust.
I have been playing around with corrplot, ggcorrplot and ggplot2, but I have struggled to find a way to generate this plot.
Thank you - I tried this but it didn't turn out right! I separately calculated a distance matrix from cormat and then performed hclust on this. Then took the row order and used this to order the base_cluster factor in cluster_markers. But the points were no longer restricted to the upper triangle of my plot.
data_wide <- cor(longley)
myorder <- colnames(data_wide)[hclust(dist(data_wide))$order]
library(reshape2)
library(ggplot2)
#prepare data to plot
data_long <- melt(data_wide,value.name="correlation")
data_long$Var1 <- factor(data_long$Var1,levels=myorder)
data_long$Var2 <- factor(data_long$Var2,levels=myorder)
# just a simulated additional variable to scale by
data_long$sizedby <- rnorm(dim(data_long)[1])
myplot <- ggplot(data_long,aes(x=Var1,y=Var2,color=correlation,size=sizedby)) + geom_point() + scale_size_area()
myplot
Im not too familiar with corrplot, but it is at least possible to provide a significance matrix of p-values for the respective correlations via the p.mat parameter and specify the consequences via sig.level,insig and pch in case you asked the question because you wish to display significant correlations only.
Thank you Thias, the ggplot approach works really well. One question: what would be the easiest way to just visualise the upper triangle of the plot as they are correlations, so the upper and lower triangles provide identical information.
Hi,
Please, provide your attempts or an example figure, for you us to help you.
António
This works well but unfortunately I can't work out how to size the squares by another matrix.
Same with the following:
On the other hand, I have converted the matrix into long format and run the following:
But I can't work out how to order the above using hclust, as now my data is in long format.
If you have the order from hclust, you can use that to turn the cluster names into a factor using that order as the levels.
Thank you - I tried this but it didn't turn out right! I separately calculated a distance matrix from cormat and then performed hclust on this. Then took the row order and used this to order the base_cluster factor in cluster_markers. But the points were no longer restricted to the upper triangle of my plot.
For your ggplot command what code are you using to set the factor order?
Before running ggplot, I did:
Where
hclust_order
is the cluster names in the order from hclust.All of this should be possible via
corrplot()
. I implement it here: https://github.com/kevinblighe/scDataviz/blob/master/R/plotSignatures.RThank you, which argument is it that you use to size by another matrix?
Oh, by 'another' matrix? - for that, proceed with the ggplot2 approach. lattice::levelplot could also do it.