SCI图片复现:热图+柱状图

创建一个包含5种肿瘤类型(A, B, C, D, E)和10个基因(Gene1到Gene10)的数据集,每个基因在每种肿瘤类型中的出现频率是随机生成的

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


# 生成虚构数据
set.seed(123) # 设置随机种子以便结果可重现
fake_data <- data.frame(
  Gene = rep(paste0("Gene", 1:10), 5),
  `Abbreviation.of.Tumor.Type` = rep(LETTERS[1:5], each = 10),
  Freq = sample(1:15, 50, replace = TRUE)
)

# 显示前几行数据
head(fake_data)



length(data$Gene)
length(data$`Abbreviation.of.Tumor.Type`)


# 数据预处理:
data_mat <- fake_data %>%
  pivot_wider(names_from = `Abbreviation.of.Tumor.Type`, values_from = Freq) %>%
  column_to_rownames("Gene")

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

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

# 创建热图
heatmap_plot <- 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
pdf("Heatmap.pdf", height = 8, width = 8)
draw(heatmap_plot, heatmap_legend_side = "bottom")
dev.off()




# 创建堆积柱状图
stacked_bar_data <- as_tibble(data_mat / rowSums(data_mat)) %>%
  pivot_longer(cols = everything(), names_to = "CancerType",
               values_to = "value")
stacked_bar_data$Gene <- factor(rep(rownames(data_mat), each = 5), 
                                levels = rev(rownames(data_mat)))

stacked_bar_plot <- ggplot(stacked_bar_data) +
  geom_bar(aes(x = Gene, y = value, fill = CancerType), 
           position = "stack", stat = "identity") +
  scale_y_continuous(labels = seq(0, 1, 0.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()

stacked_bar_plot

# 保存堆积柱状图为PDF
ggsave("Stacked_Barplot.pdf", plot = stacked_bar_plot, height = 8, width = 1.5)


# 将热图转换为 ggplot 对象
heatmap_ggplot <- as.ggplot(heatmap_plot, heatmap_legend_side = "bottom")

# 合并两个图形
combined_plot <- heatmap_ggplot + stacked_bar_plot + plot_layout(ncol = 2, widths = c(3, 1))

# 显示合并后的图形
combined_plot

# 保存合并后的图形为 PDF
ggsave("Combined_Plot.pdf", plot = combined_plot, height = 8, width = 9.5)

 

 

 

 

 

阅读剩余
THE END