thomasp85 / gganimate

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

wiki script not reproducible #388

Open JVAQUEROM opened 4 years ago

JVAQUEROM commented 4 years ago

Hello,

I can't reproduce the script in Animation Composition.

> library(dplyr)

Attaching package: ‘dplyr’

The following objects are masked from ‘package:stats’:

    filter, lag

The following objects are masked from ‘package:base’:

    intersect, setdiff, setequal, union

> library(ggplot2)
> library(magick)
Linking to ImageMagick 7.0.7.34
Enabled features: cairo, fontconfig, freetype, fftw, lcms, pango, rsvg, webp, x11
Disabled features: ghostscript
> library(gganimate)
> 
> 
> A<-rnorm(100,50,10)
> B<-rnorm(100,50,10)
> DV <- c(A,B)
> IV <- rep(c("A","B"),each=100)
> sims <- rep(rep(1:10,each=10),2)
> df<-data.frame(sims,IV,DV)
> 
> means_df <- df %>%
+                group_by(sims,IV) %>%
+                summarize(means=mean(DV),
+                          sem = sd(DV)/sqrt(length(DV)))
> 
> stats_df <- df %>%
+               group_by(sims) %>%
+               summarize(ts = t.test(DV~IV,var.equal=TRUE)$statistic)
> 
> a <- ggplot(means_df, aes(x = IV,y = means, fill = IV)) +
+   geom_bar(stat = "identity") +
+   geom_point(aes(x = IV, y = DV), data = df, alpha = .25) +
+   geom_errorbar(aes(ymin = means - sem, ymax = means + sem), width = .2) +
+   theme_classic() +
+   transition_states(
+     states = sims,
+     transition_length = 2,
+     state_length = 1
+   ) + 
+   enter_fade() + 
+   exit_shrink() +
+   ease_aes('sine-in-out')
>   
> a_gif <- animate(a, width = 240, height = 240)
>                                                                             
> b <- ggplot(stats_df, aes(x = ts))+
+   geom_vline(aes(xintercept = ts, frame = sims))+
+   geom_line(aes(x=x,y=y),
+             data = data.frame(x = seq(-5,5, .1),
+                               y = dt(seq(-5,5, .1), df = 18))) +
+   theme_classic() +
+   ylab("density") +
+   xlab("t value") +
+   transition_states(
+     states = sims,
+     transition_length = 2,
+     state_length = 1
+   ) +
+   enter_fade() + 
+   exit_shrink() +
+   ease_aes('sine-in-out')
Warning message:
Ignoring unknown aesthetics: frame 
> 
> b_gif <- animate(b, width = 240, height = 240)
>                                                                             
> a_mgif <- image_read(a_gif)
Error in image_read(a_gif) : path must be URL, filename or raw vector
> b_mgif <- image_read(b_gif)
Error in image_read(b_gif) : path must be URL, filename or raw vector
> 
> new_gif <- image_append(c(a_mgif[1], b_mgif[1]))
Error in assert_image(image) : objeto 'a_mgif' no encontrado
> for(i in 2:100){
+   combined <- image_append(c(a_mgif[i], b_mgif[i]))
+   new_gif <- c(new_gif, combined)
+ }
Error in assert_image(image) : objeto 'a_mgif' no encontrado
> 
> new_gif

My Session info:

> sessionInfo()
R version 4.0.0 (2020-04-24)
Platform: x86_64-suse-linux-gnu (64-bit)
Running under: openSUSE Leap 15.1

Matrix products: default
BLAS:   /usr/lib64/R/lib/libRblas.so
LAPACK: /usr/lib64/R/lib/libRlapack.so

locale:
 [1] LC_CTYPE=es_ES.UTF-8       LC_NUMERIC=C              
 [3] LC_TIME=es_ES.UTF-8        LC_COLLATE=es_ES.UTF-8    
 [5] LC_MONETARY=es_ES.UTF-8    LC_MESSAGES=es_ES.UTF-8   
 [7] LC_PAPER=es_ES.UTF-8       LC_NAME=C                 
 [9] LC_ADDRESS=C               LC_TELEPHONE=C            
[11] LC_MEASUREMENT=es_ES.UTF-8 LC_IDENTIFICATION=C       

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

other attached packages:
[1] gganimate_1.0.5.9000 magick_2.3           ggplot2_3.3.0       
[4] dplyr_0.8.5         

loaded via a namespace (and not attached):
 [1] Rcpp_1.0.4        magrittr_1.5      hms_0.5.3         progress_1.2.2   
 [5] tidyselect_0.2.5  munsell_0.5.0     colorspace_1.4-1  R6_2.4.1         
 [9] rlang_0.4.5       fansi_0.4.1       plyr_1.8.4        tools_4.0.0      
[13] grid_4.0.0        gtable_0.3.0      cli_2.0.1         withr_2.1.2      
[17] ellipsis_0.3.0    digest_0.6.25     assertthat_0.2.1  tibble_3.0.0     
[21] lifecycle_0.2.0   crayon_1.3.4      purrr_0.3.3       tweenr_1.0.1     
[25] farver_2.0.3      vctrs_0.2.4       glue_1.3.1        labeling_0.3     
[29] stringi_1.4.6     compiler_4.0.0    pillar_1.4.3      scales_1.1.0     
[33] prettyunits_1.0.2 pkgconfig_2.0.2  

Can you help me get it right?

Thank you for the good work!! gganimate` is an amazing package!

jennaccarlson commented 4 years ago

I had the same problem and created a work around using anim_save()

`library(dplyr) library(ggplot2) library(magick) library(gganimate)

A<-rnorm(100,50,10) B<-rnorm(100,50,10) DV <- c(A,B) IV <- rep(c("A","B"),each=100) sims <- rep(rep(1:10,each=10),2) df<-data.frame(sims,IV,DV)

means_df <- df %>% group_by(sims,IV) %>% summarize(means=mean(DV), sem = sd(DV)/sqrt(length(DV)))

stats_df <- df %>% group_by(sims) %>% summarize(ts = t.test(DV~IV,var.equal=TRUE)$statistic)

a <- ggplot(means_df, aes(x = IV,y = means, fill = IV)) + geom_bar(stat = "identity") + geom_point(aes(x = IV, y = DV), data = df, alpha = .25) + geom_errorbar(aes(ymin = means - sem, ymax = means + sem), width = .2) + theme_classic() + transition_states( states = sims, transition_length = 2, state_length = 1 ) + enter_fade() + exit_shrink() + ease_aes('sine-in-out')

a_gif <- animate(a, width = 240, height = 240) anim_save("a.gif")

b <- ggplot(stats_df, aes(x = ts))+ geom_vline(aes(xintercept = ts, frame = sims))+ geom_line(aes(x=x,y=y), data = data.frame(x = seq(-5,5, .1), y = dt(seq(-5,5, .1), df = 18))) + theme_classic() + ylab("density") + xlab("t value") + transition_states( states = sims, transition_length = 2, state_length = 1 ) + enter_fade() + exit_shrink() + ease_aes('sine-in-out')

b_gif <- animate(b, width = 240, height = 240) anim_save("b.gif")

a_mgif <- image_read("a.gif") b_mgif <- image_read("b.gif")

new_gif<-image_append(c(a_mgif[1], b_mgif[1])) for(i in 2:100){ combined <- image_append(c(a_mgif[i], b_mgif[i])) new_gif<-c(new_gif,combined) }

new_gif `