rcorrelationpairwiser-corrplotggcorrplot

How to merge two matrices into one with "corrplot"?


I have two matrices, one with the correlation coefficients between pairs of variables, and the other which shows the P-values for the same pairs:

    matrix_r <- structure(c(1, 0.137953684143088, 0.057024439117235, 0.301128814810929, 
    0.141547179615176, 0.396556225401372, -0.11863977378892, 0.276207586138419, 
    0.259640090601703, -0.317890631606822, 0.39383947598779, 0.337544484143242, 
    0.462625749535307, 0.343728297005152, 0.419037914458768, 0.334450530149522, 
    0.137953684143088, 1, -0.025214277445333, -0.109726372575931, 
    -0.0655321323274804, 0.0170670884375245, -0.093306860778893, 
    -0.00534703948153741, 0.347245723356623, -0.131813564135445, 
    0.385508413813477, 0.214355183883235, 0.0502183512863292, 0.00901781806968087, 
    0.124101926527097, 0.0302589056556792, 0.057024439117235, -0.025214277445333, 
    1, 0.0513877769983156, 0.049100303890413, 0.447322696809423, 
    0.324643974060803, 0.715868789372832, 0.170230823233716, -0.096377537692442, 
    0.271615024862277, -0.313671559003813, 0.55848707237266, 0.642100119786812, 
    0.342275924527041, 0.408726270388249, 0.301128814810929, -0.109726372575931, 
    0.0513877769983156, 1, 0.258700939498519, 0.607156878113643, 
    0.103312836266844, 0.172652770486034, 0.273237670072016, -0.133569397639734, 
    0.0801599387027875, 0.00800720401313832, 0.302423016907157, 0.185579766867049, 
    0.379938765382931, 0.345737441339468, 0.141547179615176, -0.0655321323274804, 
    0.049100303890413, 0.258700939498519, 1, 0.320458937786398, 0.350508402691638, 
    0.152234792676687, 0.289573867472253, 0.205145262530466, 0.0718773984163691, 
    -0.0436429111615801, 0.256769983030438, 0.159161240449872, 0.224924391309589, 
    0.483163913220374, 0.396556225401372, 0.0170670884375245, 0.447322696809423, 
    0.607156878113643, 0.320458937786398, 1, 0.120806406989441, 0.54942132926055, 
    0.483599127206601, -0.218508542434727, 0.336304082550851, -0.179574191591623, 
    0.717653765647, 0.542916025055907, 0.717747435492964, 0.643841155886226, 
    -0.11863977378892, -0.093306860778893, 0.324643974060803, 0.103312836266844, 
    0.350508402691638, 0.120806406989441, 1, 0.402078500454528, 0.137956701371863, 
    0.340977774076417, -0.0058850876631379, -0.226212482764356, 0.253858169675543, 
    0.336546580913081, 0.140729192029443, 0.557299437503565, 0.276207586138419, 
    -0.00534703948153741, 0.715868789372832, 0.172652770486034, 0.152234792676687, 
    0.54942132926055, 0.402078500454528, 1, 0.286168035601962, -0.168604766941589, 
    0.351096333516696, -0.158782223448302, 0.805119021742302, 0.95821208094472, 
    0.422742586946467, 0.771823173237525, 0.259640090601703, 0.347245723356623, 
    0.170230823233716, 0.273237670072016, 0.289573867472253, 0.483599127206601, 
    0.137956701371863, 0.286168035601962, 1, -0.161238039894227, 
    0.48703813683678, 0.140012737826813, 0.44974586853201, 0.306081505872618, 
    0.642109453055911, 0.546057934954125, -0.317890631606822, -0.131813564135445, 
    -0.096377537692442, -0.133569397639734, 0.205145262530466, -0.218508542434727, 
    0.340977774076417, -0.168604766941589, -0.161238039894227, 1, 
    -0.140665289961922, -0.0922998814991519, -0.305067915633029, 
    -0.241254040206962, -0.118619663190881, 0.0482390516599434, 0.39383947598779, 
    0.385508413813477, 0.271615024862277, 0.0801599387027875, 0.0718773984163691, 
    0.336304082550851, -0.0058850876631379, 0.351096333516696, 0.48703813683678, 
    -0.140665289961922, 1, 0.278817137279128, 0.411971337632543, 
    0.36427506584873, 0.59480959495125, 0.491809784143451, 0.337544484143242, 
    0.214355183883235, -0.313671559003813, 0.00800720401313832, -0.0436429111615801, 
    -0.179574191591623, -0.226212482764356, -0.158782223448302, 0.140012737826813, 
    -0.0922998814991519, 0.278817137279128, 1, -0.0620951668602389, 
    -0.0937070904481953, 0.0624156757462784, 0.126132529840603, 0.462625749535307, 
    0.0502183512863292, 0.55848707237266, 0.302423016907157, 0.256769983030438, 
    0.717653765647, 0.253858169675543, 0.805119021742302, 0.44974586853201, 
    -0.305067915633029, 0.411971337632543, -0.0620951668602389, 1, 
    0.802714344425467, 0.642749164620842, 0.789578978247098, 0.343728297005152, 
    0.00901781806968087, 0.642100119786812, 0.185579766867049, 0.159161240449872, 
    0.542916025055907, 0.336546580913081, 0.95821208094472, 0.306081505872618, 
    -0.241254040206962, 0.36427506584873, -0.0937070904481953, 0.802714344425467, 
    1, 0.457116950439421, 0.759734244879995, 0.419037914458768, 0.124101926527097, 
    0.342275924527041, 0.379938765382931, 0.224924391309589, 0.717747435492964, 
    0.140729192029443, 0.422742586946467, 0.642109453055911, -0.118619663190881, 
    0.59480959495125, 0.0624156757462784, 0.642749164620842, 0.457116950439421, 
    1, 0.68729389636416, 0.334450530149522, 0.0302589056556792, 0.408726270388249, 
    0.345737441339468, 0.483163913220374, 0.643841155886226, 0.557299437503565, 
    0.771823173237525, 0.546057934954125, 0.0482390516599434, 0.491809784143451, 
    0.126132529840603, 0.789578978247098, 0.759734244879995, 0.68729389636416, 
    1), dim = c(16L, 16L), dimnames = list(c("Variable_1", "Variable_2", 
    "Variable_3", "Variable_4", "Variable_5", "Variable_6", "Variable_7", 
    "Variable_8", "Variable_9", "Variable_10", "Variable_11", "Variable_12", 
    "Variable_13", "Variable_14", "Variable_15", "Variable_16"), 
        c("Variable_1", "Variable_2", "Variable_3", "Variable_4", 
        "Variable_5", "Variable_6", "Variable_7", "Variable_8", "Variable_9", 
        "Variable_10", "Variable_11", "Variable_12", "Variable_13", 
        "Variable_14", "Variable_15", "Variable_16")))
    matrix_p <- structure(c(NA, 0.610401692430066, 0.83385145927471, 0.257060786019344, 
    0.60103954319793, 0.128333276810624, 0.661665285667016, 0.300421002973681, 
    0.331494796597279, 0.230196318201416, 0.131209359182311, 0.201044411963585, 
    0.071171296717929, 0.192392132852606, 0.106188115640226, 0.205466445293426, 
    0.610401692430066, NA, 0.926150108004369, 0.685824724771699, 
    0.80945937740495, 0.94997790877084, 0.73106958386101, 0.984320076731099, 
    0.187580769569768, 0.626529696169394, 0.140304695175509, 0.425334437079021, 
    0.853468798156201, 0.973558492411887, 0.647011735901109, 0.911424487098764, 
    0.83385145927471, 0.926150108004369, NA, 0.850092088867308, 0.856699385146588, 
    0.0823340349727044, 0.219894531130051, 0.00181647444896171, 0.528488051265182, 
    0.722539710519669, 0.308855954651733, 0.236784189509745, 0.0245410684198566, 
    0.00732212451859993, 0.194402047320553, 0.115983063393008, 0.257060786019344, 
    0.685824724771699, 0.850092088867308, NA, 0.333309508209275, 
    0.01262181346084, 0.703388172780045, 0.522549485605328, 0.305859991093328, 
    0.621901096431033, 0.76791369253449, 0.976520946717608, 0.254920637958809, 
    0.491377991362694, 0.146619779076181, 0.189634139063326, 0.60103954319793, 
    0.80945937740495, 0.856699385146588, 0.333309508209275, NA, 0.226243287135776, 
    0.183188965382847, 0.573543110169541, 0.276657497141424, 0.445953145332781, 
    0.791372332924968, 0.872498216405837, 0.337058510628132, 0.556011936980776, 
    0.402296401737726, 0.057974977638823, 0.128333276810624, 0.94997790877084, 
    0.0823340349727044, 0.01262181346084, 0.226243287135776, NA, 
    0.655838627354961, 0.0274899207585964, 0.0577164570492095, 0.416200876128423, 
    0.202809788730217, 0.505748177444951, 0.00174727914068251, 0.0297675004772866, 
    0.00174370805928215, 0.00711412019880764, 0.661665285667016, 
    0.73106958386101, 0.219894531130051, 0.703388172780045, 0.183188965382847, 
    0.655838627354961, NA, 0.122621801727314, 0.610393807576561, 
    0.196210069076271, 0.982742486192168, 0.399535025557259, 0.342757155243396, 
    0.202463872312075, 0.603165581891898, 0.0249128851329949, 0.300421002973681, 
    0.984320076731099, 0.00181647444896171, 0.522549485605328, 0.573543110169541, 
    0.0274899207585964, 0.122621801727314, NA, 0.282601302924547, 
    0.532492147049088, 0.18240482452661, 0.556965204209428, 0.00016804492211131, 
    5.34131872065302e-09, 0.102815725308818, 0.000460782672703441, 
    0.331494796597279, 0.187580769569768, 0.528488051265182, 0.305859991093328, 
    0.276657497141424, 0.0577164570492095, 0.610393807576561, 0.282601302924547, 
    NA, 0.55080116674986, 0.055703455024714, 0.605030187910911, 0.0804876350424679, 
    0.248930489301181, 0.00732099672478936, 0.0286502020853634, 0.230196318201416, 
    0.626529696169394, 0.722539710519669, 0.621901096431033, 0.445953145332781, 
    0.416200876128423, 0.196210069076271, 0.532492147049088, 0.55080116674986, 
    NA, 0.603331796759718, 0.73387331541807, 0.250581239460207, 0.368047848000699, 
    0.661719454427133, 0.859189422590328, 0.131209359182311, 0.140304695175509, 
    0.308855954651733, 0.76791369253449, 0.791372332924968, 0.202809788730217, 
    0.982742486192168, 0.18240482452661, 0.055703455024714, 0.603331796759718, 
    NA, 0.295689554504886, 0.112835226622433, 0.165405556631901, 
    0.0150840821155644, 0.0529969409746442, 0.201044411963585, 0.425334437079021, 
    0.236784189509745, 0.976520946717608, 0.872498216405837, 0.505748177444951, 
    0.399535025557259, 0.556965204209428, 0.605030187910911, 0.73387331541807, 
    0.295689554504886, NA, 0.819294850462861, 0.729956108095266, 
    0.818376567475086, 0.64159465231427, 0.071171296717929, 0.853468798156201, 
    0.0245410684198566, 0.254920637958809, 0.337058510628132, 0.00174727914068251, 
    0.342757155243396, 0.00016804492211131, 0.0804876350424679, 0.250581239460207, 
    0.112835226622433, 0.819294850462861, NA, 0.000181858818917524, 
    0.00724402484525766, 0.000275038564024221, 0.192392132852606, 
    0.973558492411887, 0.00732212451859993, 0.491377991362694, 0.556011936980776, 
    0.0297675004772866, 0.202463872312075, 5.34131872065302e-09, 
    0.248930489301181, 0.368047848000699, 0.165405556631901, 0.729956108095266, 
    0.000181858818917524, NA, 0.0750545629341222, 0.000638622575951597, 
    0.106188115640226, 0.647011735901109, 0.194402047320553, 0.146619779076181, 
    0.402296401737726, 0.00174370805928215, 0.603165581891898, 0.102815725308818, 
    0.00732099672478936, 0.661719454427133, 0.0150840821155644, 0.818376567475086, 
    0.00724402484525766, 0.0750545629341222, NA, 0.00326303693716046, 
    0.205466445293426, 0.911424487098764, 0.115983063393008, 0.189634139063326, 
    0.057974977638823, 0.00711412019880764, 0.0249128851329949, 0.000460782672703441, 
    0.0286502020853634, 0.859189422590328, 0.0529969409746442, 0.64159465231427, 
    0.000275038564024221, 0.000638622575951597, 0.00326303693716046, 
    NA), dim = c(16L, 16L), dimnames = list(c("Variable_1", "Variable_2", 
    "Variable_3", "Variable_4", "Variable_5", "Variable_6", "Variable_7", 
    "Variable_8", "Variable_9", "Variable_10", "Variable_11", "Variable_12", 
    "Variable_13", "Variable_14", "Variable_15", "Variable_16"), 
        c("Variable_1", "Variable_2", "Variable_3", "Variable_4", 
        "Variable_5", "Variable_6", "Variable_7", "Variable_8", "Variable_9", 
        "Variable_10", "Variable_11", "Variable_12", "Variable_13", 
        "Variable_14", "Variable_15", "Variable_16")))

I intend to merge both matrices into one with the corrplot package:

library(corrplot)
corrplot(matrix_r, p.mat = matrix_p, method = "color", sig.level = 0.05)

However, not only did the matrix not result out as I expected, but the following error message also appeared:

Error in data.frame(..., check.names = FALSE): arguments imply differing number of rows: 256, 240

How can I fix this issue, and make a matrix with r and P values together using corrplot or ggcorrplot?


Solution

  • You can create empty matrix (16*16), assign its upper triangle to upper triangle of your matrix_p and assign its lower triangle to lower triangle of your correlation matrix (matrix_r).

    rp<-matrix(0,16,16)
    rp[upper.tri(rp)]<-matrix_p[upper.tri(matrix_p)]
    rp[lower.tri(rp)]<-matrix_r[lower.tri(matrix_r)]
    dimnames(rp)<-dimnames(matrix_p)
    

    Then you can plot the resulting matrix using corrplot() function where you have the r values in lower triangle of the plot and the p-values in the upper triangle of the plot:

    library(grDevices)
    library(corrplot)
    
    # set the color scale 
    col <- colorRampPalette(c("#BB4444", "#EE9988", "#FFFFFF", "#77AADD", "#4477AA"))
    

    plot merged rp matrix using corrplot:

        corrplot(rp,method = "color",  col = col(200), 
             type = "full", 
             addCoef.col = "black", # Add coefficient of correlation
             tl.col = "black", tl.srt = 90, tl.cex = 0.5,#Text label color and rotation
             tl.pos="d", number.cex=0.6,
             sig.level = 0.05, 
             diag = FALSE ,
             
    )
    

    the resulting plot with the white cells of the upper triangle indicates significant pvalues of the corresponding correlations of lower triangle matrix:

    enter image description here