ggplotly heatmap renders wrong gray color

Viewed 26

I'm trying to build a heatmap with ggplot2 and plotly. I found a corner case where the value for one of the heatmap rows is only one. In that case, the ggplot output is as expected, but when you render it with ggplotly, this value turns gray. See the code and snapshots below. It seems like a bug to me.

library(ggplot2)
library(plotly)

theme_set(theme_gray())

PROBS <- c(50, 60, 70, 80, 90, 95, 100)
gradientPalette <- colorRampPalette(c("#5cd400", "#fff100", "#d4000f"),
                                    bias = .25)

set.seed(1)
data <- data.frame(
  x = rep(seq(1, 100), 2),
  y = c(rep("V1", 100), rep("V2", 100)),
  z = c(sample(PROBS, 100, replace = T, prob = 1.01 - PROBS/100), rep(50, 100)),
  group = c(rep("G1", 100), rep("G2", 100))
)

p <- data %>% 
  ggplot(aes(x, y)) +
  geom_tile(aes(fill = z)) +
  facet_wrap(~ group, nrow = 2, scales = "free_y") +
  scale_fill_gradientn(name = NULL, 
                       colours = gradientPalette(length(PROBS) + 1),
                       limits = c(20, 100),
                       breaks = PROBS*100,
                       labels = PROBS*100)

p
ggplotly(p)

ggplot output enter image description here

ggplotly faulty output

enter image description here

Session info.

R version 4.1.3 (2022-03-10)
Platform: x86_64-pc-linux-gnu (64-bit)
Running under: Ubuntu 20.04.4 LTS

Matrix products: default
BLAS:   /usr/lib/x86_64-linux-gnu/openblas-pthread/libblas.so.3
LAPACK: /usr/lib/x86_64-linux-gnu/openblas-pthread/liblapack.so.3

locale:
 [1] LC_CTYPE=en_US.UTF-8       LC_NUMERIC=C               LC_TIME=en_US.UTF-8        LC_COLLATE=en_US.UTF-8     LC_MONETARY=en_US.UTF-8   
 [6] LC_MESSAGES=en_US.UTF-8    LC_PAPER=en_US.UTF-8       LC_NAME=C                  LC_ADDRESS=C               LC_TELEPHONE=C            
[11] LC_MEASUREMENT=en_US.UTF-8 LC_IDENTIFICATION=C       

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

other attached packages:
[1] plotly_4.9.4.1 ggplot2_3.3.5 

loaded via a namespace (and not attached):
 [1] pillar_1.6.1      compiler_4.1.3    tools_4.1.3       digest_0.6.27     jsonlite_1.7.2    lifecycle_1.0.0   tibble_3.1.2     
 [8] gtable_0.3.0      viridisLite_0.4.0 pkgconfig_2.0.3   rlang_0.4.11      crosstalk_1.1.1   yaml_2.2.1        withr_2.4.2      
[15] dplyr_1.0.7       httr_1.4.2        generics_0.1.0    vctrs_0.3.8       htmlwidgets_1.5.3 grid_4.1.3        tidyselect_1.1.1 
[22] glue_1.4.2        data.table_1.14.2 R6_2.5.0          fansi_0.5.0       purrr_0.3.4       tidyr_1.1.3       farver_2.1.0     
[29] magrittr_2.0.1    scales_1.1.1      ellipsis_0.3.2    htmltools_0.5.1.1 colorspace_2.0-2  renv_0.13.2       labeling_0.4.2   
[36] utf8_1.2.1        lazyeval_0.2.2    munsell_0.5.0     crayon_1.4.1   
0 Answers
Related