talgalili / heatmaply

Interactive Heat Maps for R Using plotly
380 stars 73 forks source link

Colour not applied to heatmap with single data point #303

Open WPettersson opened 9 months ago

WPettersson commented 9 months ago

Describe the bug

I am displaying a heatmap with a single data point, but its colour is grey despite passing in limits or scale_fill_gradient_fun.

I realise a heatmap with a single data point is silly, it's an edge case in part of a shiny app, details below in additional content.

To Reproduce

dd <- data.frame(c(1,2))
gradient <- scale_fill_gradient(limits=c(-3,3), low="#EE0000", high="#00EE00")

heatmaply(dd, dendrogram=FALSE, limits=c(-3,3))
# A heatmap of two cells is shown, with appropriate colours
heatmaply(dd, dendrogram=FALSE, limits=c(-3,3), scale_fill_gradient_fun=gradient)
# Heatmap with different colours shows

dd <- data.frame(c(1))
heatmaply(dd, dendrogram=FALSE, limits=c(-3,3))
# A heatmap of one grey cell is shown
heatmaply(dd, dendrogram=FALSE, limits=c(-3,3), scale_fill_gradient_fun=gradient)
# Heatmap with one grey cell is shown

Expected behavior

If only one data point is given, and either limits or a gradient with limits are also given, then the data point should be coloured the appropriate colour.

Additional context

It definitely seems weird to show a heatmap with one cell, especially in a static setting. However, in my setting I am creating a Shiny webapp where users can disable/enable various rows and columns of two different heatmaps. Each heatmap corresponds to a different technique, so it makes no sense to combine the data into one heatmap. However, even if one heatmap is reduced to a single cell, it still makes sense to compare the value of this cell, to whatever values are shown in the other heatmap. I am sharing limits and gradients across both heatmaps, so it would be useful to have the colours applied correctly in both heatmaps.

To expand on my situation, imagine data tracking "time taken to get from A to B" across some vehicles. I have one data set for ground-based vehicles, and the heatmap has one axis for "number of wheels" and the other for "size of engine". On the other data set, I have water-based vehicles, and the heatmap has one axis for "number of sails" and the other for "number of masts". It makes no sense to try to put all this data on one heatmap (because there's no data on land-vehicles with any number of masts, nor on water-vehicles with any number of wheels). However, it might still make sense to narrow down land vehicles to just one cell (i.e., 2 wheels and a 250cc engine) and compare that to all water vehicles. In this case, I can apply the same limits to my gradient, and would want the single cell in land vehicles to be coloured according to the gradient.

alanocallaghan commented 9 months ago

Would suggest you try setting plot_method="plotly", there's always been weird edge case issues with the scale_gradient fun and I've no time to investigate this at the moment

WPettersson commented 9 months ago

Thanks, that worked for me. Happy for you to either close this or leave it open depending on whether you (or someone) will want to investigate in the future.

alanocallaghan commented 9 months ago

Will leave it open, should be fixed eventually, cheers