clattenburg cake
clattenburg cake

Reputation: 1222

Make Heatmap Look More Professional in R

I can build very basic plots in R, but I'm trying to make my heatmap look more professional and I'm not sure how to do it. I have a data frame df of 11 observations of 11 variables:

> dput(df)
structure(list(`0` = c(6.08, 7.91, 5.14, 2.23, 0.72, 0.19, 0.04, 
0.01, 0, 0, 0), `1` = c(9.12, 11.86, 7.71, 3.34, 1.09, 0.28, 
0.06, 0.01, 0, 0, 0), `2` = c(6.84, 8.89, 5.78, 2.5, 0.81, 0.21, 
0.05, 0.01, 0, 0, 0), `3` = c(3.42, 4.45, 2.89, 1.25, 0.41, 0.11, 
0.02, 0, 0, 0, 0), `4` = c(1.28, 1.67, 1.08, 0.47, 0.15, 0.04, 
0.01, 0, 0, 0, 0), `5` = c(0.38, 0.5, 0.33, 0.14, 0.05, 0.01, 
0, 0, 0, 0, 0), `6` = c(0.1, 0.13, 0.08, 0.04, 0.01, 0, 0, 0, 
0, 0, 0), `7` = c(0.02, 0.03, 0.02, 0.01, 0, 0, 0, 0, 0, 0, 0
), `8` = c(0, 0.01, 0, 0, 0, 0, 0, 0, 0, 0, 0), `9` = c(0, 0, 
0, 0, 0, 0, 0, 0, 0, 0, 0), `10+` = c(0, 0, 0, 0, 0, 0, 0, 0, 
0, 0, 0)), row.names = c("0", "1", "2", "3", "4", "5", "6", "7", 
"8", "9", "10+"), class = "data.frame")

So I transform df into a matrix to get a heatmap:

heatmap(data.matrix(df), Rowv=NA, Colv=NA, col = heat.colors(256), scale="column", margins=c(5,10))

This is what the plot looks like:

enter image description here

I'm not sure how to:

  1. Change the location of the keys for the row and columns. I want them both to start from 0 in the top left corner, and both row and column to continue ascending until 10+
  2. I'd also like more granularity in the colour. Right now you can't event tell the difference in values by looking at the colour...

Is heatmap from base R even the right library for this? I looked up a few examples and I wasn't sure if there's a better library to achieve what I want.

Upvotes: 0

Views: 1702

Answers (1)

Alexlok
Alexlok

Reputation: 3134

There are several libraries that offer heatmap functionalities. IMO base heatmap and gplots::heatmap.2 did not age well and are not the best options anymore. 3 good possibilities are with ggplot2::geom_tile, pheatmap and ComplexHeatmap.

Example data

Let's assume we have a matrix

dta <- matrix(rnorm(25), nrow=5)
rownames(dta) <- letters[1:5]
colnames(dta) <- LETTERS[1:5]

ggplot2::geom_tile

The ggplot2 version requires your data to be a tidy dataframe, so we can transform our matrix with tidyr::pivot_longer().

dta %>%
  as_tibble(rownames = "myrows") %>%
  pivot_longer(cols = -myrows, names_to = "mycols", values_to = "level") %>%
  ggplot() +
  geom_tile(aes(x=myrows, y=mycols, fill = level))

pheatmap

The pheatmap package is quite good at generating modern heatmaps. It takes a matrix as input. It can cluster the rows and columns and make a dendrogram, which is often a desired feature. It can also scale rows and columns (effectively plotting a Z-score).

pheatmap::pheatmap(dta,
                   scale = "none",
                   cluster_rows = FALSE,
                   cluster_cols = FALSE)

Note that the positions of rows and columns are not the same as with ggplot. You can look at the options that allow some useful customization. For example, if our rows have classes defined elsewhere.

ann_df <- data.frame(row.names = rownames(dta),
                     classification = rep(c("first", "second"), times = c(2,3)))
pheatmap::pheatmap(dta,
                   scale = "none",
                   cluster_rows = FALSE,
                   cluster_cols = FALSE,
                   annotation_row = ann_df,
                   gaps_row = c(2))

Color scale

One of the big aspects that make your heatmap look professional is the color scale. On ggplot, you should check out scale_fill_gradient2().

On pheatmap, you can try these settings for color as a starting point (see the documentation of these functions):

color = scales::div_gradient_pal(low = "navy",
  mid = "green",
  high="yellow")(seq(0,1,
    length.out = max(dta))),

color = colorRampPalette(RColorBrewer::brewer.pal(n = 9,
      name = "Blues"))(max(dta)),

color = viridisLite::plasma(max(dta)),

ComplexHeatmap

Finally, a package that has gained success recently is ComplexHeatmap. It is based on pheatmap but offers many additional options. See the link in zx8754's comment for a detailed book full of examples.

Upvotes: 2

Related Questions