[英]Correlation heatmap for a data frame split by a factor in R
您可以使用psych
包和图形包(例如ggplot2
或pheatmap
。 首先定义一个自定义函数来获取 p 值,然后使用by
应用该函数。
library(psych)
myfun <- function(df) corr.test(df[, 1], df[, 3:6], method = "spearman", adjust =
"none")$p
dfs <- by(mtcars, mtcars$cyl, myfun)
mat <- do.call(rbind, dfs)
rownames(mat) <- c("cyl = 4", "cyl = 6", "cyl = 8")
library(pheatmap)
pheatmap(mat,
cluster_rows = F,
cluster_cols = F,
display_numbers = T,
show_rownames = T,
show_colnames = T,
number_format = "%.4f")
我没有尝试完全复制上面的热图,但您可以阅读?pheatmap
以了解其所有选项。
附注:如果任何人都可以做到这一点,在适当的功能purrr
管道进入ggplot2
的热图,你有我给予好评。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.