thomasp85 / gganimate

A Grammar of Animated Graphics
https://gganimate.com
Other
1.95k stars 310 forks source link

`transition_layers` and `enter_fade` not working together #475

Closed bbcuffer closed 1 year ago

bbcuffer commented 1 year ago

This example (from the help for transition_reveal) works for enter_grow but not for enter_fade.

 anim <- ggplot(mtcars, aes(mpg, disp)) +
       geom_point() +
       geom_smooth(colour = 'grey', se = FALSE) +
       geom_smooth(aes(colour = factor(gear))) +
       transition_layers(layer_length = 1, transition_length = 2) +
       enter_grow()

anim

All good to there. But

anim + enter_fade()

results in

`geom_smooth()` using method = 'loess' and formula = 'y ~ x'                     
 `geom_smooth()` using method = 'loess' and formula = 'y ~ x'                     
 Error in `vec_rbind()`:                                                          
 ! Can't combine `..1$fill` <logical> and `..2$fill` <character>.                 
 Run `rlang::last_error()` to see where the error occurred.                       
 There were 14 warnings (use warnings() to see them)  

My setup is as follows:

R version 4.2.1 (2022-06-23)                                                     
 Platform: x86_64-apple-darwin17.0 (64-bit)                                       
 Running under: macOS Big Sur ... 10.16                                           

 Locale: en_GB.UTF-8 / en_GB.UTF-8 / en_GB.UTF-8 / C / en_GB.UTF-8 / en_GB.UTF-8  

 Package version:                                                                 
   abind_1.4-5         askpass_1.1         assertthat_0.2.1                       
   av_0.8.2            backports_1.4.1     base64enc_0.1-3                        
   bbbiz_0.0.0.9000    bbplot2_0.3         bit_4.0.4                              
   bit64_4.0.5         blob_1.2.3          boot_1.3.28                            
   brio_1.1.3          broom_1.0.2         bslib_0.4.0                            
   cachem_1.0.6        Cairo_1.6-0         callr_3.7.3                            
   car_3.1-1           carData_3.0-5       cellranger_1.1.0                       
   cli_3.6.0           clipr_0.8.0         colorspace_2.0-3                       
   compiler_4.2.1      config_0.3.1        corrplot_0.92                          
   cowplot_1.1.1       cpp11_0.4.3         crayon_1.5.2                           
   curl_4.3.3          data.table_1.14.2   DBI_1.1.3                              
   dbplyr_2.2.1        desc_1.4.2          diffobj_0.3.5                          
   digest_0.6.31       dplyr_1.0.10        dtplyr_1.2.2                           
   ellipsis_0.3.2      evaluate_0.19       fansi_1.0.3                            
   farver_2.1.1        fastmap_1.1.0       forcats_0.5.2                          
   fs_1.5.2            gargle_1.2.1        generics_0.1.3                         
   gganimate_1.0.8     ggplot2_3.4.0       ggpubr_0.5.0                           
   ggrepel_0.9.2       ggsci_2.9           ggsignif_0.6.4                         
   ggtext_0.1.2        gifski_1.6.6-1      glue_1.6.2                             
   googledrive_2.0.0   googlesheets4_1.0.1 graphics_4.2.1                         
   grDevices_4.2.1     grid_4.2.1          gridExtra_2.3                          
   gridtext_0.1.5      gtable_0.3.1        haven_2.5.1                            
   here_1.0.1          highr_0.10          hms_1.1.2                              
   htmltools_0.5.3     httr_1.4.4          ids_1.0.1                              
   isoband_0.2.7       jpeg_0.1.9          jquerylib_0.1.4                        
   jsonlite_1.8.4      knitr_1.41          labeling_0.4.2                         
   lattice_0.20.45     lifecycle_1.0.3     lme4_1.1.31                            
   lubridate_1.8.0     magick_2.7.3        magrittr_2.0.3                         
   markdown_1.1        MASS_7.3.58.1       Matrix_1.5.1                           
   MatrixModels_0.5.1  memoise_2.0.1       methods_4.2.1                          
   mgcv_1.8.40         mime_0.12           minqa_1.2.5                            
   modelr_0.1.9        munsell_0.5.0       nlme_3.1.159                           
   nloptr_2.0.3        nnet_7.3.17         numDeriv_2016.8.1.1                    
   OECD_0.2.5          openssl_2.0.3       pacman_0.5.1                           
   parallel_4.2.1      pbkrtest_0.5.1      pillar_1.8.1                           
   piton_1.0.0         pkgbuild_1.3.1      pkgconfig_2.0.3                        
   pkgload_1.3.2       png_0.1-8           polynom_1.4.1                          
   praise_1.0.0        prettyunits_1.1.1   processx_3.8.0                         
   progress_1.2.2      ps_1.7.2            purrr_1.0.1                
   quantreg_5.94       R6_2.5.1            ragg_1.2.4                             
   rappdirs_0.3.3      RColorBrewer_1.1.3  Rcpp_1.0.9                             
   RcppEigen_0.3.3.9.3 rcutils_0.0.0.9000  readr_2.1.2                            
   readsdmx_0.3.0      readxl_1.4.1        rematch_1.0.1                          
   rematch2_2.1.2      remotes_2.4.2       reprex_2.0.2                           
   rlang_1.0.6         rmarkdown_2.16      rprojroot_2.0.3                        
   rstatix_0.7.1       rstudioapi_0.14     rvest_1.0.3                            
   sass_0.4.2          scales_1.2.1        selectr_0.4-2                          
   SparseM_1.81        splines_4.2.1       stats_4.2.1                            
   stringi_1.7.8       stringr_1.5.0       survival_3.4.0                         
   svglite_2.1.0       sys_3.4             systemfonts_1.0.4                      
   testthat_3.1.6      textshaping_0.3.6   tibble_3.1.8                           
   tidyr_1.2.1         tidyselect_1.2.0    tidyverse_1.3.2                        
   tidyxl_1.0.8        tinifyr_0.2.0       tinytex_0.41                           
   tools_4.2.1         tweenr_2.0.2        tzdb_0.3.0                             
   unpivotr_0.6.2      utf8_1.2.2          utils_4.2.1                            
   uuid_1.1.0          vctrs_0.5.1         viridisLite_0.4.1                      
   vroom_1.5.7         waldo_0.4.0         withr_2.5.0                            
   xfun_0.36           xml2_1.3.3          yaml_2.3.6         
linzi-sg commented 1 year ago

I took a look at this. The error was triggered when animating the geom_point layer, because GeomPoint's default_aes for fill is NA (i.e. a logical constant), while part of the transformation done by enter_fade (see gganimate:::transmute_fade for details) involves applying alpha() on various other aesthetic mappings. In particular, the following code

x$fill[no_alpha] <- alpha(x$fill[no_alpha], alpha)

will convert a column of NAs to NA_character_s, which leads to the error when vec_rbind() tries to combine a dataframe of original points (where the fill column is logical) with a dataframe of tweened points (where the fill column has become character).

Workaround

Changing geom_point() to geom_point(fill = NA_character_) in the example would mitigate the issue here. In the long run, though, it may be worthwhile to include some check on the input & output column classes within gganimate:::transmute_fade.

tnguyenqh commented 1 year ago

I had the same error today and i have tried the suggested solution and I had another error: _Error in transform_path(data[0, ], data, ease, enterlength, NULL, enter, : The transformr package is required to tween paths and lines

thomasp85 commented 1 year ago

@tnguyenqh as the message indicates you need to install the transformr package

thomasp85 commented 1 year ago

This has been fixed in the dev version of tweenr