简体   繁体   English

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

[英]How to color the background of a corrplot by group?

Consider this data, where we have several groups with 10 observations each, and we conduct a pairwise.t.test() :考虑这个数据,我们有几个组,每个组有 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

Now lets get the matrix of p values, convert them to a binary matrix showing significant and non-significant values, and plot them with corrplot() , so that we can visualize which groups are different:现在让我们获取 p 值的矩阵,将它们转换为显示显着和非显着值的二进制矩阵,并使用corrplot()对它们进行 plot ,这样我们就可以看到哪些组是不同的:

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

在此处输入图像描述

Does anyone know a way to color the background of each square according to a grouping label?有谁知道根据分组 label 为每个正方形的背景着色的方法? For instance, say we want the squares for groups a:g to be yellow, the squares for groups h:n to be blue, and the squares for groups o:r to be red.例如,假设我们希望组a:g的正方形为黄色,组h:n的正方形为蓝色,组o:r的正方形为红色。 Or is there an alternative way to do this with ggplot ?还是有另一种方法可以用ggplot做到这一点?

You can pass a vector of background colors via the bg= parameter.您可以通过bg=参数传递背景向量 colors。 The trick is just making sure they are in the right order.诀窍只是确保它们的顺序正确。 Here's on way to do that这就是这样做的方式

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)

在此处输入图像描述

Basically we just make a matrix the same shape as our input, then we set the colors we want for the different rows, and then we just pass the lower triangle of that matrix to the function.基本上我们只是制作一个与输入相同形状的矩阵,然后我们为不同的行设置我们想要的 colors,然后我们将该矩阵的下三角形传递给 function。

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

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