title: How to loop through unique combinations of 2 categorical variables to create several animated plots?
body: <p>I'm looking to loop through unique combinations of two categorical variables to create animated bubble plots in R. I'm gonna use example data to I don't overcomplicate anything. Note: Ignore that there is only one unique ID.</p>
  <p>Lets say I have a data frame with the following information:</p>
  <pre><code>#Create data frame
  DF &lt;- data.frame(&quot;frameId&quot; = c(1,1,1,2,2,2,3,3,3,1,1,1,2,2,2,3,3,3), 
  &quot;CV1&quot; = c(&quot;A&quot;,&quot;A&quot;,&quot;A&quot;,&quot;A&quot;,&quot;A&quot;,&quot;A&quot;,&quot;A&quot;,&quot;A&quot;,&quot;A&quot;,&quot;B&quot;,&quot;B&quot;,&quot;B&quot;,&quot;B&quot;,&quot;B&quot;,&quot;B&quot;,&quot;B&quot;,&quot;B&quot;,&quot;B&quot;),
   &quot;CV2&quot; = c(&quot;I&quot;,&quot;J&quot;,&quot;K&quot;,&quot;I&quot;,&quot;J&quot;,&quot;K&quot;,&quot;I&quot;,&quot;J&quot;,&quot;K&quot;,&quot;I&quot;,&quot;J&quot;,&quot;K&quot;,&quot;I&quot;,&quot;J&quot;,&quot;K&quot;,&quot;I&quot;,&quot;J&quot;,&quot;K&quot;),
   &quot;X&quot;=10:27,&quot;Y&quot;=30:47,&quot;ID&quot;=c(1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1))
  </code></pre>
  <p>I've created an animated bubble plot for one combination of &quot;CV1&quot; and &quot;CV2&quot;, which was where &quot;CV1&quot; = &quot;A&quot; and &quot;CV2&quot;=&quot;I&quot;.</p>
  <pre><code>#Create subset of one unique combination of the categorical variables
  S1&lt;- subset(DF, CV1==&quot;A&quot; &amp;CV2 ==&quot;I&quot;,select=frameId:ID)
  
  #Create animated bubble plot with gganimate
  P1&lt;-ggplot(S1, aes(X, Y, color = ID)) +
    geom_point() +
    scale_x_log10() +
    theme_bw() +
    # gganimate specific bits:
    labs(title = 'frameId: {frame_time}', x = 'X', y = 'Y') +
    transition_time(frameId) +
    ease_aes('linear')
  #animate the plot
  animate(P1)
  #Save the plot to my files
  anim_save(&quot;Plot_1.gif&quot;)
  </code></pre>
  <p>I want to create this animated bubble plot for each unique combination of &quot;CV1&quot; and &quot;CV2&quot;, and then save it with a unique name. How can I write a loop function to do this?</p>
  <p>I appreciate any help, thank you!</p>