Error in correlation plot display in R
1
0
Entering edit mode
5.4 years ago
sonayuv • 0

I used the following code to get a correlogram of 326 genes. I have the pairwise correlation values. I wanted to represent them in a plot.

> library(readxl)
> pears <- read_excel("PATH/pears.xlsx")
> View(pears)
> library(corrplot)
corrplot 0.84 loaded
> corrplot(pears, method="circle")
Error in matrix(if (is.null(value)) logical() else value, nrow = nr, dimnames = list(rn,  : 
  length of 'dimnames' [2] not equal to array extent

Please help to resolve.

R gene • 9.2k views
ADD COMMENT
0
Entering edit mode

What is the output of

pears[1:5, 1:5]

str(pears)
ADD REPLY
0
Entering edit mode
pears[1:5, 1:5]
# A tibble: 5 x 5
  cols    CRKL TGFBR2  MID1 SFTPA2
  <chr>  <dbl>  <dbl> <dbl>  <dbl>
1 CRKL   0      1.34  0.801  1.39 
2 TGFBR2 1.34   0     1.44   0.443
3 MID1   0.801  1.44  0      1.09 
4 SFTPA2 1.39   0.443 1.09   0    
5 EGFR   1.03   1.67  0.610  1.55 


str(pears)
Classes ‘tbl_df’, ‘tbl’ and 'data.frame':   326 obs. of  327 variables:
 $ cols    : chr  "CRKL" "TGFBR2" "MID1" "SFTPA2" ...
 $ CRKL    : num  0 1.336 0.801 1.385 1.032 ...
 $ TGFBR2  : num  1.336 0 1.444 0.443 1.674 ...
 $ MID1    : num  0.801 1.444 0 1.088 0.61 ...
 $ SFTPA2  : num  1.385 0.443 1.088 0 1.548 ...
ADD REPLY
1
Entering edit mode

Hi @sonayuv,

If you have a square (symmetric) correlation matrix, where columns and rows correspond to some correlation metric that you have used, the problem is related with your input to the function "corrplot()".

When you import data from excel with "read_excel" function, your data belongs to the tibble class. Well the function "corrplot()" requires data matrix, not a tibble. Check the class of your data with "class(pears)". So, you have to convert your tibble data frame into a matrix in order to you use the function corrplot.

pears_mtx <- as.matrix(pears) # convert the tibble data frame into a matrix

you may lose your row names in the last code, so restore them follow the next code

names_rows <- colnames(pears) # this assumes that you have a square/symetric metric here the name of the first column is the same as the first row, and so on

restore the row names

rownames(pears_mtx) <- names_rows # substitute your matrix row names by your column names

Plot your correlograma

corrplot(pears_mtx)

I hope this helps! Sincerely, António

ADD REPLY
0
Entering edit mode

The previous solution presented by @Kevin Blighe is much more elegant!

António

ADD REPLY
0
Entering edit mode

Obrigado / thanks, António. In your answer, you also explain the reason why the user's code did not work (due to it being a tibble); so, that is good.

ADD REPLY
0
Entering edit mode

Hi @Antonio, Thank you. Running the above mentioned statement gave me the following error.

rownames(pears_mtx) <- names_rows

Error in dimnames(x) <- dn : length of 'dimnames' [1] not equal to array extent

Please assist.

ADD REPLY
0
Entering edit mode

Does my code work? (see below)

ADD REPLY
0
Entering edit mode

Which is the result of:

dim(pears)

I think you may not have a symmetric table. You still might plot your correlogram without running that command. Although I think your table is not symmetric and you may face some problems.

Try the solution proposed by @Kevin Blighe. It may solve your problems!

António

ADD REPLY
0
Entering edit mode

rownames(pears_mtx) <- names_rows Error in dimnames(x) <- dn : length of 'dimnames' [1] not equal to array extent dim(pears) [1] 326 327

The table is not symmetric it seems. But there are 326 genes names on the first row and column with A1 cell having "genename" in it.

ADD REPLY
0
Entering edit mode

Can you do print of the header of your table?

Try:

head(pears)

I think that your column names are the first row. If they are in the first row, you need to exclude them by using:

pears <- pears[-1,]

This last command will exclude the first row of pears.

I hope that this helps!

ADD REPLY
2
Entering edit mode
5.4 years ago

Oh, you just need to do this:

corrplot(data.matrix(pears), method="circle")
ADD COMMENT

Login before adding your answer.

Traffic: 1637 users visited in the last hour
Help About
FAQ
Access RSS
API
Stats

Use of this site constitutes acceptance of our User Agreement and Privacy Policy.

Powered by the version 2.3.6