You need to enable JavaScript to run this app.
优惠活动
大模型
产品
解决方案
定价
更多

为什么ggcorrplot2与ggcorrplot显示的显著性星号存在差异?

ggcorrplot2与ggcorrplot显著性星号显示不一致的原因与解决方法

问题复现代码

data(mtcars)
ct <- corr.test(mtcars)
corr <- ct$r
p.mat <- ct$p
ggcorrplot(corr, type= "lower", p.mat = p.mat, 
                 insig = "label_sig", sig.lvl = c(0.05, 0.01, 0.001),show.diag=F)
ggcorrplot.mixed(corr, upper = "number", lower = "circle", p.mat = p.mat, 
                 insig = "label_sig", sig.lvl = c(0.05, 0.01, 0.001))

根本原因

两个包显示结果存在差异并非绘图函数本身的bug,而是corr.test()输出的p值矩阵本身是非对称的:对角线上方的条目经过了多重检验校正,对角线下方为未校正的原始p值,两个包读取p值时默认调用的三角区域不同,最终呈现的显著性标记就会出现偏差。

修复方法

根据你需要使用的p值类型,选择对应的对称化处理方案:

  • 需要全图使用校正后p值:将上三角的校正p值镜像覆盖到下三角,处理代码为 p.mat[lower.tri(p.mat)] <- t(p.mat)[lower.tri(p.mat)]
  • 需要全图使用未校正原始p值:将下三角的原始p值镜像覆盖到上三角即可。

修正后完整代码示例

data(mtcars)
cor.matrix <- corr.test(mtcars,method = "spearman", adjust = "BH", alpha = 0.05, ci = F)
corr <- cor.matrix[["r"]]
p.mat <- cor.matrix[["p"]]
p.mat[lower.tri(p.mat)] <- t(p.mat)[lower.tri(p.mat)] # 让全图对称使用校正后的p值

p.mat[lower.tri(p.mat, diag = T)] <- 1 # 将下三角设为1
corrplot.mixed(corr, order= "original",mar=c(0,0,2,0), tl.col = 'black', p.mat = p.mat, insig = "label_sig", sig.level = c(.001, .01, .05), pch.cex=1.5, tl.cex = .8, number.font=2, number.cex=0.8)

内容的提问来源于stack exchange,提问作者brauer-t

相关产品推荐
方舟 Agent Plan

超全模态模型 × Harness 升级,最新支持 Deepseek-V4.1-Flash、GLM-5.3 系列、Doubao-Seedream-5.0-pro、Kimi-K3 (部分), 限时 9.9 元起

最近更新时间:2026.09.30 23:57:01