3

I try to run this code below. The issue I ran into is that if I do not set a width and height it saves, if I set width and height it is stuck in running without ever saving (I tried letting it run for a day). I also tried the code on multiple machines. Many thanks for any help

## Data generation (This works, but is included to reproduce the exact data)
set.seed(2711)
library('MASS')
library(tidyverse)
library(magrittr)
library(gganimate)

group_loop_df <- 
lapply(seq(100, 1000, 100), function(sample_size){
  test1 <- data.frame(V1 = truncnorm::rtruncnorm(sample_size, 1, 10, 5, 2))
  summary_test <- Rmisc::summarySE(test1, "V1")

  test1$ci_V1 <- summary_test$ci
  test1$sd_V1 <- summary_test$sd
  test1$mean_V1 <- summary_test$V1
  hist_data <- ggplot_build(ggplot(test1) +
                            aes(x = V1) +
                            geom_histogram() +
                            theme_classic())$data[[1]]
  test1$ceiling_var <- max(hist_data$count)
  test1$sample_size <- sample_size
  test1$group <- "Group 1"
  
  test2 <- data.frame(V1 = truncnorm::rtruncnorm(sample_size, 1, 10, 4, 2))
  summary_test <- Rmisc::summarySE(test2, "V1")

  test2$ci_V1 <- summary_test$ci
  test2$sd_V1 <- summary_test$sd
  test2$mean_V1 <- summary_test$V1
  hist_data <- ggplot_build(ggplot(test2) +
                            aes(x = V1) +
                            geom_histogram() +
                            theme_classic())$data[[1]]
  test2$ceiling_var <- max(hist_data$count)
  test2$sample_size <- sample_size  
  test2$group <- "Group 2"
  
  overall <- rbind(test1, test2)
  overall$p <- t.test(V1 ~ group, overall)$p.value
  overall
  }) %>%
do.call(rbind, .)




full_hist_overlap <- ggplot(group_loop_df) +
                     geom_histogram(aes((y=..count../sum(..count..)) * 100, x = V1, fill = group) ) + 
                     geom_vline(aes(xintercept = mean_V1, color = group), size = 2) +
                     geom_vline(aes(xintercept = mean_V1 - ci_V1, color = group), linetype = "dashed", size = 2) +
                     geom_vline(aes(xintercept = mean_V1 + ci_V1, color = group), linetype = "dashed", size = 2) +
                     theme_classic() +
                     labs(y = "Percent of Values", x = NULL) +
                     transition_states(sample_size,
                            transition_length = 5,
                            state_length = 10) +
                            ggtitle('Now showing {closest_state} samples') +
                            ease_aes('cubic-in-out')  


### This is where the error occurs.
### This works
anim_save("hist_overlap.gif")

### This does not

anim_save("hist_overlap.gif", units = "cm",
         width = 25.4, height = 14.288, res = 300)
J.Karl
  • 45
  • 4
  • Please consider winnowing down the number of lines of code to make your issue less problematic. This will help everyone. – mccurcio Mar 14 '21 at 22:15
  • I edited the post to clarify that the top part just creates the exact reproducible example. The problem arises in the last two lines of the code and is now clearly marked. – J.Karl Mar 14 '21 at 22:23

1 Answers1

1

This worked for me. I think the resolution parameters belong to the animate function that anim_save invokes, but anim_save doesn't seem to pass those parameters along.

enter image description here

animate(full_hist_overlap, units = "cm", 
        width = 25.4, height = 14.288, res = 300)
anim_save("hist_overlap.gif")
Jon Spring
  • 55,165
  • 4
  • 35
  • 53
  • Many thanks for that it now works. Initially the solution did not run, but for some reason after updating gifski it now works with your solution. – J.Karl Mar 15 '21 at 01:39