pheatmap热图技巧合集

作者: 周运来就是我 | 来源:发表于2019-07-30 05:10 被阅读20次

    突然间发现,我一做热图就用pheatmap几乎不存在第二个选项了。是时候跑一遍pheatmap的demo了.

    Examples
    # 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)
    
    pheatmap(test, kmeans_k = 2)
    

    scale是一个值得注意的参数,它旨在说明你想表达什么。关于聚类的几个参数也需要注意。

    pheatmap(test, scale = "row", clustering_distance_rows = "correlation")
    

    颜色色设定。

    pheatmap(test, color = colorRampPalette(c("navy", "white", "firebrick3"))(50))
    
    pheatmap(test, cluster_row = FALSE)
    
    pheatmap(test, legend = FALSE)
    
    Show text within cells

    显示标签。

    pheatmap(test, display_numbers = TRUE)
    
    pheatmap(test, display_numbers = matrix(ifelse(test > 5, "*", ""), nrow(test)))
    
    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")
    
     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)
    
    Make heatmaps in R with pheatmap
    set.seed(42)
    random_string <- function(n) {
      substr(paste(sample(letters), collapse = ""), 1, n)
    }
    
    mat <- matrix(rgamma(1000, shape = 1) * 5, ncol = 50)
    
    colnames(mat) <- paste(
      rep(1:3, each = ncol(mat) / 3),
      replicate(ncol(mat), random_string(5)),
      sep = ""
    )
    rownames(mat) <- replicate(nrow(mat), random_string(3))
    col_groups <- substr(colnames(mat), 1, 1)
    table(col_groups)
    mat[,col_groups == "1"] <- mat[,col_groups == "1"] * 5
    
    library(ggplot2)
    # Set the theme for all the following plots.
    theme_set(theme_bw(base_size = 16))
    
    dat <- data.frame(values = as.numeric(mat))
    ggplot(dat, aes(values)) + geom_density(bw = "SJ")
    
    ibrary(pheatmap)
    library(RColorBrewer)
    library(viridis)
    
    # Data frame with column annotations.
    mat_col <- data.frame(group = col_groups)
    rownames(mat_col) <- colnames(mat)
    
    # List with colors for each annotation.
    mat_colors <- list(group = brewer.pal(3, "Set1"))
    names(mat_colors$group) <- unique(col_groups)
    
    pheatmap(
      mat               = mat,
      color             = inferno(10),
      border_color      = NA,
      show_colnames     = FALSE,
      show_rownames     = FALSE,
      annotation_col    = mat_col,
      annotation_colors = mat_colors,
      drop_levels       = TRUE,
      fontsize          = 14,
      main              = "Default Heatmap"
    )
    
    mat_breaks <- seq(min(mat), max(mat), length.out = 10)
    
    
    
    ## ----uniform-color-breaks-detail, fig.height=2, echo=FALSE---------------
    dat_colors <- data.frame(
      xmin = mat_breaks[1:(length(mat_breaks)-1)],
      xmax = mat_breaks[2:length(mat_breaks)],
      ymin = 0,
      ymax = max(density(mat, bw = "SJ")$y),
      fill = rev(inferno(length(mat_breaks) - 1)),
      stringsAsFactors = FALSE
    )
    ggplot() +
      geom_rect(
        data = dat_colors,
        mapping = aes(
          xmin = xmin, xmax = xmax, ymin = ymin, ymax = ymax, fill = fill
        )
      ) +
      geom_density(
        data = dat,
        mapping = aes(values),
        bw = "SJ", color = "cyan"
      ) +
      scale_fill_manual(values = dat_colors$fill) +
      theme(legend.position = "none") +
      labs(title = "Uniform breaks")
    
    dat2 <- as.data.frame(table(cut(
      mat, mat_breaks
    )))
    dat2$fill <- inferno(nrow(dat2))
    ggplot() +
      geom_bar(
        data = dat2,
        mapping = aes(x = Var1, weight = Freq, fill = Var1),
        color = "black", size = 0.1
      ) +
      coord_flip() +
      scale_fill_manual(values = dat2$fill) +
      theme(legend.position = "none") +
      labs(y = "data points", x = "breaks",
           title = "Number of data points per color")
    
    quantile_breaks <- function(xs, n = 10) {
      breaks <- quantile(xs, probs = seq(0, 1, length.out = n))
      breaks[!duplicated(breaks)]
    }
    
    mat_breaks <- quantile_breaks(mat, n = 11)
    
    
    
    ## ----quantile-color-breaks-detail, fig.height=2, echo=FALSE--------------
    dat_colors <- data.frame(
      xmin = mat_breaks[1:(length(mat_breaks)-1)],
      xmax = mat_breaks[2:length(mat_breaks)],
      ymin = 0,
      ymax = max(density(mat, bw = "SJ")$y),
      fill = rev(inferno(length(mat_breaks) - 1)),
      stringsAsFactors = FALSE
    )
    ggplot() +
      geom_rect(
        data = dat_colors,
        mapping = aes(
          xmin = xmin, xmax = xmax, ymin = ymin, ymax = ymax, fill = fill
        )
      ) +
      geom_density(
        data = dat,
        mapping = aes(values),
        bw = "SJ", color = "cyan"
      ) +
      scale_fill_manual(values = dat_colors$fill) +
      theme(legend.position = "none") +
      labs(title = "Quantile breaks")
    
    dat2 <- as.data.frame(table(cut(
      mat, mat_breaks
    )))
    dat2$fill <- inferno(nrow(dat2))
    ggplot() +
      geom_bar(
        data = dat2,
        mapping = aes(x = Var1, weight = Freq, fill = Var1),
        color = "black", size = 0.1
      ) +
      coord_flip() +
      scale_fill_manual(values = dat2$fill) +
      theme(legend.position = "none") +
      labs(y = "data points", x = "breaks",
           title = "Number of data points per color")
    
    
    pheatmap(
      mat               = mat,
      color             = inferno(length(mat_breaks) - 1),
      breaks            = mat_breaks,
      border_color      = NA,
      show_colnames     = FALSE,
      show_rownames     = FALSE,
      annotation_col    = mat_col,
      annotation_colors = mat_colors,
      drop_levels       = TRUE,
      fontsize          = 14,
      main              = "Quantile Color Scale"
    )
    
    pheatmap(
      mat               = log10(mat),
      color             = inferno(10),
      border_color      = NA,
      show_colnames     = FALSE,
      show_rownames     = FALSE,
      annotation_col    = mat_col,
      annotation_colors = mat_colors,
      drop_levels       = TRUE,
      fontsize          = 14,
      main              = "Log10 Transformed Values"
    )
    
    mat_cluster_cols <- hclust(dist(t(mat)))
    plot(mat_cluster_cols, main = "Unsorted Dendrogram", xlab = "", sub = "")
    
    #' 
    #' 
    #' Let's flip the branches to sort the dendrogram. The most similar
    #' columns will appear clustered toward the left side of the plot. The columns
    #' that are more distant from each other will appear clustered toward the right
    #' side of the plot.
    #' 
    #' 
    ## ----hclust-dendsort-example---------------------------------------------
    # install.packages("dendsort")
    library(dendsort)
    
    sort_hclust <- function(...) as.hclust(dendsort(as.dendrogram(...)))
    
    mat_cluster_cols <- sort_hclust(mat_cluster_cols)
    plot(mat_cluster_cols, main = "Sorted Dendrogram", xlab = "", sub = "")
    
    
    mat_cluster_rows <- sort_hclust(hclust(dist(mat)))
    pheatmap(
      mat               = mat,
      color             = inferno(length(mat_breaks) - 1),
      breaks            = mat_breaks,
      border_color      = NA,
      cluster_cols      = mat_cluster_cols,
      cluster_rows      = mat_cluster_rows,
      show_colnames     = FALSE,
      show_rownames     = FALSE,
      annotation_col    = mat_col,
      annotation_colors = mat_colors,
      drop_levels       = TRUE,
      fontsize          = 14,
      main              = "Sorted Dendrograms"
    )
    
    
    draw_colnames_45 <- function (coln, gaps, ...) {
        coord <- pheatmap:::find_coordinates(length(coln), gaps)
        x     <- coord$coord - 0.5 * coord$size
        res   <- grid::textGrob(
          coln, x = x, y = unit(1, "npc") - unit(3,"bigpts"),
          vjust = 0.75, hjust = 1, rot = 45, gp = grid::gpar(...)
        )
        return(res)
    }
    assignInNamespace(
      x = "draw_colnames",
      value = "draw_colnames_45",
      ns = asNamespace("pheatmap")
    )
    
    pheatmap(
      mat               = mat,
      color             = inferno(length(mat_breaks) - 1),
      breaks            = mat_breaks,
      border_color      = NA,
      cluster_cols      = mat_cluster_cols,
      cluster_rows      = mat_cluster_rows,
      cellwidth         = 20,
      show_colnames     = TRUE,
      show_rownames     = FALSE,
      annotation_col    = mat_col,
      annotation_colors = mat_colors,
      drop_levels       = TRUE,
      fontsize          = 14,
      main              = "Rotated Column Names"
    )
    
    
    # Draw heatmaps
    pheatmap(test)
    
    min(test)
    bk <- c(seq(-.25,0,by=0.001),seq(0.01,8,by=0.01))
    length(bk)
    which(bk==0)
    # 做热图:
    pheatmap(test,
             scale = "none",
             color = c(colorRampPalette(colors = c("blue","white"))(length(bk)/4.2),colorRampPalette(colors = c("white","red"))(length(bk)/1.32)),
             legend_breaks=seq(-.25,8,1),breaks=bk
    )
    

    相关文章

      网友评论

        本文标题:pheatmap热图技巧合集

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