Question: Error in correlation plot display in R
0
gravatar for sonayuv
3 months ago by
sonayuv0
sonayuv0 wrote:

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 • 307 views
ADD COMMENTlink modified 3 months ago • written 3 months ago by sonayuv0

What is the output of

pears[1:5, 1:5]

str(pears)
ADD REPLYlink written 3 months ago by Kevin Blighe50k
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 REPLYlink modified 3 months ago by Kevin Blighe50k • written 3 months ago by sonayuv0
1

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 REPLYlink modified 3 months ago by Kevin Blighe50k • written 3 months ago by antonioggsousa50

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

António

ADD REPLYlink written 3 months ago by antonioggsousa50

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 REPLYlink written 3 months ago by Kevin Blighe50k

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 REPLYlink written 3 months ago by sonayuv0

Does my code work? (see below)

ADD REPLYlink written 3 months ago by Kevin Blighe50k

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 REPLYlink written 3 months ago by antonioggsousa50

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 REPLYlink written 3 months ago by sonayuv0

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 REPLYlink written 3 months ago by antonioggsousa50
2
gravatar for Kevin Blighe
3 months ago by
Kevin Blighe50k
Kevin Blighe50k wrote:

Oh, you just need to do this:

corrplot(data.matrix(pears), method="circle")
ADD COMMENTlink written 3 months ago by Kevin Blighe50k
Please log in to add an answer.

Help
Access

Use of this site constitutes acceptance of our User Agreement and Privacy Policy.
Powered by Biostar version 2.3.0
Traffic: 1237 users visited in the last hour