首页 > 解决方案 > R卡在anim_save中的渲染

问题描述

我尝试在下面运行此代码。我遇到的问题是,如果我不设置宽度和高度,它会保存,如果我设置宽度和高度,它就会卡在运行中而没有保存(我试着让它运行一天)。我还在多台机器上尝试了代码。非常感谢您的帮助

## 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)

标签: rggplot2gganimate

解决方案


这对我有用。我认为解析参数属于调用的animate函数,但似乎并没有传递这些参数。anim_saveanim_save

在此处输入图像描述

animate(full_hist_overlap, units = "cm", 
        width = 25.4, height = 14.288, res = 300)
anim_save("hist_overlap.gif")

推荐阅读