热图

作者: 超级无敌大蜗牛 | 来源:发表于2020-03-01 22:37 被阅读0次
    • Create test matrix
    test = matrix(rnorm(200), 20, 10)
    test[1:10, seq(1, 10, 2)] = test[1:10, seq(1, 10, 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 = "")
    
    • Draw heatmaps
    pheatmap(test, scale = "row", cluster_row = FALSEcolor = colorRampPalette(c("navy", "white", "firebrick3"))(50))
    
    • Show text within cells
    pheatmap(test, cluster_row = FALSE, legend_breaks = -1:4, 
             legend_labels = c("0","1e-4", "1e-3", "1e-2", "1e-1", "1"))##是图例的显示数字
    
    • Fix cell sizes and save to file with correct size
    pheatmap(test, cellwidth = 15, cellheight = 12, main = "Example heatmap")
    pheatmap(test, cellwidth = 15, cellheight = 12, fontsize = 8, filename = "test.pdf")##直接保存到当前路径下
    
    • Generate annotations for rows and columns
    annotation_col = data.frame(CellType = factor(rep(c("CT1", "CT2"), 5)),
                                Time = 1:5)
    rownames(annotation_col) = paste("Test", 1:10, sep = "")
    
    annotation_row = data.frame(GeneClass = factor(rep(c("Path1", "Path2", "Path3"), 
                                                       c(10, 4, 6))))
    rownames(annotation_row) = paste("Gene", 1:20, sep = "")
    
    • Display row and color annotations
    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)
    
    • Change angle of text in the columns
    pheatmap(test, annotation_col = annotation_col, 
             annotation_row = annotation_row, angle_col = "45")
    pheatmap(test, annotation_col = annotation_col, angle_col = "0")
    
    • Specify colors
    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 in heatmaps
    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)
    
    • Show custom strings as row/col names
    labels_row = c("", "", "", "", "", "", "", "", "", "", "", "", "", "", "", 
                   "", "", "Il10", "Il15", "Il1b")
    
    pheatmap(test, annotation_col = annotation_col, labels_row = labels_row)
    
    • Specifying clustering from distance matrix
    drows = dist(test, method = "minkowski")
    dcols = dist(t(test), method = "minkowski")
    pheatmap(test, clustering_distance_rows = drows, 
             clustering_distance_cols = dcols)
    
    • Modify ordering of the clusters using clustering callback option
    callback = function(hc, mat){
      sv = svd(t(mat))$v[,1]
      dend = reorder(as.dendrogram(hc), wts = sv)
      as.hclust(dend)
    }
    pheatmap(test, clustering_callback = callback)
    
    • Example
    data <- read.table("interest_exp.txt",header = T, row.names = 1)
    colnames(data) <- c("Resting_1","Resting_2","Activated_24h_1","Activated_24h_2",
                        "Activated_72h_1","Activated_72h_2")
    head(data)
    
    A data.frame: 6 × 6
    Resting_1   Resting_2   Activated_24h_1 Activated_24h_2 Activated_72h_1 Activated_72h_2
    <dbl>   <dbl>   <dbl>   <dbl>   <dbl>   <dbl>
    Ell2    11.97499    11.98783    12.62147    12.70513    13.284151   13.041119
    Pou2f2  15.30454    15.35850    15.10195    15.30378    15.562624   15.626705
    Pax5    17.31524    17.23150    16.68075    16.75841    16.304538   16.262626
    Sdc1    10.67355    10.83138    10.55465    10.33494    8.937576    8.762968
    Myc 14.47428    14.68633    15.19882    15.27371    13.666088   13.773051
    Bcl6    13.43074    13.28095    10.31432    10.35660    11.205224   11.533343
    ##generate annotation text
    annotation_col = data.frame(CellType = c(rep('Resting',2),rep('Activated_24h',2),rep('Activated_72h',2)))
    rownames(annotation_col) = c("Resting_1","Resting_2","Activated_24h_1","Activated_24h_2",
                                 "Activated_72h_1","Activated_72h_2")
    callback = function(hc, mat){
      sv = svd(t(mat))$v[,1]
      dend = reorder(as.dendrogram(hc), wts = sv)
      as.hclust(dend)
    }
    ##draw heatmap
    pheatmap(data, cluster_rows = T,
    #          border=F,##去掉边框
             scale = "row",#每行进行归一化处理
             annotation_col=annotation_col,angle_col = "45",
    #          fontsize = 6, border_color = "grey60",treeheight_row = 50, treeheight_col = 30, 
    #         clustering_callback = callback
            )
    
    pheatmap

    相关文章

      网友评论

          本文标题:热图

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