pythonic生物人

导师认为你应该会的7大科研图表

为大家整理了科研高频使用的7大类图表,希望对大家有帮助!

1、上三角Mantel test Heatmap

ImageImage

✅参考代码,

# 绘制图形:创建相关性热图与Mantel检验连线组合图
qcorrplot(correlate(varechem),  # 首先计算环境因子之间的Pearson相关系数矩阵并绘制热图
          type = "lower",        # 只显示相关矩阵的下三角部分
          diag = FALSE) +        # 不显示对角线
# 为热图添加方形单元格
  geom_square() +
# 添加连接线:将Mantel检验结果以曲线的形式展示
# 曲线颜色(colour)表示p值显著性,粗细(size)表示相关系数大小
  geom_couple(aes(colour = pd, size = rd),
              data = mantel,     # 使用前面Mantel检验的结果
              curvature = nice_curvature()  # 自动调整曲线曲率使其美观
  )

2、下三角Mantel test Heatmap

ImageImage

✅参考代码,

# 绘制组合图形
qcorrplot(correlate(varechem),  # 计算环境因子间的Pearson相关系数矩阵
          type = "upper",        # 显示相关矩阵的上三角部分
          diag = FALSE) +        # 不显示对角线
  geom_square() +                # 添加方形单元格
# 添加Mantel检验连线:颜色表示p值,粗细表示r值
  geom_couple(aes(colour = pd, size = rd),
              data = mantel,
              curvature = nice_curvature()) 

3、双Mantel test Heatmap

ImageImage

✅参考代码,

# 左图:完整环境因子相关性
p_left <- qcorrplot(cor_full, type = "lower", diag = FALSE) +
  geom_square() +
  geom_couple(aes(colour = pd, size = rd),
              data = mantel, curvature = nice_curvature())

# 右图:子集环境因子相关性
p_right <- qcorrplot(cor_sub, type = "upper", diag = FALSE) +
  geom_square() +
  geom_couple(aes(colour = pd, size = rd),
              data = mantel, curvature = nice_curvature()) 

# 拼接
p_final <- p_left + p_right + 
  plot_layout(widths = c(1, 1)) &
  guides(
    size = guide_legend(title = "Mantel's r", override.aes = list(colour = "grey35"), order = 2),
    colour = guide_legend(title = "Mantel's p", override.aes = list(size = 3), order = 1),
    fill = guide_colorbar(title = "Pearson's r", order = 3)
  )

p_final

4、上三角Correlation heatmap

Image

✅参考代码,

library(ggcorrplot)

ggcorrplot(
  corr_matrix, # 相关性矩阵
  type = "upper", # 上三角相关性矩阵
  method = "square", # 使用方格显示
  hc.order = TRUE, # 层次聚类排序,增强高低相关性对比
  lab = TRUE, # 显示相关性数值
  lab_size = 5, # 数值字体大小
  lab_col = "black", # 数值颜色
  colors = c("#0571B0", "white", "#CA0020"), # 使用高对比度颜色主题
  outline.color = "gray30", # 添加深灰色轮廓线,增强单元格边界
  tl.cex = 10# 行/列标签字体大小
) 

5、下三角Correlation heatmap

Image

✅参考代码,

library(ggcorrplot)

ggcorrplot(
  corr_matrix, # 相关性矩阵
  type = "lower", # 🔸下三角相关性矩阵
  method = "square", # 使用方格显示
  hc.order = TRUE, # 层次聚类排序,增强高低相关性对比
  lab = TRUE, # 显示相关性数值
  lab_size = 5, # 数值字体大小
  lab_col = "black", # 数值颜色
  colors = c("#0571B0", "white", "#CA0020"), # 颜色主题
  outline.color = "gray30", # 添加深灰色轮廓线,增强单元格边界
  ggtheme = ggplot2::theme_minimal(base_size = 8) # 设置基础字体大小
)

6 、趴三角Correlation heatmap

ImageImage

✅参考代码,

p <- ggcorrplot(
  corr_matrix_ord,
  type = "upper",
  show.diag = TRUE,
  method = "square",
  lab = TRUE,
  lab_size = 5,
  lab_col = "black",
  colors = c("#0571B0", "white", "#CA0020"),
  outline.color = "gray30",
  tl.cex = 0
)

# 对角线标签
n <- length(ordered_vars)
diag_labels <- data.frame(
  x = 1:n,
  y = 1:n,
  label = ordered_vars
)

# 最终绘图调整
final_plot <- p +
  geom_text(
    data = diag_labels,
    aes(x = x + 0.3, y = y - 0.8, label = label), # 调整 x 实现居中
    inherit.aes = FALSE,
    angle = 0,
    size = 5,
    fontface = "plain",
    color = "black",
    hjust = 0.5,
    vjust = 1
  ) +
  coord_cartesian(ylim = c(0, n + 0.5), xlim = c(0, n + 1)) 

final_plot

7、躺三角Correlation heatmap

ImageImageImage

✅参考代码,

# 使用 ggcorrplot 创建热图
p <- ggcorrplot(
  corr_matrix_ord,
  type = "upper",
  show.diag = TRUE,
  method = "square",
  lab = TRUE,
  lab_size = 5,
  lab_col = "black",
  colors = c("#0571B0", "white", "#CA0020"),
  outline.color = "gray30",
  tl.cex = 0
)

# 对角线标签
n <- length(ordered_vars)
diag_labels <- data.frame(
  x = 1:n,
  y = 1:n,
  label = ordered_vars
)

# 最终绘图调整
final_plot <- p +
  geom_text(
    data = diag_labels,
    aes(x = x + 0.3, y = y - 0.8, label = label), # 调整 x 实现居中
    inherit.aes = FALSE,
    angle = 0,
    size = 5,
    fontface = "plain",
    color = "black",
    hjust = 0.5,
    vjust = 1
  ) +
  coord_cartesian(ylim = c(0, n + 0.5), xlim = c(0, n + 1)) 
final_plot

over,希望对大家有帮助!


节选自👉《保姆级R可视化教程3.0》

学起来~

图片
#科研绘图#R语言#生物信息#热图