Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

ggplotly mouse values while using a log-transformed color scale

Tags:

r

ggplot2

plotly

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)
like image 627
Yo B. Avatar asked Aug 09 '26 20:08

Yo B.


1 Answers

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

like image 141
RLave Avatar answered Aug 11 '26 13:08

RLave



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!