pythonic生物人

复现IF 48.5的期刊图,这个图帮你省下50%版面费!

最近,看到一篇Nature,频繁使用这种趴着或者躺着的Correlation Heatmap,美观+省版面空间(ref: s41586-023-05794-2),

ImageImage

下面使用R语言复现一下这种趴着或者躺着的Correlation Heatmap。

效果图:趴着Correlation Heatmap
效果图:趴着Correlation Heatmap
效果图:躺着Correlation Heatmap
效果图:躺着Correlation Heatmap

这种图可由之前介绍的上三角Correlation Heatmap修改而来,

上三角Correlation Heatmap
上三角Correlation Heatmap

R绘制-趴着Correlation Heatmap

首先,关闭xy轴刻度标签,并且开启对角线刻度标签,

library(ggcorrplot)

ggcorrplot(
  corr_matrix,
  type = "upper",
  show.diag = TRUE, # 开启对角线刻度标签,
  method = "square",
  hc.order = TRUE,
  lab = TRUE,
  lab_size = 5,
  lab_col = "black",
  colors = c("#0571B0", "white", "#CA0020"),
  outline.color = "gray30",
  tl.cex = 10
) +
  theme(
    axis.text.x = element_blank(), # 关闭xy轴刻度标签
    axis.text.y = element_blank(),
    legend.position = "none", # 关闭图例
    panel.grid = element_blank()
  )
Image

然后,在对角线添加样本名称,

library(ggcorrplot)
library(ggplot2)

hc <- hclust(as.dist(1 - corr_matrix))
ordered_vars <- colnames(corr_matrix)[hc$order]
corr_matrix_ord <- corr_matrix[ordered_vars, ordered_vars]

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.1, y = y - 0.8, label = label),
    inherit.aes = FALSE,
    angle = 0,
    size = 5,
    fontface = "plain",
    color = "black",
    hjust = 0.5,
    vjust = 1
  ) +
  coord_cartesian(ylim = c(0.2, n)) +
  theme(
    axis.text.x = element_blank(),
    axis.text.y = element_blank(),
    axis.ticks = element_blank(),
    panel.grid = element_blank(),
    legend.position = "none"
  )

print(final_plot)
Image

这里注意几个细节:

  • aes(x = x + 0.1, y = y - 0.8, label = label)调整样本名称xy方向的坐标;
  • coord_cartesian(ylim = c(0.2, n)) 防止Sample7和Sample5显示不完全。

最后,通过ggdraw() + draw_plot(ggplotify::as.ggplot(final_plot, angle = -45))旋转图形即可,

library(cowplot)

options(repr.plot.width = 10, repr.plot.height = 10, repr.plot.res = 200)

hc <- hclust(as.dist(1 - corr_matrix))
ordered_vars <- colnames(corr_matrix)[hc$order]
corr_matrix_ord <- corr_matrix[ordered_vars, ordered_vars]

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),
    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)) +
  theme(
    axis.text.x = element_blank(),
    axis.text.y = element_blank(),
    axis.ticks = element_blank(),
    panel.grid = element_blank(),
    legend.position = "none",
    plot.margin = unit(c(3.5, 3, 2, 3.5), "cm")
  )

ggdraw() +
  draw_plot(ggplotify::as.ggplot(final_plot, angle = -45)) # 旋转图形
Image

换个颜色,

Image

R绘制-躺着Correlation Heatmap

方法和上面一样,注意通过aes(x = x + 0.1, y = y - 0.8, label = label)调整样本名称xy方向的坐标;通过ggdraw() + draw_plot(ggplotify::as.ggplot(final_plot, angle = -45))旋转图形的角度,

ImageImage

当然,原图还有一些细节可借助PPT等工具完成!

数据

使用“1.4.9 gene数据集”,

图片

用cor方法简单计算一下相关性。


测试数据+详细代码,后期会加入👉《保姆级R可视化教程》来了!

加入学习(备注:708)

图片