使用 R 中变量之间的相关值制作矩形矩阵热图

Making a rectangular matrix heatmap using correlation values between variables in R

我正在尝试使用 R 中变量之间的相关值制作一个矩形矩阵热图。我使用了基本的热图函数,但没有得到预期的效果。我希望变量的名称显示在右侧而不是数字,我不想要这些行。如果有更好的图表来创建更好的功能,我会很高兴。我也想补个图例

注意:数据由计算的相关值组成

data10 <- structure(list(Electrode = c("Fz", "F3", "F7", "FT9", "FC5", 
"FC1", "C3", "T7", "TP9", "CP5", "CP1", "Pz", "P3", "P7", "O1", 
"Oz", "O2", "P4", "P8", "TP10", "CP6", "CP2", "Cz", "C4", "T8", 
"FC6", "FC2", "F4", "F8"), IDE_CEN = c(0.289085553, -0.040364278, 
-0.23444593, -0.174289056, -0.341244576, -0.003670951, -0.193078617, 
-0.289021483, -0.080720823, -0.174416554, 0.011169192, 0.056160101, 
0.029518861, -0.098985661, -0.026726966, -0.086753437, -0.011331318, 
0.128140686, 0.057792173, -0.133527061, 0.011249671, 0.091677619, 
-0.170242194, -0.193177385, -0.156362157, -0.232429957, -0.162449406, 
0.032521691, -0.188177905), NJ_CEN = c(0.069938616, 0.070596405, 
-0.014454024, 0.251870803, 0.037665033, 0.152478385, 0.109559416, 
0.064564861, 0.290055537, 0.153677236, 0.305780042, 0.297954065, 
0.349571034, 0.204741904, 0.193468243, 0.165960995, 0.226244244, 
0.190829218, 0.188284709, 0.283481916, 0.136419773, 0.38492844, 
0.357102858, 0.205588183, 0.081746294, 0.033268488, 0.2565965, 
0.118334399, -0.029007439), MT_CEN = c(0.170415102, 0.095407428, 
0.037058094, 0.296885625, 0.047983845, 0.187816135, 0.054785505, 
0.137090607, 0.350060379, 0.160685515, 0.259345666, 0.327800337, 
0.338078081, 0.272518304, 0.351981555, 0.293046001, 0.370511012, 
0.279094445, 0.322401529, 0.340214395, 0.203988595, 0.355038268, 
0.159964997, 0.147007604, 0.137270588, 0.064700459, 0.207665974, 
0.200686438, 0.038567514), PL_CEN = c(0.233668303, -0.097900676, 
-0.26022078, -0.163697409, -0.275954056, 0.01414225, -0.074666038, 
-0.19278938, 0.017192638, -0.077713235, 0.006956221, -0.003372238, 
-0.043625727, -0.040999695, -0.061740705, -0.118584172, -0.084526487, 
0.100952874, 0.072975412, -0.019765695, 0.149684462, 0.069758976, 
-0.252490904, -0.069383542, 0.044649778, -0.077222047, -0.176133072, 
0.035150567, -0.130923228), RV_CEN = c(-0.219319073, -0.18939396, 
-0.045385769, -0.264485165, -0.115583396, -0.140414261, -0.070454108, 
-0.197187833, -0.297996164, -0.199781685, -0.254521378, -0.331622003, 
-0.311652644, -0.274568436, -0.346976422, -0.312284793, -0.353043964, 
-0.256559944, -0.309664892, -0.248515687, -0.103311691, -0.302567649, 
-0.255234299, -0.045257647, -0.053160297, -0.014984892, -0.25419166, 
-0.260759921, -0.007678838), EE_CEN = c(0.0098728, -0.159873611, 
0.140245057, 0.384287928, -0.097575603, 0.111373917, -0.135127093, 
0.070004896, 0.277797945, -0.037157269, 0.056339112, 0.234177818, 
0.125766326, 0.164387399, 0.303076316, 0.304370915, 0.302179792, 
0.284937264, 0.217391602, 0.250264217, 0.248506679, 0.264425048, 
-0.24998381, 0.194142545, 0.205776711, 0.168609801, -0.01893364, 
0.059275513, 0.16779318), IDE_IPS = c(-0.002126294, -0.001723149, 
-0.059461146, -0.069860202, -0.072004992, 0.144220026, -0.036186689, 
-0.163248116, -0.09694795, -0.111151574, 0.04639296, -0.099165442, 
-0.000120392, -0.139100468, -0.17924007, -0.218498816, -0.077361771, 
-0.0301273, -0.096386385, -0.172424645, -0.138815069, 0.052833604, 
0.193841772, -0.058690472, -0.182362255, -0.114756076, 0.101985003, 
-0.058099203, -0.066994227), NJ_IPS = c(-0.184760898, -0.262675174, 
-0.0933219, 0.139076737, 0.054345143, 0.169816642, 0.160699366, 
0.041003226, 0.026932088, 0.083405521, 0.153879148, 0.01098305, 
0.107202762, -0.00985623, -0.096739371, -0.151184422, 0.050660291, 
0.026105527, 0.088380884, 0.159612727, 0.081609933, 0.064335902, 
0.137679261, 0.089447254, 0.012214944, 0.006950912, 0.243031571, 
-0.083232234, -0.10633534), MT_IPS = c(0.052367146, -0.069274357, 
-0.057895767, 0.165898526, 0.142594539, 0.247621386, 0.1892933, 
0.120357264, 0.107080498, 0.139238543, 0.182412108, 0.117590793, 
0.150056085, 0.068371383, 0.043694567, -0.015334148, 0.183119451, 
0.151916418, 0.218674834, 0.216154104, 0.14341797, 0.110449711, 
0.070077124, 0.052286242, 0.069675247, 0.032729067, 0.269567137, 
0.085488293, -0.057178932), PL_IPS = c(0.320489514, 0.33593278, 
0.207118483, 0.236737044, 0.279376483, 0.021733316, 0.092895676, 
0.245800917, 0.365104773, 0.189230352, 0.134540772, 0.094364922, 
0.125856855, 0.278453584, 0.192038058, 0.159871203, 0.190627541, 
0.206375965, 0.278332686, 0.310436947, 0.162456334, 0.17430174, 
-0.161049487, 0.040874189, 0.234737019, 0.135640622, 0.031081633, 
0.142899205, 0.012003294), RV_IPS = c(-0.205737138, -0.116408938, 
-0.011515186, -0.17776059, -0.193654678, -0.301001006, -0.155094691, 
-0.113985036, -0.094801274, -0.136308791, -0.203216848, -0.209894226, 
-0.162726702, -0.094282013, -0.10717168, -0.09239757, -0.230913689, 
-0.232184173, -0.210235071, -0.148324767, -0.106575618, -0.161172241, 
-0.116611155, -0.034087632, -0.055007792, -0.07773681, -0.316377922, 
-0.220356114, -0.028975333), EE_IPS = c(-0.03431002, 0.001669245, 
0.09201166, 0.208916442, 0.110171018, 0.03663178, 0.089612391, 
0.159640608, 0.163035739, 0.147649732, 0.153849192, 0.190055346, 
0.118107136, 0.093154607, 0.069032847, 0.146669843, 0.051863027, 
-0.008001681, -0.074899263, 0.072004298, -0.021373533, 0.154438134, 
0.203416646, 0.076834382, 0.036060689, 0.073120387, -0.001967532, 
0.023979076, 0.112259557), IDE_CTL = c(0.086316453, 0.035622841, 
0.197174426, -0.01221096, 0.092125637, -0.02291759, 0.076552541, 
0.032277745, -0.159363676, 0.020679966, 0.027630691, -0.104554525, 
0.025839629, -0.066628563, -0.029989767, -0.067146898, -0.096956369, 
-0.055838223, -0.077961655, -0.234435848, -0.089016147, -0.057560032, 
0.218897294, -0.048055033, -0.100986924, -0.002898129, 0.055950756, 
0.094685466, 0.077026467), NJ_CTL = c(0.087341049, 0.078258341, 
-0.145443571, 0.099001429, 0.056017821, 0.237038014, 0.228125169, 
0.242217212, 0.254890685, 0.249001734, 0.075247769, 0.18266451, 
0.254584463, 0.243139445, 0.21194206, 0.255043734, 0.27611399, 
0.257633028, 0.379179981, 0.439291647, 0.341044083, 0.208639845, 
0.438651047, 0.278740343, 0.353818916, 0.187978416, 0.373015926, 
0.145119621, 0.105211174), MT_CTL = c(0.289573679, 0.191504269, 
-0.134157256, 0.151676387, 0.111408578, 0.318357643, 0.156006988, 
0.293424573, 0.356500971, 0.230921309, 0.095342241, 0.275790393, 
0.257405965, 0.319343677, 0.356109771, 0.381602249, 0.441937343, 
0.386821749, 0.536001298, 0.510683437, 0.422702953, 0.259304525, 
0.309314977, 0.282308365, 0.422194844, 0.28809796, 0.48930884, 
0.312781694, 0.208746091), PL_CTL = c(-0.061072672, -0.113638273, 
0.161438635, -0.229285951, -0.127615022, -0.411693567, -0.193691674, 
-0.249022939, -0.333843743, -0.286951187, -0.248558064, -0.422782995, 
-0.327202512, -0.272963262, -0.273245639, -0.333319536, -0.308906581, 
-0.302695213, -0.194280195, -0.373171469, -0.377414533, -0.353480179, 
-0.22533315, -0.436804628, -0.309529499, -0.247123883, -0.314427815, 
-0.110453335, -0.149534513), RV_CTL = c(-0.297711414, -0.210643311, 
0.174150908, -0.17770566, -0.097404414, -0.339139708, -0.16070138, 
-0.260629516, -0.337206292, -0.208289039, -0.170257162, -0.371452398, 
-0.269868313, -0.328218504, -0.389625422, -0.426088351, -0.481858373, 
-0.434352224, -0.51457102, -0.428113671, -0.382110335, -0.307212941, 
-0.309830596, -0.319455091, -0.323443161, -0.237769563, -0.526367223, 
-0.262536839, -0.146268311), EE_CTL = c(0.21884883, 0.083436159, 
0.078163322, 0.404016003, 0.083126709, 0.135063571, -0.088363007, 
0.211161069, 0.385397614, 0.085790816, 0.104104597, 0.331018292, 
0.139475365, 0.30045014, 0.247292938, 0.275847469, 0.301464011, 
0.282380559, 0.280415049, 0.262206676, 0.269523867, 0.278483356, 
0.005159098, 0.258376541, 0.299923624, 0.250780329, 0.46768759, 
0.253026387, 0.072825582)), class = c("spec_tbl_df", "tbl_df", 
"tbl", "data.frame"), row.names = c(NA, -29L), spec = structure(list(
    cols = list(Electrode = structure(list(), class = c("collector_character", 
    "collector")), IDE_CEN = structure(list(), class = c("collector_double", 
    "collector")), NJ_CEN = structure(list(), class = c("collector_double", 
    "collector")), MT_CEN = structure(list(), class = c("collector_double", 
    "collector")), PL_CEN = structure(list(), class = c("collector_double", 
    "collector")), RV_CEN = structure(list(), class = c("collector_double", 
    "collector")), EE_CEN = structure(list(), class = c("collector_double", 
    "collector")), IDE_IPS = structure(list(), class = c("collector_double", 
    "collector")), NJ_IPS = structure(list(), class = c("collector_double", 
    "collector")), MT_IPS = structure(list(), class = c("collector_double", 
    "collector")), PL_IPS = structure(list(), class = c("collector_double", 
    "collector")), RV_IPS = structure(list(), class = c("collector_double", 
    "collector")), EE_IPS = structure(list(), class = c("collector_double", 
    "collector")), IDE_CTL = structure(list(), class = c("collector_double", 
    "collector")), NJ_CTL = structure(list(), class = c("collector_double", 
    "collector")), MT_CTL = structure(list(), class = c("collector_double", 
    "collector")), PL_CTL = structure(list(), class = c("collector_double", 
    "collector")), RV_CTL = structure(list(), class = c("collector_double", 
    "collector")), EE_CTL = structure(list(), class = c("collector_double", 
    "collector"))), default = structure(list(), class = c("collector_guess", 
    "collector")), skip = 1), class = "col_spec"))

heatmap(data10)

我们可以使用 pivot_longer 转换为 'long' 格式,然后使用 ggplot2

中的 geom_tile
library(dplyr)
library(tidyr)
library(ggplot2)
data10 %>%
    pivot_longer(cols = -Electrode) %>% 
    ggplot(aes(x = name, y = Electrode, fill = value)) + 
      geom_tile() 

或使用heatmap

heatmap(`row.names<-`(as.matrix(data10[,-1]), data10[[1]]))

-输出