How to zoom in on/extract a subsection of/extract colours from a heatmap.2 in R?

232 views Asked by At

I'm creating heatmaps in R using heatmap.2 (I think it needs to be heatmap.2 because I'm using 1 dataset to generate the colours of the heatmap and a second dataset to overlay numerical data).

Here is a sample of my code so far. The actual data set is 30 columns and 1000 rows.

heatmap_all_data <-
    data.frame(name = c("John", "Mark", "Luke", "Jack", "Will", "Jim", "Clive", "Steve"),
               trait_1 = c(1, 2, 5, 8, 5, 3, 7, 8),
               trait_2 = c(5, 7, 3, 4, 6, 3, 2, 1)) %>%
    column_to_rownames(var="name")

heatmap_colour <- colorRampPalette(brewer.pal(11, "RdYlBu"))(1000)

heatmap.2(as.matrix(heatmap_all_data),
          scale = "column",
          key = FALSE,
          dendrogram = "none",
          Rowv = FALSE,
          Colv = FALSE,
          trace = "none",
          col = rev(heatmap_colour),
          labRow = row.names(heatmap_all_data))

Which generates the following heatmap: https://i.stack.imgur.com/lK8Sc.png

NOW, the problem is I only want a subsection of this data, e.g I want the following heatmap:

heatmap_part_data <-
    data.frame(name = c("John", "Mark", "Luke"),
               trait_1 = c(1, 2, 5),
               trait_2 = c(5, 7, 3)) %>%
    column_to_rownames(var="name")

heatmap_colour <- colorRampPalette(brewer.pal(11, "RdYlBu"))(1000)

heatmap.2(as.matrix(heatmap_part_data),
          scale = "column",
          key = FALSE,
          dendrogram = "none",
          Rowv = FALSE,
          Colv = FALSE,
          trace = "none",
          col = rev(heatmap_colour),
          labRow = row.names(heatmap_part_data))

https://i.stack.imgur.com/j33Ic.png

BUT, I want each cell to keep the same colours as the original. I.e. I want the colours in my subsetted heatmap to be relative to the total data and not just the subsetted data. (In the real example I want to show 10 out of 1000 entries).

So, I need to either "zoom in" and rescale the top section of the heatmap and then crop the image, extract the top section of the heatmap into a new object while maintaining the same colours, or extract information about the colours in the full heatmap and overwrite the default colours in the subsetted heatmap.

The goal is basically to output an image of the subsetted data heatmap with each colour in each cell the same as in the all_data heatmap.

I hope this is clear - please advise if you need any clarification!

Many thanks for taking the time to read and I hope someone can help.

Best, Ryan

1

There are 1 answers

0
Ryand956 On

Found the solution!

So I switched from heatmap.2 to heatmaply - same functionality but with interactivity. With heatmaply you can drag an area over the heatmap and zoom into that area which gives the desired result but I wanted to consistently zoom to a specific area.

From this website (https://plotly.com/r/axes/) I found out about the Layout function of the wider plotly library (that heatmaply is part of).

So to the existing code you can add:

%>% layout(yaxis = list(range = c(10.5, 0.5)))

(Need to add 0.5 to centre the rows properly)

Et voila! The heatmap colours are generated relative to the wider dataset but only a subset is shown.