看帮助文档学热图

作者: 小洁忘了怎么分身 | 来源:发表于2019-01-28 23:11 被阅读17次

    生成测试数据

    
    test = matrix(rnorm(200), 20, 10)
    test[1:10, seq(1, 9, 2)] = test[1:10, seq(1, 9, 2)] + 3
    test[11:20, seq(2, 10, 2)] = test[11:20, seq(2, 10, 2)] + 2
    test[15:20, seq(2, 10, 2)] = test[15:20, seq(2, 10, 2)] + 4
    colnames(test) = paste("Test", 1:10, sep = "")
    rownames(test) = paste("Gene", 1:20, sep = "")
    
    

    画热图

    library(pheatmap)
    pheatmap(test)
    
    #忽略行(即基因)间的差异,专注列(样本)之间的差异
    pheatmap(test, scale = "row")
    
    #换颜色
    pheatmap(test, color = colorRampPalette(c("navy", "white", "firebrick3"))(50))
    
    #不按行聚类
    pheatmap(test, cluster_row = FALSE)
    
    #不显示图例
    pheatmap(test, legend = FALSE)
    
    # 图上显示文本,默认显示原数值
    pheatmap(test, display_numbers = TRUE)
    
    #科学计数法
    pheatmap(test, display_numbers = TRUE, number_format = "%.1e")
    
    #数值>5的标星号
    pheatmap(test, display_numbers = matrix(ifelse(test > 5, "*", ""), nrow(test)))
    
    #添加标题,修改大小,存为pdf
    pheatmap(test, cellwidth = 15, cellheight = 12, main = "Example heatmap")
    
    #pheatmap(test, cellwidth = 15, cellheight = 12, fontsize = 8, filename = "test.pdf")
    
    # 行列注释,格式是数据框,数据类型是因子。
    #列,分组信息,test1:10分别属于CT1和CT2
    annotation_col = data.frame(
      CellType = factor(rep(c("CT1", "CT2"), 5)), 
      Time = 1:5
    )
    rownames(annotation_col) = colnames(test)
    
    annotation_row = data.frame(
      GeneClass = factor(rep(c("Path1", "Path2", "Path3"), c(10, 4, 6)))
    )
    rownames(annotation_row) = rownames(test)
    
    # 显示行列分组信息注释栏
    pheatmap(test, annotation_col = annotation_col)
    
    #注释栏图例可不显示
    pheatmap(test, annotation_col = annotation_col, annotation_legend = FALSE)
    
    pheatmap(test, annotation_col = annotation_col, annotation_row = annotation_row)
    
    
    # 改变注释栏颜色
    ann_colors = list(
      Time = c("white", "firebrick"),
      CellType = c(CT1 = "#1B9E77", CT2 = "#D95F02"),
      GeneClass = c(Path1 = "#7570B3", Path2 = "#E7298A", Path3 = "#66A61E")
    )
    
    pheatmap(test, 
             annotation_col = annotation_col, 
             annotation_colors = ann_colors, 
             main = "Title")
    
    pheatmap(test, 
             annotation_col = annotation_col, 
             annotation_row = annotation_row, 
             annotation_colors = ann_colors)
    
    pheatmap(test, 
             annotation_col = annotation_col, 
             annotation_colors = ann_colors[2]) 
    
    # 显示gaps
    pheatmap(test, 
             annotation_col = annotation_col, 
             cluster_rows = FALSE, 
             gaps_row = c(10, 14))
    
    pheatmap(test, 
             annotation_col = annotation_col, 
             cluster_rows = FALSE, 
             gaps_row = c(10, 14), 
             cutree_col = 2)
    
    #需要指定行不排序才能加gaps,列同理。分簇后也加gaps用cutree_col和cutree_row,如果没有聚成簇,则此参数被忽略。
    # 修改行列名,不显示用“”。
    labels_row = c("", "", "", "", "", "", "", "", "", "", "", "", "", "", "", 
                   "", "", "Il10", "Il15", "Il1b")
    
    pheatmap(test, annotation_col = annotation_col, labels_row = labels_row)
    

    相关文章

      网友评论

        本文标题:看帮助文档学热图

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