How can I create a correlation matrix in R?

后端 未结 5 777
情深已故
情深已故 2020-11-27 10:24

I have 92 set of data of same type.

I want to make a correlation matrix for any two combination possible.

i.e. I want a matrix of 92 x92.

such that e

5条回答
  •  星月不相逢
    2020-11-27 10:54

    There are other ways to achieve this here: (Plot correlation matrix into a graph), but I like your version with the correlations in the boxes. Is there a way to add the variable names to the x and y column instead of just those index numbers? For me, that would make this a perfect solution. Thanks!

    edit: I was trying to comment on the post by [Marc in the box], but I clearly don't know what I'm doing. However, I did manage to answer this question for myself.

    if d is the matrix (or the original data frame) and the column names are what you want, then the following works:

    axis(1, 1:dim(d)[2], colnames(d), las=2)
    axis(2, 1:dim(d)[2], colnames(d), las=2)
    

    las=0 would flip the names back to their normal position, mine were long, so I used las=2 to make them perpendicular to the axis.

    edit2: to suppress the image() function printing numbers on the grid (otherwise they overlap your variable labels), add xaxt='n', e.g.:

    image(x=seq(dim(x)[2]), y=seq(dim(y)[2]), z=COR, col=rev(heat.colors(20)), xlab="x column", ylab="y column", xaxt='n')
    

提交回复
热议问题