R corrplot更改数据标签 [英] R corrplot change data labels

查看:790
本文介绍了R corrplot更改数据标签的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在使用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.

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

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")) 

结果

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标签是在colnames()函数中从rownames()输入的corr矩阵.

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")

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

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

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

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 "$".

示例:

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)

这篇关于R corrplot更改数据标签的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆