Yollanda Beetroot
Yollanda Beetroot

Reputation: 333

ggplotly mouse values while using a log-transformed color scale

In the minimal example below, where the color scale is log-transformed, the z value displayed when hovering the mouse above the raster is also log-transformed. This is very uninformative and I need it to be expressed in the same unit as the scale legend.

Is it possible to avoid plotly to automaticaly do this conversion?

library(plotly)
library(reshape2)
library(RColorBrewer)

myPalette <- colorRampPalette(brewer.pal(11, "Spectral"))

p <- volcano %>%
  melt() %>% 
  ggplot(aes(Var1, Var2, fill = value)) + geom_tile() +
  scale_fill_gradientn(colours = rev(myPalette(100)), trans="log")

ggplotly(p)

Upvotes: 3

Views: 270

Answers (1)

RLave
RLave

Reputation: 8374

A workaround is this, I just added the text = paste("Value:", value) part (which doesn't get affected by the log):

p <- volcano %>%
  melt() %>% 
  ggplot(aes(Var1, Var2, fill = value, text = paste("Value:", value))) + geom_tile() +
  scale_fill_gradientn(colours = rev(myPalette(100)), trans="log")

ggplotly(p, tooltip = c("Var1", "Var2", "text"))

Also tooltip to control what to show on hover.

enter image description here

Upvotes: 1

Related Questions