Search code examples
pythonrggplot2heatmapcomplexheatmap

Heatmaps with each box represents multiple sources using R


I would like to plot heatmaps that are similar to this: enter image description here

where there are three rows corresponding to each box. I am thinking about using the ComplexHeatmap package in R but couldn't find an example, any suggestions are welcomed!


Solution

  • Here's a ggplot2 example. The code after facet_grid is fiddling the theme to look more like your example.

    df <- data.frame(row = sample(LETTERS[1:10], 1e3, replace = TRUE),
                     col = sample(letters[11:20], 1e3, replace = TRUE),
                     segment = sample(1:3, 1e3, replace = TRUE, prob = 1:3))
    
    
    library(tidyverse)
    df %>%
      count(col, row, segment) %>%
      ggplot(aes(x = 1, y = segment, fill = scale(n))) +
      geom_tile() +
      scale_fill_distiller(palette = "RdBu") +
      facet_grid(row~col, switch = "both") +
      scale_y_continuous(expand = c(0,0)) +
      scale_x_continuous(expand = c(0,0)) +
      labs(x = NULL, y = NULL) +
      theme_classic() +
      theme(panel.spacing = unit(0, "line"),
            panel.border = element_rect(fill = NA),
            axis.text = element_blank(),
            axis.ticks = element_blank())
    

    enter image description here