如何按组为 corrplot 的背景着色?

How to color the background of a corrplot by group?

考虑这个数据,我们有几个组,每个组有 10 个观察值,我们进行 pairwise.t.test():

set.seed(123)
data <- data.frame(group = rep(letters[1:18], each = 10),
                   var = rnorm(180, mean = 2, sd = 5))
ttres <- pairwise.t.test(x=data$var, g=data$group, p.adjust.method = "none")#just to make sure i get some sigs for the example

现在让我们得到 p 值的矩阵,将它们转换为显示显着值和非显着值的二进制矩阵,并用 corrplot() 绘制它们,这样我们就可以看到哪些组是不同的:

library(corrplot)
pmat <- as.matrix(ttres$p.value)
pmat<-round(pmat,2)
pmat <- +(pmat <= 0.1)
pmat
corrplot(pmat, insig = "blank", type = "lower")

有谁知道根据分组标签为每个方块的背景着色的方法吗?例如,假设我们希望组 a:g 的方块为黄色,组 h:n 的方块为蓝色,组 o:r 的方块为红色。或者是否有其他方法可以使用 ggplot?

您可以通过 bg= 参数传递背景颜色向量。诀窍就是确保它们的顺序正确。这是执行此操作的方法

bgcolors <- matrix("white", nrow(pmat), ncol(pmat),dimnames = dimnames(pmat))
bgcolors[1:6, ] <- "yellow"
bgcolors[7:15, ] <- "blue"
bgcolors[14:17, ] <- "red"
bgcolors <- bgcolors[lower.tri(bgcolors, diag=TRUE)]
corrplot(pmat, insig = "blank", type = "lower", bg=bgcolors)

基本上我们只是制作一个与我们的输入形状相同的矩阵,然后我们为不同的行设置我们想要的颜色,然后我们只是将该矩阵的下三角传递给函数。