美文网首页
【跟着CNS来作图2】复杂热图

【跟着CNS来作图2】复杂热图

作者: jjjscuedu | 来源:发表于2023-12-15 10:48 被阅读0次

    今天我们尝试复现一个热图,下面图C的右边部分。

    Landscape of somatic alterations in large-scale solid tumors from an Asian population

    下载附表的原始数据。

    附表7

    首先加载需要的包:

    library(xlsx)

    library(ComplexHeatmap)

    library(tidyverse)

    library(ggplot2)

    library(circlize)

    library(patchwork)

    library(ggplotify)

    library(RColorBrewer)

    library(scales)

    library(ggsci)

    library(paletteer)

    加载和处理输入文件:

    data <- read.xlsx("41467_2022_31780_MOESM9_ESM.xlsx",sheetIndex=1)  #读入excel文件,设置读入的是第一个文件

    data <- data[-c(1,2),]  #把前两行不要的给去掉

    colnames(data) <- data[1,]  #设置列名

    data <- data[-1,]      #把列名那一行去掉

    从paper的示意图中可以看出,图中的热图展示的是不同基因在不同tumer type中的数量。这种目的大多可以用table函数实现。

    num <- as.matrix(table(data$Gene,data$'Abbreviation of Tumor Type'))

    这样子,通过table就获得了各个Tumor Type下面的各个基因的数目。

    根据图中设置颜色区间。最低值是白色。根据需要设置一个颜色的区间。

    col_fun = colorRamp2(c(0, 5, 10, 15, 20), c("#ffffff","#b4d9e5", "#91a1cf", "#716bbf","#5239a3"))

    下面先画一个正常的热图,用ComplexHeatmap包。

    draw(Heatmap(num,

            col = col_fun,

            cluster_rows = T,

            cluster_columns = T,

            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),

          ),

    heatmap_legend_side = "bottom"

    )

    和paper中图片相比,需要改进的地方如下:

    1. 行和列的顺序不一样,也就是图片中不是按热图的聚类画的。图中没有聚类。但是从规律来看,列是按所有基因相加排序的。行也是如此。所以我们需要自己排序,然后图中不聚类。

    2.需要在热图块中显示非0的基因数目。

    data_mat <- num[,order(colSums(num),decreasing=T)] #按列的总和排序

    data_mat <- data_mat[order(rowSums(data_mat),decreasing=T),]  #按行的总和排序

    draw(Heatmap(data_mat,

            col = col_fun,

            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),

          ),

    heatmap_legend_side = "bottom"

    )

    再画热图,与paper相比,差不多已经有基本的外型了。行和列的排序也是一样的。就缺把非0 的数字显示了。

    Complexheatmap里面是通过grid.text来实现的。

    draw(Heatmap(data_mat,

            col = col_fun,

            cluster_rows = F,

            cluster_columns = F,

            row_names_side = "left",

            #border_gp=gpar(col="white",lty=5),

            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 (data_mat[i,j]!=0) {

                  grid.text(sprintf("%1.f", data_mat[i, j]), x, y,

                          gp = gpar(fontsize = 10, col = "#df9536"))

              }

            }

          ),

    heatmap_legend_side = "bottom"

    )

    相关文章

      网友评论

          本文标题:【跟着CNS来作图2】复杂热图

          本文链接:https://www.haomeiwen.com/subject/lezwgdtx.html