0

I'm pretty sure this is a bug, and am hoping in the short term for a workaround.

library(tidyverse)
library(plotly)
d <- tibble(
  date = as.Date("2012-01-01") + 0:5000,
  y = rnorm(5001)
)

p <- ggplot(d, aes (x = date, y = y)) + 
  geom_point() 

p

ggplotly(p)

Here is p as rendered by ggplot2:

enter image description here

The data correctly go up to September 2025.

But here it is as rendered by plotly:

enter image description here

All the data are there, and the tooltips correctly label the points at the right of that chart as referring to September 2025, but according to the x axis they are more like 2022 (in fact, it looks a just transposed four or five years out).

Any advice on a) if this is a bug or am I doing something silly and b) is there a workaround?

Edit to add sessionInfo()

> sessionInfo()
R version 3.6.3 (2020-02-29)
Platform: x86_64-w64-mingw32/x64 (64-bit)
Running under: Windows 10 x64 (build 16299)

Matrix products: default

locale:
[1] LC_COLLATE=English_Australia.1252  LC_CTYPE=English_Australia.1252    LC_MONETARY=English_Australia.1252
[4] LC_NUMERIC=C                       LC_TIME=English_Australia.1252    

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

other attached packages:
 [1] plotly_4.9.2    forcats_0.5.0   stringr_1.4.0   dplyr_0.8.5     purrr_0.3.3     readr_1.3.1    
 [7] tidyr_1.0.2     tibble_2.1.3    ggplot2_3.3.0   tidyverse_1.3.0

loaded via a namespace (and not attached):
 [1] tidyselect_1.0.0  haven_2.2.0       lattice_0.20-40   colorspace_1.4-1  vctrs_0.2.4       generics_0.0.2   
 [7] htmltools_0.4.0   viridisLite_0.3.0 yaml_2.2.1        rlang_0.4.5       pillar_1.4.3      glue_1.3.1       
[13] withr_2.1.2       DBI_1.1.0         dbplyr_1.4.2      modelr_0.1.6      readxl_1.3.1      lifecycle_0.2.0  
[19] munsell_0.5.0     gtable_0.3.0      cellranger_1.1.0  rvest_0.3.5       htmlwidgets_1.5.1 labeling_0.3     
[25] crosstalk_1.1.0.1 Cairo_1.5-11      fansi_0.4.1       broom_0.5.5       Rcpp_1.0.3        scales_1.1.0     
[31] backports_1.1.5   jsonlite_1.6.1    farver_2.0.3      fs_1.3.2          hms_0.5.3         digest_0.6.25    
[37] stringi_1.4.6     grid_3.6.3        cli_2.0.2         tools_3.6.3       magrittr_1.5      lazyeval_0.2.2   
[43] crayon_1.3.4      pkgconfig_2.0.3   data.table_1.12.8 xml2_1.2.5        reprex_0.3.0      lubridate_1.7.4  
[49] assertthat_0.2.1  httr_1.4.1        rstudioapi_0.11   R6_2.4.1          nlme_3.1-145      compiler_3.6.3  
Peter Ellis
  • 5,694
  • 30
  • 46
  • 2
    My X-axis goes up to 2025, similar to the ggplot. That is, your results are not reproducible on my computer. I'm using plotly 4.9.1 and ggplot2 3.2.1. – Edward Mar 23 '20 at 03:29
  • 1
    With plotly 4.9.2, it works too. – dc37 Mar 23 '20 at 03:49
  • Hmm, that's interesting. I have 4.9.2. I have added sessionInfo() from running this in a clean R session (I get the same output as before, but black rather than orange - that was a changed default in my previous setting - which doesn't impact the problem because the problem persists in the new session). – Peter Ellis Mar 23 '20 at 04:00
  • Thanks @Edward for mentioning the ggplot2 version number which sparked a successful line of inquiry - problem seems to have been introduced with ggplot2 3.3.0. – Peter Ellis Mar 23 '20 at 06:34

1 Answers1

1

Turns out this is something going wrong between plotly and the latest version of ggplot2. If I revert from ggplot2 3.3.0 to 3.2.1 the problem fixes itself:

devtools::install_version("ggplot2", version = "3.2.1", repos = "http://cran.us.r-project.org")

library(tidyverse)
library(plotly)

d <- tibble(
  date = as.Date("2012-01-01") + 0:5000,
  y = rnorm(5001)
)

p <- ggplot(d, aes (x = date, y = y)) + 
  geom_point() 

ggplotly(p)

enter image description here

So the workaround is to stick to ggplot2 3.2.1 for now and post a bug report for plotly (I don't think we can call it a ggplot2 bug as it only appears in plotly's interpretation of the scale).

Peter Ellis
  • 5,694
  • 30
  • 46