Search code examples
rggplot2gganimate

Define size for .gif created by gganimate - change dimension / resolution


I'm using gganimate to create some .gif files that I want to insert into my reports. I'm able to save the files and view them fine, however, I find that the displayed size is small: 480x480. Is there a way to adjust that - perhaps along the lines of height and width arguments in ggsave()?

I can zoom in but that impacts the quality poorly and makes it rather unreadable for my use case.

Here's some sample code:

gplot <- 
  ggplot(gapminder, 
         aes(x = gdpPercap, y = lifeExp, colour = continent, 
             size = pop, 
             frame = year)) +
    geom_point(alpha = 0.6) + 
    scale_x_log10()

gganimate(gplot, "test.gif")

Below is the output for this code.

test.gif


Solution

  • There can be issues with using the magick package.

    I think a better solution is use the animate() function in gganimate to create an object which is then passed to the anim_save() function. No need to use another package.

    library(gganimate)
    library(gapminder)
    
    my.animation <- 
      ggplot(
      gapminder,
      aes(x = gdpPercap, y = lifeExp, colour = continent, size = pop)
     ) +
    geom_point(alpha = 0.6) +
    scale_x_log10() +
    transition_time(year)
    
    # animate in a two step process:
    animate(my.animation, height = 800, width =800)
    anim_save("Gapminder_example.gif")