Search code examples
rr-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?

Do I have to create a variable before? Or can you select columns within the command?

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

enter image description here


Solution

  • tl.col takes a vector with colors for each variable. So you can set the color for each variable directly with the tl.col argument like this:

    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:

    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)