简体   繁体   English

如何更改 R 中 corrplot 中特定变量的字体颜色?

[英]How do I change the font color of specific variables in corrplot in R?

How do I change the color of the font tl.col specific for certain variables like in the image?如何更改特定于图像中某些变量的字体tl.col的颜色?

Do I have to create a variable before?我必须先创建一个变量吗? Or can you select columns within the command?或者您可以在命令中使用 select 列吗?

corrplot(cor(iris[,-5]), tl.col="black", tl.cex=0.8, tl.srt=70)

在此处输入图像描述

Try with this:试试这个:

library (corrplot)
library(MASS)
library(calibrate)
data("iris")

corrplot(cor(iris[,-5]), cl.pos = "n", tl.pos = "n")  
textxy(0,1,labs=c("Petal.Width"),cex=0.8,offset=-0.2,col="blue")
textxy(0,2,labs=c("Petal.Length"),cex=0.8,offset=-0.2,col="blue")
textxy(4,5,labs=c("Petal.Width"),cex=0.8,offset=0,col="blue",srt=45)
textxy(3,5,labs=c("Petal.Length"),cex=0.8,offset=0,col="blue",srt=45)

textxy(0,3,labs=c("Sepal.Width"),cex=0.8,offset=-0.2,col="red")
textxy(0,4,labs=c("Sepal.Length"),cex=0.8,offset=-0.2,col="red")
textxy(2,5,labs=c("Sepal.Width"),cex=0.8,offset=0,col="red",srt=45)
textxy(1,5,labs=c("Sepal.Length"),cex=0.8,offset=0,col="red",srt=45)

Basically you have to remove the variable names displayed by corrplot and textxy creates labels.基本上,您必须删除 corrplot 显示的变量名称,然后 textxy 创建标签。 You have to set position, color..., it's a shortcut for your problem您必须设置 position,颜色...,这是您问题的捷径

tl.col takes a vector with colors for each variable. tl.col为每个变量获取一个带有 colors 的向量。 So you can set the color for each variable directly with the tl.col argument like this:因此,您可以直接使用tl.col参数为每个变量设置颜色,如下所示:

corrplot(cor(iris[,-5]), tl.col=c("red", "red", "blue", "blue"), tl.cex=0.8, tl.srt=70)

Alternatively you can define the colors within a function:或者,您可以在 function 中定义 colors:

colors <- ifelse(grepl("Sepal", names(iris)[-5]), "red", "blue")
corrplot(cor(iris[,-5]), tl.col=colors, tl.cex=0.8, tl.srt=70)

# or 

corrplot(cor(iris[,-5]), tl.col=ifelse(grepl("Sepal", names(iris)[-5]), "red", "blue"),
tl.cex=0.8, tl.srt=70)

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM