美文网首页基因组数据绘图
热图7:ggplot2/ComplexHeatmap做离散型热图

热图7:ggplot2/ComplexHeatmap做离散型热图

作者: KS科研分享与服务 | 来源:发表于2022-05-23 09:52 被阅读0次

    很久之前我们出过热图系列,一共有6篇文章,反响还是可以,但是最近发现很多新关注的小伙伴没有翻看的习惯,居然不知道,所以今天推文全部列出来。此外,这篇文章的内容是补充之前的不足,因为没有涉及到离散型热图的做法,这里我们以一篇Cell文章为引子,展示下ggplot2/ComplexHeatmap做离散型热图,这样热图系列就完善了。

    我们不可能将所有文章出现的热图复现一遍,请从这短小的几篇介绍中发挥想象,深入学习,通过变化可以展示更多的图形。

    示例数据及代码已上传群文件!
    请关注我的公众号《KS科研分享与服务》

    图片来源

    image.png

    (Reference:Peng Y R , et al. Molecular Classification and Comparative Taxonomics of Foveal and Peripheral Cells in Primate Retina. 2018. Cell)

    构建数据

    作图数据和之前gene表达量一样,只不过将数值换成了因子:

    image.png

    方法1:ggplot2

    加载数据并转化为ggplot长数据:

    setwd('F:/生物信息学/离散型热图')
    A <- read.csv("gene_dis.csv", header = T)
    library(tidyr)
    dft <-gather(A, disease, value, 2:9)
    dft
    
    library(forcats)
    dft$gene <- as.factor(dft$gene)
    dft$gene <- fct_inorder(dft$gene)
    library(ggplot2)
    

    作图:

    ggplot(data=dft,aes(x=disease,y=gene))+
      geom_tile(aes(fill=value),color="grey")+
      theme_minimal()+
      theme(panel.border = element_rect(fill=NA,color="black", size=1, linetype="solid"),
            panel.grid = element_blank(),
            axis.ticks.y = element_blank(),
            axis.title = element_blank(),
            axis.text.x = element_text(angle=45,hjust=1, colour = 'black', size = 12),
            axis.text.y = element_text(colour = 'black', size = 12),
            plot.margin=unit(c(0.4,0.4,0.4,0.4),units=,"cm"))+
      scale_fill_manual(values = c('white','black'))+
      labs(fill="Disease\nassociation")
    
    image.png

    方法2:ComplexHeatmap

    管它什么类型热图,只要是热图,ComplexHeatmap就能搞定。读入数据作图,效果和ggplot2一样。

    library(ComplexHeatmap)
    B <- read.csv("gene_dis.csv", header = T, row.names = 1)
    
    
    Heatmap(B,
            cluster_rows = F,
            cluster_columns = F,
            show_column_names = T,
            show_row_names = T,
            row_names_side =  'left',
            column_title = NULL,
            heatmap_legend_param = list(
              title='Disease\nassociation'),
            col = c('white','black'),
            border = 'black',
            rect_gp = gpar(col = "grey", lwd = 1),
            row_names_gp = gpar(fontsize = 10),
            column_names_gp = gpar(fontsize = 10))
    
    image.png

    我们还可以为热图添加注释,黑白配上一点彩色,感觉还挺有艺术感。

    disease <- c("disease1","disease2","disease3","disease4","disease5","disease6","disease7","disease8")
    group <- c("Male_spc","Male_spc","Male_spc","Female_spc","Female_spc","Female_spc","MF","MF")
    Group <- data.frame(disease, group)#创建数据框
    
    top_anno=HeatmapAnnotation(df=Group,
                               border = T,
                               show_annotation_name = F,
                               col = list(group=c('Male_spc'='#006699',
                                                  'Female_spc'='#993333',
                                                  'MF'='#33CCCC')))
    
    Heatmap(B,
            cluster_rows = F,
            cluster_columns = F,
            show_column_names = F,
            show_row_names = T,
            row_names_side =  'left',
            column_title = NULL,
            heatmap_legend_param = list(
              title='Disease\nassociation'),
            col = c('white','black'),
            border = 'black',
            rect_gp = gpar(col = "grey", lwd = 1),
            row_names_gp = gpar(fontsize = 10),
            column_names_gp = gpar(fontsize = 10),
            top_annotation = top_anno)
    
    image.png

    这就是热图得全部内容了,还是那句话,自己要学会探索学习,不能一味得依靠别人解决错误。通过一个小例子,也要学会拓展,才能创造!

    觉得有用的请点个赞,转发,分享至看一看再走呗。

    相关文章

      网友评论

        本文标题:热图7:ggplot2/ComplexHeatmap做离散型热图

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