R corrplot 更改数据标签

R corrplot change data labels

我正在使用 R corrplot 库。它看起来很棒,但是为了生成一个非常好的图,我想更改相关矩阵的行和列的标签。

一个解决方案是做这样的事情:

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

然而,这看起来又奇怪又难看。

我想我应该使用 text() 函数的 labels 参数,但我不知道该怎么做。

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

结果

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

如何正确执行此操作?

在当前的 corrplot 版本 0.75 中,您不能使用 labels 参数,因为 X 和 Y 标签是在 corrplot() 函数中从 colnames() 和 [=输入 corr 矩阵的 16=]。

我正在使用与您建议的类似的方法:

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

顺便说一句,我从我们的 github 问题跟踪器中链接了这个 Whosebug 问题: https://github.com/taiyun/corrplot/issues/20

更新: 在当前的 corrplot 版本 0.78 中,变量名称中也允许使用 plotmath 表达式。只需在您的姓名前加上字符“:”、“=”或“$”之一即可。

示例:

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)