tidyverse / ggplot2

An implementation of the Grammar of Graphics in R
https://ggplot2.tidyverse.org
Other
6.39k stars 2k forks source link

ggplotly does not work when adding scale_fill_binned #5926

Closed thgsponer closed 1 month ago

thgsponer commented 1 month ago

I had exactly the same issue as described in #5892. After updating the plotly package to version 4.10.4 most of my plots worked well.

However, in the follwing situation ggplotly does not work properly.

library(ggplot2)
library(plotly)

xy_grid <- expand.grid(ps=seq(-1,3, length=100),
                       trs=seq(-1,3,length=100))
xy_grid$ptl <-  xy_grid$ps^2 + xy_grid$trs^2

theme_set(theme_bw())
g1 <- ggplot(xy_grid) +
  geom_raster(aes(y=ps,x=trs,fill=ptl), interpolate = TRUE) +
  geom_contour(aes(y=ps,x=trs,z=ptl), breaks = c(2,4,6,8,10), color='black')

# this works
ggplotly(g1)

# adding scale_fill_binned
g2 <- g1 + scale_fill_binned(low = 'red', high = 'green', breaks = c(2,4,6,8,10))

# this does not work
ggplotly(g2)

For the second plot (g2), when using ggplotly I get the following error:

Error in `[.data.frame`(decor, c("value", "colour")) : 
  undefined columns selected

sessionInfo:

R version 4.3.2 (2023-10-31 ucrt)
Platform: x86_64-w64-mingw32/x64 (64-bit)
Running under: Windows 11 x64 (build 22631)

Matrix products: default

locale:
[1] LC_COLLATE=English_Switzerland.utf8  LC_CTYPE=English_Switzerland.utf8   
[3] LC_MONETARY=English_Switzerland.utf8 LC_NUMERIC=C                        
[5] LC_TIME=English_Switzerland.utf8    

time zone: Europe/Zurich
tzcode source: internal

attached base packages:
[1] stats     graphics  grDevices datasets  utils     methods   base     

other attached packages:
[1] plotly_4.10.4 ggplot2_3.5.1

The example above works in the following session:

R version 4.3.2 (2023-10-31 ucrt)
Platform: x86_64-w64-mingw32/x64 (64-bit)
Running under: Windows 11 x64 (build 22631)

Matrix products: default

locale:
[1] LC_COLLATE=English_Switzerland.utf8  LC_CTYPE=English_Switzerland.utf8   
[3] LC_MONETARY=English_Switzerland.utf8 LC_NUMERIC=C                        
[5] LC_TIME=English_Switzerland.utf8    

time zone: Europe/Zurich
tzcode source: internal

attached base packages:
[1] stats     graphics  grDevices utils     datasets  methods   base     

other attached packages:
[1] plotly_4.10.3 ggplot2_3.4.4
teunbrand commented 1 month ago

We updated guide_coloursteps() recently, and I think plotly just hasn't caught up yet. It is up to {plotly} to decide what features they implement and which they don't, it isn't really something that can be fixed in ggplot2. The alternative is to reverse the change, but we made the change for a good reason. You can use guide = "colourbar" to display the regular colourbar guide.