Combining Multiple Plots into Animation within R Markdown

Hi! I am working on making some R animations. I am most familiar with gganimate, but I'm running into some issues with wanting more flexibility than what it seems to be able to give.

I am working to create an illustration of Simpson's paradox and would like to end up with something like Rafael Irizarry has done here. In his code, he creates three different static ggplots and then uses an online animated gif maker to turn them into a gif that can be imbedded into a webpage etc. I would love to do something similar, but I was wondering if there is a way to do this directly in R, that is, create multiple plots, combine them in R studio into a gif, and then knit markdown file into html with the working gif. I have been looking into the "animation" package, but I am having some trouble with it. Is there an easy way to do this sort of thing directly in R? If the animation package is the most straightforward way to potentially proceed, does anyone have any recommendations of examples or tutorials that might help someone like me who is less familiar with animation learn how to do this? Thank you so much for there help. If it is useful, below is a reprex with 3 preliminary plots I would like to combine based on Irizarry's example.

library(tidyverse)

new <- structure(list(X1 = c(12.0696672296859, 11.6841163061608, 12.0715797021037, 
12.0723680840704, 11.7787414773008, 11.665930648957, 10.8549012860911, 
12.6579823141601, 11.0882045159352, 11.2705525911076, 7.37776245454223, 
10.24542423234, 8.7219467179225, 8.1184600142838, 11.0960074484599, 
9.28028843704825, 8.47794814802428, 10.0081401874127, 8.48945149994568, 
8.60526896859887, 6.72716897230105, 6.89299660302577, 7.04186147082062, 
7.02243464291171, 8.42328083644302, 7.79126006940277, 5.17911917780443, 
6.60949790076307, 6.34183359739644, 6.39438933110829), X2 = c(4.85791238051334, 
3.24730046076944, 3.48393017326714, 3.08696718543513, 2.08364339959147, 
2.7206106423608, 2.31620150882639, 5.59056832144476, 2.85747320477784, 
3.8848257334616, 2.83395926680445, 5.87882208149775, 4.74233626243597, 
3.1420326860469, 7.11077329657872, 5.4698951803137, 5.09220178695681, 
5.46589384773571, 3.79655909881602, 4.91029443296786, 6.98739391121053, 
7.22650380283232, 7.88567358305414, 6.79967543209962, 7.91517572155003, 
6.98549403170164, 4.45571244250537, 6.99040809449601, 7.05524354625015, 
6.61896387252642), group = c(1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 2, 
2, 2, 2, 2, 2, 2, 2, 2, 2, 3, 3, 3, 3, 3, 3, 3, 3, 3, 3)), row.names = c(NA, 
-30L), class = "data.frame")

# Plot 1
p1<- new %>%
  ggplot(aes(X1, X2)) +
  geom_point() +
  ggtitle("Data") 


# Plot 2

# calculate correlations

correlations <- new %>%
  group_by(group) %>%
  summarise(cor(X1,X2)) %>%
  pull() %>%
  round(2)

p2 <- new %>%
  ggplot(aes(X1, X2)) +
  geom_point(aes(color = as.factor(group))) +
  geom_smooth(aes(color = as.factor(group)), method = "lm", se = F) +
  ggtitle(paste("Correlations = ",toString(correlations))) 

# Plot 3

p3 <- new %>%
  ggplot(aes(X1, X2)) +
  geom_point(color = "purple") +
  geom_smooth(color = "purple", method = "lm", se = F) +
  ggtitle(paste("Correlation = ", round(cor(new$X1,new$X2), 2))) 

assuming you have 3 plots made in environment names p1,p2,p3

library(animation)

# explicit
animation::saveGIF(
  expr = {
    plot(p1)
    plot(p2)
    plot(p3)
  },
  movie.name = "explicit_my3.gif"
)

# implicit
obs_in_env <- ls()
ggplots_in_env_lgl <- map_lgl(
  obs_in_env,
  ~ is.ggplot(get(.))
)
ggplots_in_env <- obs_in_env[ggplots_in_env_lgl] %>% sort()

animation::saveGIF(
  expr = {
    purrr::walk(
      ggplots_in_env,
      ~ plot(get(.))
    )
  },
  movie.name = "implicit_my3.gif"
)

@nirgrahamuk thank you so much for the reply. This makes sense and will now help me to build off of this to create further plots. Thanks!

This topic was automatically closed 7 days after the last reply. New replies are no longer allowed.