2

Similar questions have been asked about changing the colour of the pop-up box when using ggplotly from the plotly package, however, none seem to solve my particular issue.

I have a heatmap, created using ggplot and I'm using ggplotly to make it interactive. Currently when you hover over a cell in the heatmap, the background colour of the pop-up box is grey. I am trying to make the background colour of the pop-up box to match the colour of the heatmap cell the mouse is hovering over.

To illustrate what I want, the 1st code block below show an example of using geom_point. When you run the code and hover over a point, the pop-up box is coloured the same as the point.

library(ggplot2)
library(poorly)

p <- ggplot() +
  geom_point(data = iris, aes(x = Sepal.Length, y = Sepal.Width, color = Species))

ggplotly(p)

I believe the colour of the pop-up box may be controlled by the color aesthetic, because when I create a heatmap (like the one below) and have to use fill, the pop-up box is coloured grey.

library(ggplot2)
library(plotly)

# Dummy data
x <- LETTERS[1:5]
y <- paste0("var", seq(1,5))
data <- expand.grid(X=x, Y=y)
data$Z <- runif(25, 0, 5)

# colour palette
pal <- colorRampPalette(c("darkblue", 'white', 'darkred'))(100)

# Heatmap 
p <- ggplot(data, aes(X, Y, fill= Z)) + 
  geom_tile() + scale_fill_gradientn(colors=pal)

ggplotly(p)

Any suggestions as to how I could get the po-up box background colour to match the colour of the cell it is hovering over in a heatmap?

zx8754
  • 52,746
  • 12
  • 114
  • 209
Electrino
  • 2,636
  • 3
  • 18
  • 40

1 Answers1

0

You fill should be a factor in geom_tile. You could cut your colors based on the number of observations to create a discrete value but have multiple colors. Here is a workaround:

library(ggplot2)
library(plotly)
library(dplyr)

# Dummy data
x <- LETTERS[1:5]
y <- paste0("var", seq(1,5))
data <- expand.grid(X=x, Y=y)
data$Z <- runif(25, 0, 5)

# colour palette
pal <- colorRampPalette(c("darkblue", 'white', 'darkred'))(25)

# cut colors
data$Z2 = cut(data$Z, 25)
# Heatmap 
p <- ggplot(data, aes(X, Y, fill = factor(Z2))) + 
  geom_tile() +
  scale_fill_manual(values=pal)
ggplotly(p) %>%
  layout(showlegend = FALSE)

enter image description here

Created on 2023-04-19 with reprex v2.0.2

As you can see the background color is now the same.

Quinten
  • 35,235
  • 5
  • 20
  • 53
  • Your method works for colouring the background but at the expense of the colour scale and the pop-up text now showing `Z2` instead of `Z` (the latter of the 2 issues Im sure is easily fixed). For the colour scale, using your method results in mainly blue colours being used and few reds (i.e., it's not using the full range of the colour palette). Lowering the amount of colours selected from `colorRampPallete` will allow the full range of colours to be used, but I would rather keep this at 100, as it is part of a function argument and I dont want to have to tweak this every time I create a plot – Electrino Apr 19 '23 at 17:53
  • I would also like to show the legend. Any suggestions as to how I could adapt your suggestion to do this? – Electrino Apr 19 '23 at 17:56