美文网首页生物信息学
画出美哭了的热图

画出美哭了的热图

作者: PriscillaBai | 来源:发表于2018-06-21 17:41 被阅读216次

    声明:本帖中代码为小丫画图出品,我只是辛勤的搬运工,侵删。如想学习更多生信知识,请关注下方二维码,打赏后进群,群内干货多多。生信路上自学愉快~(为啥二维码这么大)

    微信号:epigenomics

    热图必备三要素:配色美美哒,边栏有scale,第三个我编不出来了……

    场景一:基因表达量的热图

    1 读取数据,加载包

    df<-read.table("mRNA.txt",row.names = 1,header = T,as.is = T)
    head(df)
    library(pheatmap)
    

    2 设置分组,用来画scale

    annotation_col = data.frame(
      Gender = factor(rep(c("F","M"),32)),#按性别分组
      type = factor(rep(c("DG","PFC","PCC","CA1","CB","OC","TC","PC"),c(8,8,8,8,8,8,8,8)))) #按样本类别分组
    rownames(annotation_col) = colnames(df)
    

    3 设置scale的颜色

    ann_colors = list(
      Gender = c(F = "#FFA42D", M = "#A9D9DF"),#给性别分组设置颜色
      type = c(DG = "blue", PFC = "green", PCC = "red", CA1 = "black",
      CB = "pink", OC = "grey", TC = "yellow", PC = "purple")
    

    4 画热图

    pheatmap(df[1:1000,], #横行是画基因的相关性
             cellwidth = 8, cellheight = 1, fontsize = 8, #调整格子和字体的大小
             method="spearman", #计算gene或sample之间的相关性的方法,可选"pearson" (default), "kendall", or "spearman"
             scale="row", #为基因做scale
             cluster_rows=T,#为基因做聚类
             cluster_cols=T,#为sample做聚类
             color = colorRampPalette(c("navy", "white", "firebrick3"))(20), #调颜色
             show_colnames=F,show_rownames =F,#图太大了的话横轴和纵轴名就都不显示
             annotation_col = annotation_col, #scale的名字
             annotation_colors = ann_colors,  #scale的颜色
             #treeheight_row = "0",treeheight_col = "0",#不画树
             border_color = "NA")
    
    image.png

    场景二:样本相关性的热图

    1. 计算样本间的相关性

    cormat<-round(cor(df,method = "spearman"),2)
    

    2. 画热图

    library(pheatmap)
    pheatmap(cormat,cellwidth = 8, cellheight = 8,fontsize = 8,
             color = colorRampPalette(c("#3C7DAF", "#EAF4F1","#FFFCBA", "#E83140"))(20),
             show_colnames=T,show_rownames =T,#显示sample的名字
             #border_color = "NA",#默认有边框,不要边框的话就加这行
             treeheight_row = "0",treeheight_col = "0")#不画树
    
    image.png

    3. 显示数字的热图

    pheatmap(cormat[4:24,4:24],cellwidth = 15, cellheight = 15,fontsize = 8,
             color = colorRampPalette(c("#3C7DAF", "#EAF4F1","#FFFCBA", "#E83140"))(20),
             show_colnames=T,show_rownames =T,
             display_numbers = TRUE,#显示数字
             treeheight_row = "0",treeheight_col = "0")#不画树
    
    image.png

    4. 显示scale 改改配色

    #按照sample的顺序,告诉R,它是属于哪个组的
    annotation_col = data.frame(
      type = factor(rep(c("DG","PFC","PCC","CA1","CB","OC","TC","PC"),c(8,8,8,8,8,8,8,8))))
    rownames(annotation_col) = colnames(df)
    
    annotation_row = data.frame(
      type = factor(rep(c("DG","PFC","PCC","CA1","CB","OC","TC","PC"),c(8,8,8,8,8,8,8,8))))
    rownames(annotation_row) = colnames(df)
    
    #然后给每个组设置颜色
    ann_colors = list(
      type = c(DG = "blue", PFC = "green", PCC = "red", CA1 = "black",
      CB = "pink", OC = "grey", TC = "yellow", PC = "purple")
    )
    
    pheatmap(cormat,cellwidth = 8, cellheight = 8,
             fontsize = 8,
             #display_numbers = TRUE,
             color = colorRampPalette(c("navy", "white", "firebrick3"))(20),
             show_colnames=F,show_rownames =F,#不显示sample的名字
             annotation_col = annotation_col, annotation_row = annotation_row,
             annotation_colors = ann_colors,
             treeheight_row = "0",treeheight_col = "0",#不画树
             border_color = "NA")#不显示边框
    
    image.png

    版权原因,数据集不能给出,如想要数据集练习,请扫码进群走大门。小丫画图欢迎你~

    相关文章

      网友评论

        本文标题:画出美哭了的热图

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