I am trying to output an animation using R and gganimate
. I want an animation of 3 columns and 10 rows, each cell of which is lighted up one by one to show all row combinations. I think the code and animation below explain it.
library(data.table)
library(ggplot2)
library(gganimate)
library(magrittr)
scn_to_plot <- 1000
lst_block <- lapply(seq(10), function(i) {
return(c(list(rep(0:2, each = 3**(10-i), times = 3**(i-1))), list(rep(1:3, each = 3**(10-i), times = 3**(i-1)))))
}) %>% do.call(c, .) %>% set_names(paste0(rep(paste0("p", 1:10), each = 2), rep(c("x1", "x2"), times = 10))) %>% as.data.table
lst_block[lst_block == 1] <- 1/3
lst_block[lst_block == 2] <- 2/3
lst_block[lst_block == 3] <- 1
lst_block[, paste0(rep(paste0("p", 1:10), each = 2), rep(c("y1", "y2"), times = 10)) := list(
0.9, 1, 0.8, 0.9, 0.7, 0.8, 0.6, 0.7, 0.5, 0.6, 0.4, 0.5, 0.3, 0.4, 0.2, 0.3, 0.1, 0.2, 0, 0.1
)]
lst_block[, scnN := seq(.N)]
p_bs <- ggplot(lst_block[1:scn_to_plot], aes(x = p1x1, y = p1y1)) +
coord_cartesian(xlim = c(0, 1), ylim = c(0, 1)) +
theme_classic() +
theme(axis.line = element_blank(),
axis.text = element_blank(),
axis.title = element_blank(),
axis.ticks = element_blank(),
plot.margin = margin(0, 0, 0, 0, "cm"),
panel.grid.major = element_blank(),
panel.grid.minor = element_blank())
p_bs %<>%
add(geom_rect(aes(xmin = p1x1, xmax = p1x2, ymin = p1y1, ymax = p1y2), size = 0, fill = "#ff0000", alpha = 0.25)) %>%
add(geom_rect(aes(xmin = p2x1, xmax = p2x2, ymin = p2y1, ymax = p2y2), size = 0, fill = "#ff0000", alpha = 0.25)) %>%
add(geom_rect(aes(xmin = p3x1, xmax = p3x2, ymin = p3y1, ymax = p3y2), size = 0, fill = "#ff0000", alpha = 0.25)) %>%
add(geom_rect(aes(xmin = p4x1, xmax = p4x2, ymin = p4y1, ymax = p4y2), size = 0, fill = "#ff0000", alpha = 0.25)) %>%
add(geom_rect(aes(xmin = p5x1, xmax = p5x2, ymin = p5y1, ymax = p5y2), size = 0, fill = "#ff0000", alpha = 0.25)) %>%
add(geom_rect(aes(xmin = p6x1, xmax = p6x2, ymin = p6y1, ymax = p6y2), size = 0, fill = "#ff0000", alpha = 0.25)) %>%
add(geom_rect(aes(xmin = p7x1, xmax = p7x2, ymin = p7y1, ymax = p7y2), size = 0, fill = "#ff0000", alpha = 0.25)) %>%
add(geom_rect(aes(xmin = p8x1, xmax = p8x2, ymin = p8y1, ymax = p8y2), size = 0, fill = "#ff0000", alpha = 0.25)) %>%
add(geom_rect(aes(xmin = p9x1, xmax = p9x2, ymin = p9y1, ymax = p9y2), size = 0, fill = "#ff0000", alpha = 0.25)) %>%
add(geom_rect(aes(xmin = p10x1, xmax = p10x2, ymin = p10y1, ymax = p10y2), size = 0, fill = "#ff0000", alpha = 0.25))
p_bs %<>% add(transition_manual(scnN))
anim_save("plot_combn.gif", animation = p_bs, nframes = 600, fps = 60, bg = "transparent")
I am encountering two problems:
fps
(the last line) (I also tried using options(gganimate.fps = xx)
, it seems that the fps of the final output gif file is fixed at somewhere like 10.png
device. (I need to embed the resultant gif file in powerpoint presentation, but the outcome always has a white background.)Any help is appreciated.
It seems like the maximum allowed fps value is 50. If you go above that (like in your case 60) it will go back to the default 10 without any error messages. So try set the fps to 50!