P-values 热图 - 所有成对 comparisons/p-values 与 R 的表示(例如像彩色相关矩阵)

P-values heatmap - Representation of all pairwise comparisons/p-values with R (e.g. like a colored correlation matrix)

我有很多成对比较;在报告中可视化或添加所有 p 值非常困难。我想知道 R 中是否有一个函数可以 很好地表示成对的 comparisons/all p 值。

像这样:

但是不是在小方块中使用相关值,而是使用 p 值?

谢谢

******* EDITED/SOLUTION *********

使用 emmeans 包进行成对比较时的解决方案(感谢@Park):

p.val.test<-pwpm(emmeans(your_model, "your_factor"),means = FALSE, flip = TRUE,reverse = TRUE) # p-values presented compactly in matrix form
p.val.test<-sub("[<>]", "", p.val.test)
p.matx<-matrix(as.numeric((p.val.test)),nrow = length(p.val.test[,1]),ncol = length(p.val.test[,1])) #if your factor has 5 levels ncol and nrow=5
rownames(p.matx) <- colnames(p.matx) <-colnames(p.val.test)
p.matx[upper.tri(p.matx, diag=FALSE)] <- NA
melt(p.matx) %>%
  ggplot(aes(Var1, Var2, fill = value)) + geom_tile() +
  geom_text(aes(label = value))

嗯,我做了成对的简单线性回归矩阵。

dummy <- data.frame(
  x1 = c(1,2,3,4),
  x2 = c(1,0,3,4),
  x3 = c(1,0,7,4),
  x4 = c(1,0,7,-1),
  x5 = c(8,0,7,-1)
)
colNames <- names(dummy)
mat <- matrix(NA, nrow = 5, ncol = 5)
mat[lower.tri(mat)] <- combn(colNames, 2, function(x) summary(lm(dummy[x][,1] ~ dummy[x][,2]))$coefficients[2,4])

mat <- round(mat,2)
rownames(mat) = colnames(mat) = colnames(dummy)
mat

     x1   x2   x3   x4 x5
x1   NA   NA   NA   NA NA
x2 0.15   NA   NA   NA NA
x3 0.35 0.19   NA   NA NA
x4 0.96 0.80 0.27   NA NA
x5 0.45 0.88 0.75 0.33 NA

然后,使用 meltgeom_tile()(或 geom_rect 等)将 return 相关矩阵图样式 p 值矩阵图

melt(mat) %>%
  ggplot(aes(Var1, Var2, fill = value)) + geom_tile() +
  geom_text(aes(label = value))