Search code examples
rggplot2gganimate

R stuck on rendering in anim_save


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)

Solution

  • 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")