Dendrogram with Corrplot (R)

好久不见. 提交于 2021-02-09 11:42:19

问题


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


回答1:


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:

You can learn more about heatmaply in this vignette.




回答2:


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 this function to calculate a matrix of p-values from correlation tests
## https://stackoverflow.com/a/13112337/4747043
cor.test.p <- function(x){
    FUN <- function(x, y) cor.test(x, y)[["p.value"]]
    z <- outer(
      colnames(x), 
      colnames(x), 
      Vectorize(function(i,j) FUN(x[,i], x[,j]))
    )
    dimnames(z) <- list(colnames(x), colnames(x))
    z
}
p <- cor.test.p(mtcars)
heatmaply_cor(
  r,
  node_type = "scatter",
  point_size_mat = -log10(p), 
  point_size_name = "-log10(p-value)",
  label_names = c("x", "y", "Correlation")
)



来源:https://stackoverflow.com/questions/40228235/dendrogram-with-corrplot-r

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!