Home > database >  inner labelling for heatmap, in R ggplot
inner labelling for heatmap, in R ggplot

Time:02-27

I am trying to add a number label on each cell of a heatmap. Because it also needs marginal barcharts I have tried two packages. iheatmapr and ComplexHeatmap.

(1st try) iheatmapr makes it easy to add to add bars as below, but I couldnt see how to add labels inside the heatmap on individual cells.

library(tidyverse)
library(iheatmapr)
library(RColorBrewer)

in_out <- data.frame(
  'Economic' = c(2,1,1,3,4),
  'Education' = c(0,3,0,1,1),
  'Health' = c(1,0,1,2,0),
  'Social' = c(2,5,0,3,1) )
rownames(in_out) <- c('Habitat', 'Resource', 'Combined', 'Protected', 'Livelihood')

GreenLong <- colorRampPalette(brewer.pal(9, 'Greens'))(12)
lowGreens <- GreenLong[0:5]

in_out_matrix <- as.matrix(in_out)
main_heatmap(in_out_matrix, colors = lowGreens)

in_out_plot <- iheatmap(in_out_matrix,
  colors=lowGreens) %>% 
  add_col_labels() %>% 
  add_row_labels() %>% 
  add_col_barplot(y = colSums(bcio)/total) %>% 
  add_row_barplot(x = rowSums(bcio)/total)
in_out_plot

Then used: save_iheatmap(in_out_plot, "iheatmapr_test.png") Because I couldnt use ggsave(device = ragg::agg_png etc) with iheatmapr object.

saved with iheatmap

Also, the iheatmapr object's apparent incompatibility (maybe I am wrong) with ggsave() is a problem for me because I normally use ragg package to export image AGG to preserve font sizes. I am suspecting some other heatmap packages make custom objects that maybe incompatible with patchwork and ggsave.

ggsave("png/iheatmapr_test.png", plot = in_out_plot,
       device = ragg::agg_png, dpi = 72,
       units="in", width=3.453, height=2.5,
       scaling = 0.45)

(2nd try) ComplexHeatmap makes it easy to label individual number "cells" inside a heatmap, and also offers marginal bars among its "Annotations", and I have tried it, but its colour palette system (which uses integers to refer to a set of colours) doesnt suit my RGB vector colour gradient, and overall it is a sophisticated package clearly designed to make graphics more advanced than what I am doing.

I am aiming for style as shown in screenshot example below, which was made in Excel. Excel heatmap with bars and labelled cells

Please can anyone suggest a more suitable R package for a simple heatmap like this with marginal bars, and number labels inside?

CodePudding user response:

Instead of relying on packages which offer out-of-the-box solutions one option to achieve your desired result would be to create your plot from scratch using ggplot2 and patchwork which gives you much more control to style your plot, to add labels and so on.

Note: The issue with iheatmapr is that it returns a plotly object, not a ggplot. That's why you can't use ggsave.

library(tidyverse)
library(patchwork)

in_out <- data.frame(
  'Economic' = c(1,1,1,5,4),
  'Education' = c(0,0,0,1,1),
  'Health' = c(1,0,1,0,0),
  'Social' = c(1,1,0,3,1) )
rownames(in_out) <- c('Habitat', 'Resource', 'Combined', 'Protected', 'Livelihood')

in_out_long <- in_out %>% 
  mutate(y = rownames(.)) %>% 
  pivot_longer(-y, names_to = "x")

# Summarise data for marginal plots
yin <- in_out_long %>% 
  group_by(y) %>% 
  summarise(value = sum(value)) %>% 
  mutate(value = value / sum(value))

xin <- in_out_long %>% 
  group_by(x) %>% 
  summarise(value = sum(value)) %>% 
  mutate(value = value / sum(value))

# Heatmap
ph <- ggplot(in_out_long, aes(x, y, fill = value))  
  geom_tile()  
  geom_text(aes(label = value), size = 8 / .pt)  
  scale_fill_gradient(low = "#F7FCF5", high = "#00441B")  
  theme(legend.position = "bottom")  
  labs(x = NULL, y = NULL, fill = NULL)

# Marginal plots
py <- ggplot(yin, aes(value, y))  
  geom_col(width = .75)  
  geom_text(aes(label = scales::percent(value)), hjust = -.1, size = 8 / .pt)  
  scale_x_continuous(expand = expansion(mult = c(.0, .25)))  
  theme_void()

px <- ggplot(xin, aes(x, value))  
  geom_col(width = .75)  
  geom_text(aes(label = scales::percent(value)), vjust = -.5, size = 8 / .pt)  
  scale_y_continuous(expand = expansion(mult = c(.0, .25)))  
  theme_void()

# Glue plots together
px   plot_spacer()   ph   py   plot_layout(ncol = 2, widths = c(2, 1), heights = c(1, 2))

  • Related