如何在 R corrplot 相关矩阵的文本标签中包含多种颜色?

How can I include multiple colours in the text labels for a correlation matrix in R corrplot?

所以我正在尝试使用 R 的 corrplot 包创建相关矩阵。我想在文本标签中使用两种颜色,以显示变量组。

举个简单的例子:

dat <- data.frame("Blue" = c(1:20), "Red" = sample(1:20, 20, replace = T))

dat <- as.matrix(dat)

C = rcorr(dat, type = "pearson")

corrplot(corr = C$r, order = "original", title = "Pearson Correlations", method = "color", type = "full", p.mat=C$P, insig = "blank", tl.col = "blue", addgrid.col = "darkgrey", bg = "white", cl.pos = "b", tl.pos = "tl", col = colorRampPalette(c("darkred","white","midnightblue"))(100), mar = c(4, 0, 4, 0))

我知道 tl.col 是标题颜色的参数,但我想将这两个变量更改为彼此具有不同的颜色,但在文档中找不到此选项。这可能吗?

您可以只使用组合函数 c() 为不同的列输入不同的颜色标签。

library(corrplot)
library(Hmisc)

# defining dataframe
dat <-
  data.frame("Blue" = c(1:20),
             "Red" = sample(1:20, 20, replace = T))

# getting correlations
C = Hmisc::rcorr(as.matrix(dat), type = "pearson")

# preparing the plot
corrplot::corrplot(
  corr = C$r,
  order = "original",
  title = "Pearson Correlations",
  method = "color",
  type = "full",
  p.mat = C$P,
  insig = "blank",
  tl.col = c("blue", "red"), # different colors
  addgrid.col = "darkgrey",
  bg = "white",
  cl.pos = "b",
  tl.pos = "tl",
  col = colorRampPalette(c("darkred", "white", "midnightblue"))(100),
  mar = c(4, 0, 4, 0)
)

reprex package (v0.2.0) 创建于 2018-02-20。