【问题标题】:How can I include multiple colours in the text labels for a correlation matrix in R corrplot?如何在 R corrplot 中的相关矩阵的文本标签中包含多种颜色?
【发布时间】:2018-02-20 22:43:08
【问题描述】:

所以我正在尝试使用 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 是标题颜色的参数,但我想将这两个变量更改为彼此不同的颜色,并且在文档中找不到此选项。这可能吗?

【问题讨论】:

  • 文档说 tl.colThe color of text label 这似乎是您所要求的,并且可以采用颜色向量

标签: r r-corrplot


【解决方案1】:

您可以使用组合函数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 年 2 月 20 日创建。

【讨论】:

猜你喜欢
  • 1970-01-01
  • 2019-07-28
  • 1970-01-01
  • 1970-01-01
  • 2020-06-25
  • 1970-01-01
  • 2014-10-17
  • 1970-01-01
  • 2018-02-04
相关资源
最近更新 更多