Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Dendrogram with Corrplot (R)

Does anyone have a method to adorn an R corrplot correlation plot with a dendrogram?

like image 622
abalter Avatar asked Oct 24 '16 22:10

abalter


2 Answers

The closest solution I know of is to use a heatmap on a correlation matrix, for example you could also use gplots::heatmap.2.

Here is how to do it using the heatmaply R package, which also offers an interactive interface where you can zoom-in and get a tooltip when hovering over the cells:

# for the first time:
# install.packages("heatmaply")

library(heatmaply)
my_cor <- cor(mtcars)
heatmaply_cor(my_cor)

Here is how it looks:

enter image description here

You can learn more about heatmaply in this vignette.

like image 77
Tal Galili Avatar answered Sep 22 '22 15:09

Tal Galili


heatmaply actually has this functionality baked in since about December 2017! See the example below taken from the upcoming v1.0 vignette:

library("heatmaply")
r <- cor(mtcars)
## We use rcorr to calculate a matrix of p-values from correlation tests
library("Hmisc")
mtcars.rcorr <- rcorr(as.matrix(mtcars))
p <- mtcars.rcorr$P

heatmaply_cor(
  r,
  node_type = "scatter",
  point_size_mat = -log10(p), 
  point_size_name = "-log10(p-value)",
  label_names = c("x", "y", "Correlation")
)

like image 24
alan ocallaghan Avatar answered Sep 21 '22 15:09

alan ocallaghan