欢迎您访问 最编程 本站为您分享编程语言代码,编程技术文章!
您现在的位置是: 首页

学习Nature Communications的图表制作技巧:复杂热图与堆积柱状图详解 - 互动赢福利!点赞、在看并分享任一课程至朋友圈,集齐20个赞,即可享受¥119加入我们的教程社群哦!

最编程 2024-02-10 13:24:01
...
1355a5e779820a2ec998efcc879ac8a0.png
参考文献

话不多说,直接上图!

读图

93c5bc3eaf32d347ac5b6f1830879267.png
原图

效果展示

2b487aecc34ec5826cc72d6aec1c3a7c.png
复现效果

本期示例数据为文章附件,大家可以通过以下链接下载:

https://static-content.springer.com/esm/art%3A10.1038%2Fs41467-022-31780-9/MediaObjects/41467_2022_31780_MOESM9_ESM.xlsx

由于ComplexHeatmap包自带的rowAnnotation柱状图注释效果有限,所以这里师兄用ggplot2绘制了左侧的堆积柱状图,最后用AI将两者拼接在一起!大家也可以自己尝试用ComplexHeatmap绘制,但是效果可能没这个好看。

R包载入和数据预处理

library(xlsx)
library(ComplexHeatmap)
library(tidyverse)
library(ggplot2)
library(circlize)
library(patchwork)
library(ggplotify)

# 读取文章附件数据:
data <- read.xlsx("41467_2022_31780_MOESM9_ESM.xlsx", sheetIndex = 1)
data <- data[-c(1:2), ]
colnames(data) <- data[1,]
data <- data[-1,]

# 数据预处理:
data_mat <- as.data.frame(table(data$Gene, data$`Abbreviation of Tumor Type`)) %>% 
  pivot_wider(names_from = Var2, values_from = Freq) %>% 
  column_to_rownames("Var1")

data_mat <- data_mat[,order(colSums(data_mat), decreasing = T)]
data_mat <- data_mat[order(rowSums(data_mat), decreasing = T),]
data_mat2 <- as.matrix(data_mat)
data_mat2[which(data_mat2 == 0)] <- NA

热图

# 基础热图:
Heatmap(data_mat2,
        na_col = "white",
        # 去掉行列聚类:
        cluster_rows = F,
        cluster_columns = F)


# 修改颜色+添加文字和描边
col_fun = colorRamp2(c(0, 5, 10, 15), c("#b4d9e5", "#91a1cf", "#716bbf","#5239a3"))

p1 <- Heatmap(data_mat2,
        col = col_fun,
        na_col = "white",
        # 去掉行列聚类:
        cluster_rows = F,
        cluster_columns = F,
        row_names_side = "left",
        # 图例
        heatmap_legend_param = list(
          title = "Frequency(%)", 
          title_position = "leftcenter",
          legend_direction = "horizontal"
        ),
        # 行名和列名:
        row_names_gp = gpar(fontsize = 10, font = 3),
        column_names_gp = gpar(fontsize = 10, font = 3),
        # 添加文字注释:
        cell_fun = function(j, i, x, y, width, height, fill) {
          if (!is.na(data_mat2[i,j])) {
            grid.text(sprintf("%1.f", data_mat2[i, j]), x, y, 
                      gp = gpar(fontsize = 10, col = "#df9536"))
            grid.rect(x, y, width, height,
                      gp = gpar(col = "grey", fill = NA, lwd = 0.8))
          }
        })

pdf("Heatmap.pdf", height = 8, width = 8)
draw(p1, heatmap_legend_side = "bottom")
dev.off()
e592b74cebccb551e08afd016c9a2c62.png
热图

堆积柱状图

# 添加左侧堆积柱状图:
data_mat3 <- as_tibble(data_mat/rowSums(data_mat)) %>% 
  pivot_longer(cols = everything(), names_to = "CancerType",
               values_to = "value")
data_mat3$Gene <- factor(rep(rownames(data_mat), each = 21), 
                            levels = rev(rownames(data_mat)))

p2 <- ggplot(data_mat3)+
  geom_bar(aes(x = Gene, y = value, fill = CancerType), 
           position = "stack", stat = "identity")+
  scale_y_continuous(labels = seq(0,100,25),position = "right")+
  ggsci::scale_fill_igv()+
  ylab("Percentage(%)")+
  theme_bw()+
  theme(panel.grid = element_blank(), 
        axis.title.y = element_blank(),
        axis.text.y = element_blank(),
        axis.ticks.y = element_blank(),
        axis.title.x = element_text(face = "bold"),
        legend.position = "bottom",
        legend.key.size = unit(0.3, 'cm'),
        legend.title=element_text(face="bold")
        )+
  labs(fill = "Tumor Type")+
  guides(fill = guide_legend(title.position = "top",
                             title.hjust = 0.5, ncol = 2, 
                             byrow = TRUE))+
  coord_flip()

p2

ggsave("Stack_Barplot.pdf", height = 8, width = 1.5)
b34bb41db4956b33473edf5c15c583eb.png
堆积柱状图

AI拼图效果展示

56fb4745c88da7995ade3ebefcfc9823.png
复现效果

往期优秀图形目录

渐变火山图
相关性气泡热图
复杂提琴图
复杂热图
复杂散点图
甘特图
复杂百分比柱状图
箱线图美化
弦图
mantel test相关性图
瀑布图
曼哈顿图
Kegg富集柱状图

以上内容仅为群内部分内容,不代表全部。详细目录请看下方列表!已经入群的小伙伴,无需付费购买推文,群内都会及时更新!

示例数据和代码获取

本系列「所有代码和示例数据将会和生信常用图形系列绘图」放在一起,公众号右下角添加师兄微信,「付费149元,即可加入生信绘图交流群」。群内不仅提供生信常用图形系列的代码,还会「提供本系列后续所有Figure的示例数据和代码」,我会在文章更新后第一时间上传。

当然了!如果你还想白嫖,师兄的文章中代码已经写的很清楚了!但是师兄还是希望你「点个赞再走」呗!

以上就是本期的全部内容啦!「欢迎点赞,点在看」!师兄会尽快更新哦!制作不易,你的打赏将成为师兄继续更新的十足动力!