How to use Corrplot with correlation matrix created by hand (of type list)

Total
14
Shares

I used a for loop to create a correlation matrix, because I needed to use polychor to generate polychoric correaltions and I was only able to get polychor to correlate two variables at a time. Anyway, I created my own correlation table with the following code:



    for(i in 1:ncol(gd2)) {
        for (j in 1:ncol(gd2)) {
            corVal 

The table looks like this:



     head(dtnew)
       Better Afraid Alive Bored Drop Empty Energy Happy Help Home Hope Memory Satis Spirit Worth  TOT
    1:   1.00   0.32  0.29  0.39 0.36  0.46   0.25  0.43 0.39 0.13 0.46   0.39  0.50   0.45  0.48 0.67
    2:   0.32   1.00  0.25  0.20 0.24  0.30   0.23  0.30 0.43 0.15 0.44   0.28  0.31   0.29  0.34 0.62
    3:   0.29   0.25  1.00  0.26 0.28  0.46   0.38  0.60 0.35 0.19 0.41   0.10  0.49   0.53  0.43 0.65
    4:   0.39   0.20  0.26  1.00 0.36  0.56   0.31  0.36 0.39 0.16 0.32   0.23  0.39   0.35  0.44 0.67
    5:   0.36   0.24  0.28  0.36 1.00  0.44   0.41  0.37 0.43 0.31 0.35   0.22  0.42   0.37  0.40 0.72
    6:   0.46   0.30  0.46  0.56 0.44  1.00   0.32  0.55 0.51 0.18 0.45   0.17  0.62   0.52  0.64 0.75
    > 

But longer.

Here is the dput()

structure(list(Better = c(1, 0.32, 0.29, 0.39, 0.36, 0.46, 0.25, 
0.43, 0.39, 0.13, 0.46, 0.39, 0.5, 0.45, 0.48, 0.67), Afraid = c(0.32, 
1, 0.25, 0.2, 0.24, 0.3, 0.23, 0.3, 0.43, 0.15, 0.44, 0.28, 0.31, 
0.29, 0.34, 0.62), Alive = c(0.29, 0.25, 1, 0.26, 0.28, 0.46, 
0.38, 0.6, 0.35, 0.19, 0.41, 0.1, 0.49, 0.53, 0.43, 0.65), Bored = c(0.39, 
0.2, 0.26, 1, 0.36, 0.56, 0.31, 0.36, 0.39, 0.16, 0.32, 0.23, 
0.39, 0.35, 0.44, 0.67), Drop = c(0.36, 0.24, 0.28, 0.36, 1, 
0.44, 0.41, 0.37, 0.43, 0.31, 0.35, 0.22, 0.42, 0.37, 0.4, 0.72
), Empty = c(0.46, 0.3, 0.46, 0.56, 0.44, 1, 0.32, 0.55, 0.51, 
0.18, 0.45, 0.17, 0.62, 0.52, 0.64, 0.75), Energy = c(0.25, 0.23, 
0.38, 0.31, 0.41, 0.32, 1, 0.48, 0.37, 0.36, 0.31, 0.14, 0.4, 
0.43, 0.38, 0.74), Happy = c(0.43, 0.3, 0.6, 0.36, 0.37, 0.55, 
0.48, 1, 0.45, 0.21, 0.49, 0.22, 0.69, 0.84, 0.49, 0.8), Help = c(0.39, 
0.43, 0.35, 0.39, 0.43, 0.51, 0.37, 0.45, 1, 0.2, 0.51, 0.32, 
0.5, 0.44, 0.6, 0.73), Home = c(0.13, 0.15, 0.19, 0.16, 0.31, 
0.18, 0.36, 0.21, 0.2, 1, 0.23, 0.13, 0.13, 0.15, 0.26, 0.63), 
    Hope = c(0.46, 0.44, 0.41, 0.32, 0.35, 0.45, 0.31, 0.49, 
    0.51, 0.23, 1, 0.38, 0.48, 0.47, 0.59, 0.73), Memory = c(0.39, 
    0.28, 0.1, 0.23, 0.22, 0.17, 0.14, 0.22, 0.32, 0.13, 0.38, 
    1, 0.25, 0.24, 0.31, 0.66), Satis = c(0.5, 0.31, 0.49, 0.39, 
    0.42, 0.62, 0.4, 0.69, 0.5, 0.13, 0.48, 0.25, 1, 0.66, 0.6, 
    0.78), Spirit = c(0.45, 0.29, 0.53, 0.35, 0.37, 0.52, 0.43, 
    0.84, 0.44, 0.15, 0.47, 0.24, 0.66, 1, 0.51, 0.77), Worth = c(0.48, 
    0.34, 0.43, 0.44, 0.4, 0.64, 0.38, 0.49, 0.6, 0.26, 0.59, 
    0.31, 0.6, 0.51, 1, 0.77), TOT = c(0.67, 0.62, 0.65, 0.67, 
    0.72, 0.75, 0.74, 0.8, 0.73, 0.63, 0.73, 0.66, 0.78, 0.77, 
    0.77, 0.89)), row.names = c(NA, -16L), class = c("data.table", 
"data.frame"), .internal.selfref = <pointer: 0x000001d7adc21ef0>)

</pre/>

I would like to generate a visual using corrplot. However, when I try, I get an error:
Error in is.finite(tmp) : default method not implemented for type ‘list’

My data is indeed of type list. I have tried usuing ‘unlist’. Not sure what else to try.


Solution

There is a problem with your dput() output, possibly because you have a data.table. I can read it by deleting ", .internal.selfref = <pointer: 0x000001d7adc21ef0>" from the last line so that it ends class = c("data.table", "data.frame")). Printing that out shows a problem with the last line/column (Tot). The bottom row in that column should be 1.00, but it is 0.89. We can trim that and use as.matrix (my mistake in the earlier comment) to convert the data frame:

gd3 <- gd2[-16, -16]
corrplot(as.matrix(gd3))

enter image description here

Leave a Reply

Your email address will not be published. Required fields are marked *