【问题标题】:Obtain Specific Column From Correlation Heatmap从相关热图中获取特定列
【发布时间】:2020-05-03 05:25:53
【问题描述】:

我有一个名为 allDataNoNAs 的数据集,它有 19 列用于不同的变量。

首先,使用包:

library(corrplot)
library(corrgram)
library(GGally)

来自 dput(cor(allDataNoNAs) - 我的样本相关性

structure(c(1, 0.116349634765185, 0.547691763989625, 0.291991636906379, 
0.52347996305183, 0.497643100595069, 0.0129815335193983, 0.418358158731718, 
0.471373794854162, 0.505419557447448, 0.276128001065287, 0.114921357444725, 
0.483335903285957, 0.0322484793148408, 0.360658177617753, 0.163989166178892, 
0.145358618474009, 0.549222657694447, 0.0283182668409127, 0.116349634765185, 
1, 0.542678597132992, 0.228195095236888, 0.341733815370385, 0.449234592784623, 
0.040928188236085, 0.306532564182676, 0.246214540314882, 0.368735099181333, 
0.0974107116463065, 0.118633970020044, 0.0663374870504325, 0.00324065971750887, 
0.429993810524071, 0.0660128392326907, -0.208834964557656, 0.517351517191311, 
0.00340750071414792, 0.547691763989625, 0.542678597132992, 1, 
0.503509567685111, 0.834074832294578, 0.87458120333133, 0.11646402536793, 
0.709723789822138, 0.545685105436571, 0.691116703644981, 0.251055925294139, 
0.137145560677364, 0.677547477041307, 0.0138408591129587, 0.574449939471671, 
0.289088705565296, -0.0151310469001056, 0.995636799856898, 0.00806307965229721, 
0.291991636906379, 0.228195095236888, 0.503509567685111, 1, 0.5928306942291, 
0.419860437848609, 0.202947501799892, 0.600369342626932, 0.3036531414462, 
0.31218278418869, 0.0665676462597262, 0.0706549436236251, 0.463190217918095, 
0.017439704947323, 0.20361820902537, 0.563054610829996, 0.367022482937022, 
0.539278002253207, 0.0146950545295136, 0.52347996305183, 0.341733815370385, 
0.834074832294578, 0.5928306942291, 1, 0.877884027429435, 0.249913906532112, 
0.770346073267575, 0.581478562237408, 0.62684315599784, 0.158950811299692, 
0.0709795609883571, 0.707727230043996, 0.0374999988906861, 0.36979003972634, 
0.532230871495189, 0.237891979696682, 0.868052149324532, 0.0301272383779361, 
0.497643100595069, 0.449234592784623, 0.87458120333133, 0.419860437848609, 
0.877884027429435, 1, 0.0578337272432955, 0.625271696806798, 
0.642882384190134, 0.742158234646655, 0.18412573265697, 0.0846354163480033, 
0.636899685921357, 0.00136017420567482, 0.442530075276962, 0.166101818463978, 
-0.122330359121607, 0.870582759035652, -0.00536057317986459, 
0.0129815335193983, 0.040928188236085, 0.11646402536793, 0.202947501799892, 
0.249913906532112, 0.0578337272432955, 1, 0.168170227241747, 
0.0103942343836554, 0.0146416101891029, 0.0274638568337838, 0.0232209281980358, 
0.438976017479895, 0.00664290788845518, 0.0558346558356874, 0.576321333713829, 
0.205483416691572, 0.160939456560856, 0.00633413505889225, 0.418358158731718, 
0.306532564182676, 0.709723789822138, 0.600369342626932, 0.770346073267575, 
0.625271696806798, 0.168170227241747, 1, 0.421695218774506, 0.481156860252289, 
0.109952341757847, 0.0400601095104961, 0.560225169205313, 0.0470119529030615, 
0.311744196849895, 0.445382213345548, 0.237447342653341, 0.743416109744227, 
0.0437634515476897, 0.471373794854162, 0.246214540314882, 0.545685105436571, 
0.3036531414462, 0.581478562237408, 0.642882384190134, 0.0103942343836554, 
0.421695218774506, 1, 0.809375500184827, 0.201944501698817, 0.098871956246993, 
0.46496436444905, -0.00410066612855966, 0.34093890132072, 0.0955588133868073, 
-0.0561387410393148, 0.542950578488189, -0.00611403179202383, 
0.505419557447448, 0.368735099181333, 0.691116703644981, 0.31218278418869, 
0.62684315599784, 0.742158234646655, 0.0146416101891029, 0.481156860252289, 
0.809375500184827, 1, 0.166272569833104, 0.0642480288154233, 
0.493094322495752, -0.0143825404077684, 0.420509020130084, 0.0763222806834054, 
-0.137267266981321, 0.675599964220607, -0.0155210421858565, 0.276128001065287, 
0.0974107116463065, 0.251055925294139, 0.0665676462597262, 0.158950811299692, 
0.18412573265697, 0.0274638568337838, 0.109952341757847, 0.201944501698817, 
0.166272569833104, 1, 0.803405447808051, 0.209386276142885, 0.019611871344881, 
0.698294870666248, 0.024793538949468, 0.00921044459805193, 0.243573446480239, 
0.0182042685108301, 0.114921357444725, 0.118633970020044, 0.137145560677364, 
0.0706549436236251, 0.0709795609883571, 0.0846354163480033, 0.0232209281980358, 
0.0400601095104961, 0.098871956246993, 0.0642480288154233, 0.803405447808051, 
1, 0.0518698024423593, 0.0195654257050434, 0.534756730460756, 
0.00851489725348713, -0.00157091125920201, 0.131294046914676, 
0.0196406046872536, 0.483335903285957, 0.0663374870504325, 0.677547477041307, 
0.463190217918095, 0.707727230043996, 0.636899685921357, 0.438976017479895, 
0.560225169205313, 0.46496436444905, 0.493094322495752, 0.209386276142885, 
0.0518698024423593, 1, 0.00595760440442105, 0.332127234258051, 
0.402991372365854, 0.130619402830307, 0.702714128886842, 0.000759081836999778, 
0.0322484793148408, 0.00324065971750887, 0.0138408591129587, 
0.017439704947323, 0.0374999988906861, 0.00136017420567482, 0.00664290788845518, 
0.0470119529030615, -0.00410066612855966, -0.0143825404077684, 
0.019611871344881, 0.0195654257050434, 0.00595760440442105, 1, 
0.0240839070381978, 0.0543455541899934, 0.121224926189405, 0.0181415673103803, 
0.999560527964641, 0.360658177617753, 0.429993810524071, 0.574449939471671, 
0.20361820902537, 0.36979003972634, 0.442530075276962, 0.0558346558356874, 
0.311744196849895, 0.34093890132072, 0.420509020130084, 0.698294870666248, 
0.534756730460756, 0.332127234258051, 0.0240839070381978, 1, 
0.101917219961389, -0.0673808764564209, 0.55786516587572, 0.0226512629105265, 
0.163989166178892, 0.0660128392326907, 0.289088705565296, 0.563054610829996, 
0.532230871495189, 0.166101818463978, 0.576321333713829, 0.445382213345548, 
0.0955588133868073, 0.0763222806834054, 0.024793538949468, 0.00851489725348713, 
0.402991372365854, 0.0543455541899934, 0.101917219961389, 1, 
0.562085375561417, 0.360237027957389, 0.0519977244267395, 0.145358618474009, 
-0.208834964557656, -0.0151310469001056, 0.367022482937022, 0.237891979696682, 
-0.122330359121607, 0.205483416691572, 0.237447342653341, -0.0561387410393148, 
-0.137267266981321, 0.00921044459805193, -0.00157091125920201, 
0.130619402830307, 0.121224926189405, -0.0673808764564209, 0.562085375561417, 
1, 0.041068964081757, 0.119487910165712, 0.549222657694447, 0.517351517191311, 
0.995636799856898, 0.539278002253207, 0.868052149324532, 0.870582759035652, 
0.160939456560856, 0.743416109744227, 0.542950578488189, 0.675599964220607, 
0.243573446480239, 0.131294046914676, 0.702714128886842, 0.0181415673103803, 
0.55786516587572, 0.360237027957389, 0.041068964081757, 1, 0.0121897372730556, 
0.0283182668409127, 0.00340750071414792, 0.00806307965229721, 
0.0146950545295136, 0.0301272383779361, -0.00536057317986459, 
0.00633413505889225, 0.0437634515476897, -0.00611403179202383, 
-0.0155210421858565, 0.0182042685108301, 0.0196406046872536, 
0.000759081836999778, 0.999560527964641, 0.0226512629105265, 
0.0519977244267395, 0.119487910165712, 0.0121897372730556, 1), .Dim = c(19L, 
19L), .Dimnames = list(c("RPE", "Duration", "Distance", "Max Speed", 
"HML Distance", "HML Efforts", "Sprint Distance", "Sprints", 
"Accelerations", "Decelerations", "Average Heart Rate", "Max Heart Rate", 
"Average Metabolic Power", "Dynamic Stress Load", "Heart Rate Exertion", 
"High Speed Running (Relative)", "HML Density", "Speed Intensity", 
"Impacts"), c("RPE", "Duration", "Distance", "Max Speed", "HML Distance", 
"HML Efforts", "Sprint Distance", "Sprints", "Accelerations", 
"Decelerations", "Average Heart Rate", "Max Heart Rate", "Average Metabolic Power", 
"Dynamic Stress Load", "Heart Rate Exertion", "High Speed Running (Relative)", 
"HML Density", "Speed Intensity", "Impacts")))

使用上面的相关性数据,我试图只获得第一列,在那里我看到 RPE 和所有其他 18 个变量之间的相关性。我可以通过cor(allDataNoNAs)[,1] 来做到这一点,但是当我尝试使用corrplot(corrgram(allDataNoNAs))[,1] 将其绘制为相关图时,它会绘制所有 19x19 的相关性并且是一团糟,而我只需要 RPE 相关性列。

这样使用ggcorr()

ggcorr(allDataNoNAs, method = c("everything"), label = TRUE,label_size = 2, label_round = 4)

我获得了我想要的更清晰的热图。但是,将data 参数切换为allDataNoNAs[,1]cor(allDataNoNAs)[,1] 并不能只获得一个RPE 相关列。

是否可以只返回相关热图的一列?

【问题讨论】:

    标签: r


    【解决方案1】:

    我能够弄清楚并回答我自己的问题,虽然不完全是我想要的(从 ggcorr() 想要它),但是这个版本就足够了:

    我的变量名和以前一样

    #x is the variable you want to be comparing the y variables with
    myCorDF <- cor(x = allDataNoNAs$RPE, y = allDataNoNAs[2:19], use = "everything")
    
    #just changing it to colors that seem better to me
    col2 <- colorRampPalette(c("#00007F", "blue", "#007FFF", "cyan", "white",
                               "yellow", "#FF7F00", "red", "#7F0000"))
    
    #this is how I obtain the one column for RPE correlation against other all variables
    corrplot(myCorDF, tl.srt = 45, method = "color", addCoef.col = "black", 
             cl.cex = 0.56, col = col2(50))
    

    删除我的颜色的通用代码如下所示:

    corDF <- cor(x = DF$x, y = DF[2:5], use = "everything")
    
    corrplot(corDF, tl.srt = 45, method = "color", addCoef.col = "black", 
             cl.cex = 0.56)
    

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 2019-06-30
      • 2020-04-29
      • 1970-01-01
      • 2020-05-15
      • 1970-01-01
      • 2013-01-23
      • 2021-06-08
      相关资源
      最近更新 更多