bgfritz1
bgfritz1

Reputation: 59

ComplexHeatmap - Highlight cells by changing alpha

I would like to highlight certain cells in the matrix by changing their transparency (i.e. all cells where highlight_matrix are TRUE should have an opacity/alpha of 0 and be solid colored, while FALSE should be semi-transparent.

# Starting Matrix
mat <- matrix(runif(25), nrow = 5, ncol = 5)

#Matix showing which cells to highlight 

highlight_matrix <- matrix(sample(c(TRUE, FALSE), 25, replace = TRUE), nrow = 5)

#Colors
my_colors <- circlize::colorRamp2(c(0,1), c("#440154", "#FDE725"))
               
#Heatmap                                           
ComplexHeatmap::Heatmap(mat, col = my_colors)

I can use the col_fun argument to highlight the cells in other ways (below). However, I would really like to highlight the cells by changing the cells fill opacity instead.


ComplexHeatmap::Heatmap(mat,
                        col = my_colors,
                        cell_fun = function(j, i, x, y, width, height, fill) {

                          if(highlight_matrix[i, j]){
                            grid.points(x, y, pch = 7,
                                        gp = gpar(fontsize = 10))
                          }
                        })

Upvotes: 0

Views: 134

Answers (1)

Yun
Yun

Reputation: 305

You can use circlize::add_transparency to give the color transparency (See https://jokergoo.github.io/circlize/reference/add_transparency.html).

Another option would be ggplot2 - ggalign:

  library(grid)
  # Starting Matrix
  set.seed(1L)
  mat <- matrix(runif(25), nrow = 5, ncol = 5)
  rownames(mat) <- paste0("row", seq_len(nrow(mat)))
  colnames(mat) <- paste0("col", seq_len(ncol(mat)))

  # Matix showing which cells to highlight
  highlight_matrix <- matrix(sample(c(TRUE, FALSE), 25, replace = TRUE), nrow = 5)
  # Colors
  my_colors <- circlize::colorRamp2(c(0, 1), c("#440154", "#FDE725"))

  cheat_alpha <- ComplexHeatmap::Heatmap(mat,
    col = my_colors, rect_gp = gpar(type = "none"),
    cell_fun = function(j, i, x, y, width, height, fill) {
      if (!highlight_matrix[i, j]) {
        fill <- circlize::add_transparency(fill, 0.5)
      }
      grid::grid.rect(x, y,
        width = width, height = height,
        gp = gpar(fill = fill)
      )
    },
    column_title = "ComplexHeatmap with transpancy"
  )

  cheat_no_alpha <- ComplexHeatmap::Heatmap(mat, my_colors,
    column_title = "ComplexHeatmap no transpancy"
  )


  # ggalign maybe a little verbose, but gave you full ggplot2 syntax
  library(ggalign)

  highlight <- reshape2::melt(highlight_matrix,
    varnames = c(".y", ".x"),
    value.name = "highlight"
  )
  ggalign_alpha <- ggheatmap(mat, aes(alpha = alpha)) +
    hmanno(plot_data = function(data) {
      data <- dplyr::full_join(data, highlight, by = c(".x", ".y"))
      dplyr::mutate(data, alpha = ifelse(.data$highlight, 1, 0.5))
    }) +
    scale_alpha_identity() +
    scale_fill_gradient(low = "#440154", high = "#FDE725") +
    ggtitle("ggalign with transpancy")
  ggalign_no_alpha <- ggheatmap(mat) +
    scale_fill_gradient(low = "#440154", high = "#FDE725") +
    ggtitle("ggalign no transpancy")

  # ggalign can align ggplot2, pheatmap, ComplexHeatmap et al.
  align_plots(cheat_alpha, cheat_no_alpha,
    ggalign_alpha, ggalign_no_alpha,
    nrow = 2L
  )
})

Created on 2024-10-14 with reprex v2.1.0

enter image description here

Upvotes: 2

Related Questions