R corrplot change data labels

旧巷老猫 提交于 2019-12-01 05:27:14

问题


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?


回答1:


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)



来源:https://stackoverflow.com/questions/35965433/r-corrplot-change-data-labels

标签
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!