1

I have performed Spearman correlation for my data. Then I tried to cluster and plot my data using the "ward.D2" method for corrplot()and pheatmap(). However, the order of the variables is different between the two plots.

Could someone help me clarify this point, thus correcting my code and creating the two plots with the same order of clustered variables? Thank you so much.

#A sample of my dataset: 

dput(Data_corr)

structure(list(S_cHDLP = c(0.299999999999999, -2.78, 0.880000000000001, 
2.48, 2.15, 5.31, 3.02, 1.19, 2.1, -1.18, -0.34, 1.25, -3.25, 
-3.16, 0.19, -0.100000000000001, -2.16, -0.220000000000001, 0.77, 
-2.12), H7P = c(-0.18, -0.48, -0.13, -0.21, 0.07, 0.64, -0.13, 
-0.1, 0.12, -0.22, 0.09, -0.0399999999999999, -1.56, 0.39, 0.58, 
-0.49, 0.2, 0.13, 0.11, 0.06), H6P = c(0, 0, 0, 0.16, -0.23, 
0, 0, 0, -0.26, -0.28, 0.06, -0.17, 1.16, -0.12, -0.32, -0.17, 
0.38, 0.05, 0.01, 0), H5P = c(0, 0.84, 0.47, 1.21, 0.01, 0.21, 
1.36, 0.2, -0.12, 0.93, -1.01, 0, -0.58, -0.97, -1, 0.97, -0.89, 
0.35, -0.59, -0.12), H4P = c(-0.12, -1.27, -0.18, 0.25, 1.02, 
1.26, -0.62, -0.16, 0.25, -0.01, 0.44, 0.17, 0.19, 0.97, 2.35, 
0.3, -0.18, 0.03, 0.0899999999999999, 0.38), H3P = c(-0.31, 0.39, 
0.13, 0.29, 0, 0.02, -0.07, 0, 0, -0.32, 0, -0.79, 0, -0.53, 
-0.71, -0.2, 2.08, 0.86, 0, 0), H2P = c(-1.28, -0.619999999999999, 
-1.07, 1.96, 0.15, 4.92, 1.55, 3, -0.459999999999999, -0.56, 
1.12, 3.44, -1.48, -1.27, 1.45, 0.609999999999999, -1.59, -1.57, 
2.04, 2.03), H1P = c(1.58, -2.15, 1.96, 0.51, 2, 0.37, 1.47, 
-1.83, 2.56, -0.62, -1.46, -2.19, -1.77, -1.9, -1.25, -0.73, 
-0.57, 1.35, -1.28, -4.14), TRLZ_TRL = c(4.61, 1.49, -2.71, 1.54, 
-5.46, 2.18, 3.48, 12.83, 7.51, 7.74, -8.38, -0.729999999999997, 
6.11, -19.74, -0.869999999999997, -1.82, -1.57000000000001, 0.609999999999999, 
-14.79, -18.65), LDLZ = c(-0.0599999999999987, -0.400000000000002, 
-0.289999999999999, -1.2, -0.479999999999997, -0.59, -1.29, -0.0599999999999987, 
0.210000000000001, -1.58, 1.97, 0.0800000000000018, -1, 1.95, 
1.41, 0.00999999999999801, 0.430000000000003, -0.289999999999999, 
0.68, 0.52), HDLZ = c(-0.200000000000001, -0.200000000000001, 
-0.0700000000000003, 0, -0.0200000000000014, -0.0199999999999996, 
-0.0399999999999991, -0.119999999999999, -0.0900000000000016, 
-0.0500000000000007, -0.15, -0.16, -0.640000000000001, 0.42, 
0.16, -0.130000000000001, 0.15, 0.41, -0.0300000000000011, 0.18
)), class = "data.frame", row.names = c(NA, -20L))
library(pheatmap)
library(corrplot)


CorMethod <- "spearman"

CorMatrix <- cor(Data_corr, method=CorMethod, use="pairwise.complete.obs")


## 1st Plot

Plot3<-pheatmap(CorMatrix, cluster_cols=T, cluster_rows=T, cutree_rows = 3, angle_col=45, fontsize_col=5, fontsize_row = 7, treeheight_col=0, clustering_method="ward.D2")



#2nd Plot

Plot8 <-corrplot(CorMatrix, method="square", type="lower", order="hclust", hclust.method="ward.D2", tl.pos="ld", tl.cex = 0.5, tl.col="black", tl.srt=45)


Test_CorMatrix_3_pheatmap_spearman

Test_CorMatrix_8_corrplot_Diag_square_spearman

Geli
  • 75
  • 5

1 Answers1

1

You can create a corrplot with the same order given by pheatmap as follows:

#2nd Plot
library(RColorBrewer)
ord <- Plot3$tree_row$order
ReordCorMatrix <- CorMatrix[ord, ord]
Plot8 <-corrplot(ReordCorMatrix, method="square", type="lower", order="original", 
                 hclust.method="ward.D2", 
                 tl.pos="ld", tl.cex = 0.5, tl.col="black", tl.srt=45, 
                 col=colorRampPalette(rev(brewer.pal(n = 7, name="RdYlBu")))(100))

enter image description here

Marco Sandri
  • 23,289
  • 7
  • 54
  • 58