plotly / plotly.R

An interactive graphing library for R
https://plotly-r.com
Other
2.57k stars 626 forks source link

plotly ignores scale_fill_gradient2 #2073

Open acvill opened 3 years ago

acvill commented 3 years ago

Thank you for writing and maintaining plotly!

When making a scatterplot, I like to specify pch = 21 to print an outline around points. This helps distinguish points when they are close together. When I use this plotting scheme, ggplot2 gives the expected output, but plotly ignores the aesthetics supplied by scale_fill_gradient2() and gives the error number of items to replace is not a multiple of replacement length. I believe this issue is similar to https://github.com/plotly/plotly.R/issues/1149.

# R v4.0.5
library(plotly) # v 4.9.4.1
library(ggplot2) # v 3.3.3

p <- ggplot(data = wdat, mapping = aes(x = Age, y = as.numeric(ABV))) +
  geom_jitter(mapping = aes(fill = Score), size = 2.5, pch = 21) +
  scale_fill_gradient2(low = "#0008ff",
                       midpoint = mean(wdat$Score),
                       mid = "purple",
                       high = "#ff0000") +
  scale_y_continuous(name = "% ABV",
                     limits = c(40, 70),
                     breaks = seq(from = 40, 70, 5)) +
  theme_classic()

p + ggtitle("ggplot2")

ggplotly(p + ggtitle("plotly"))

ggplot2 plotly

If I switch my mapping aesthetic to color with pch = 19 and specify a gradient with scale_color_gradient2(), then ggplot2 and plotly give the same output without error. However, the desired outlines are lost.

p <- ggplot(data = wdat, mapping = aes(x = Age, y = as.numeric(ABV))) +
  geom_jitter(mapping = aes(color = Score), size = 2.5, pch = 19) +
  scale_color_gradient2(low = "#0008ff",
                       midpoint = mean(wdat$Score),
                       mid = "purple",
                       high = "#ff0000") +
  scale_y_continuous(name = "% ABV",
                     limits = c(40, 70),
                     breaks = seq(from = 40, 70, 5)) +
  theme_classic()

p + ggtitle("ggplot2")

ggplotly(p + ggtitle("plotly"))

ggplot2b plotlyb

Here is my scheme for downloading the dataset, in case you want to troubleshoot with the same data:

library(dplyr) # v 1.0.5
library(gsheet) # v 0.4.5

url <- "https://docs.google.com/spreadsheets/d/1geSFmGKWhoSzoSblQQK6fDzWSujY-MQ9w2b3VNbTxZk"

wdat <- gsheet::construct_download_url(url = url) %>%
  gsheet::gsheet2tbl(url = .) %>%
  mutate(ABV = gsub("%","",ABV) %>%
           as.numeric() %>%
           round(., 3) %>%
           format(., nsmall = 1)) %>%
  na.omit()
moxboxwa commented 2 years ago

And here is a minimal reprex using the example from the plotly gradient scale help page

library(plotly)

df <- data.frame(
  x = runif(100),
  y = runif(100),
  z1 = rnorm(100),
  z2 = abs(rnorm(100))
)

df_na <- data.frame(
  value = seq(1, 20),
  x = runif(20),
  y = runif(20),
  z1 = c(rep(NA, 10), rnorm(10))
)
p <-    
  ggplot(df, aes(x, y)) +
  geom_point(shape=21,
             aes(fill = z1)) +
  scale_fill_gradient2()
plotly::ggplotly(p)

generates this warning:

Warning message:
In L$marker$color[idx] <- aes2plotly(data, params, "fill")[idx] :
  number of items to replace is not a multiple of replacement length

and this plot

image

> sessionInfo()
R version 4.1.2 (2021-11-01)
Platform: x86_64-w64-mingw32/x64 (64-bit)
Running under: Windows 10 x64 (build 19044)

Matrix products: default

locale:
[1] LC_COLLATE=English_United States.1252  LC_CTYPE=English_United States.1252   
[3] LC_MONETARY=English_United States.1252 LC_NUMERIC=C                          
[5] LC_TIME=English_United States.1252    

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

loaded via a namespace (and not attached):
 [1] pillar_1.6.4       compiler_4.1.2     tools_4.1.2        digest_0.6.29     
 [5] viridisLite_0.4.0  jsonlite_1.7.2     lifecycle_1.0.1    tibble_3.1.6      
 [9] gtable_0.3.0       pkgconfig_2.0.3    rlang_0.4.12       DBI_1.1.2         
[13] cli_3.1.0          rstudioapi_0.13    crosstalk_1.2.0    yaml_2.2.1        
[17] fastmap_1.1.0      dplyr_1.0.7        httr_1.4.2         generics_0.1.1    
[21] vctrs_0.3.8        htmlwidgets_1.5.4  grid_4.1.2         tidyselect_1.1.1  
[25] glue_1.6.0         data.table_1.14.2  R6_2.5.1           fansi_0.5.0       
[29] plotly_4.10.0.9000 farver_2.1.0       ggplot2_3.3.5      purrr_0.3.4       
[33] tidyr_1.1.4        magrittr_2.0.1     scales_1.1.1       ellipsis_0.3.2    
[37] htmltools_0.5.2    assertthat_0.2.1   colorspace_2.0-2   labeling_0.4.2    
[41] utf8_1.2.2         lazyeval_0.2.2     munsell_0.5.0      crayon_1.4.2