Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

R corrplot change data labels

Tags:

r

r-corrplot

I am using R corrplot library. It looks amazing, however to produce a really good plot I want to change the labels of rows and columns of the correlation matrix.

One solution is to do something like this:

cbak <- colnames(my.data.frame)
colnames(my.data.frame) <- c("a", "set", "of", "labels")
corrplot(cor(my.data.frame))
colnames(my.data.frame) <- cbak

However this looks strange and ugly.

I guess I should work with the labels parameter of a text() function, but I can't figure how.

corrplot(cor(my.data.frame), labels=c("a", "set", "of", "labels")) 

results in

Error in text.default(pos.xlabel[, 1], pos.xlabel[, 2], newcolnames, srt = tl.srt,  : 
  invalid 'pos' value
In addition: Warning message:
In text.default(pos.xlabel[, 1], pos.xlabel[, 2], newcolnames, srt = tl.srt,  :
  NAs introduced by coercion

How to do this correctly?

like image 680
Oleg Titov Avatar asked Mar 13 '16 01:03

Oleg Titov


People also ask

How do I change the text size in Corrplot in R?

For this, we have to specify a smaller value to the number. cex argument. The smaller this value is, the smaller is the font size (or the larger). As you can see, we have modified the size of the correlation coefficients in our plot.

How do I change colors in Corrplot?

To change the color code of corrplot, we can use colorRampPalette function inside corrplot function. We can provide different colors in colorRampPalette that we want to display in the corrplot.

What does Corrplot do in R?

R package corrplot provides a visual exploratory tool on correlation matrix that supports automatic variable reordering to help detect hidden patterns among variables. corrplot is very easy to use and provides a rich array of plotting options in visualization method, graphic layout, color, legend, text labels, etc.

How do you visualize a correlation matrix in R?

The easiest way to visualize a correlation matrix in R is to use the package corrplot. In our previous article we also provided a quick-start guide for visualizing a correlation matrix using ggplot2. Another solution is to use the function ggcorr() in ggally package.


1 Answers

In the current corrplot version 0.75, you cannot use labels parameter because the X and Y labels are computed within the corrplot() function from colnames() and rownames() of the input corr matrix.

I'm using similar approach as you have suggested:

M <- cor(mtcars)
colnames(M) <- c("a", "set", "of", "x", "labels", 1:6)
corrplot(M, method = "color")

BTW, I linked this stackoverflow question from our github issue tracker: https://github.com/taiyun/corrplot/issues/20

UPDATE: In the current corrplot version 0.78, also plotmath expressions are allowed in variable names. Just prefix your name with one of the characters ":", "=" or "$".

Example:

M <- cor(mtcars)[1:5,1:5]
colnames(M) <- c("alpha", "beta", ":alpha+beta", ":a[0]", "=a[beta]")
rownames(M) <- c("alpha", "beta", NA, "$a[0]", "$ a[beta]")
corrplot(M)

enter image description here

like image 138
Viliam Simko Avatar answered Sep 21 '22 09:09

Viliam Simko