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

如何在R的corrplot中显示不同P值显著性水平(*、**等)

在corrplot中实现多水平显著性标记

要实现不同P值水平的差异化标记,核心是先根据P值矩阵生成对应标记的矩阵,再将其添加到corrplot中。以下是具体步骤:

步骤1:生成自定义显著性标记矩阵

基于你已有的pval矩阵,按P值区间生成对应标记(、、):

# 初始化空标记矩阵
sig_mat <- matrix("", nrow = nrow(pval), ncol = ncol(pval))

# 按P值区间赋值标记
sig_mat[pval < 0.001] <- "***"
sig_mat[pval >= 0.001 & pval < 0.01] <- "**"
sig_mat[pval >= 0.01 & pval < 0.05] <- "*"

# 对角线置空(自身相关无需标记)
diag(sig_mat) <- ""

步骤2:绘制corrplot并添加标记

先绘制基础的相关系数热力图,再叠加自定义的显著性标记:

# 加载corrplot包
library(corrplot)

# 绘制基础相关系数图(可根据需求调整method、type等参数)
corrplot(Corr_Emo, method = "color", type = "upper", tl.col = "black")

# 叠加自定义显著性标记
corrplot(Corr_Emo, method = "none", type = "upper", add = TRUE,
         p.mat = pval, sig.labels = sig_mat, insig = "label_sig",
         col = "black", bg = "white", cex = 1)

参数说明:

  • method = "none":仅添加标记,不覆盖原有图形
  • add = TRUE:在已有图形上叠加内容
  • sig.labels = sig_mat:指定自定义的标记矩阵
  • insig = "label_sig":仅显示我们定义的显著标记,不处理非显著项
  • cex:调整标记的字号大小

如果需要在下方显示相关系数、上方显示标记,可以用corrplot.mixed配合标记矩阵:

corrplot.mixed(Corr_Emo, lower = "number", upper = "color", tl.col = "black")
corrplot(Corr_Emo, method = "none", type = "upper", add = TRUE,
         p.mat = pval, sig.labels = sig_mat, insig = "label_sig",
         col = "black", bg = "white")

内容的提问来源于stack exchange,提问作者Hey_It'sME

相关产品推荐
方舟 Agent Plan

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

最近更新时间:2026.08.20 17:45:47