Search code examples
rggplot2gganimate

Why is animation plot smaller than static plot?


The code below morphs a star into a circle. If I just plot the the star the size is great.

ggplot(star1) +
  geom_sf(aes(geometry = geometry))

static star

However, the animation ends up being smaller:

animated star What do I do to have the animation the same size as the static plot of the stars?

library(tidyverse)
library(gganimate)
library(transformr)

star1 <- sf::st_sfc(poly_star(n = 7, st = TRUE))
circle1 <- sf::st_sfc(poly_circle(st = TRUE))

star2 <- star1 + c(2, 0)
circle2 <- circle1 + c(2,0)

star1 <- data.frame(
  id = c(1, 2),
  geo = c(star1, star2)
)

circle2 <- data.frame(
  id = c(1, 2),
  geo = c(circle1, circle2)
)

morph <- tween_sf(star1, circle2,
                  ease = 'linear', 
                  nframes = 12, 
                  id = id)  

ani <- ggplot(morph) +
       geom_sf(aes(geometry = geometry)) +
       transition_time(time = .frame) +
       theme(legend.position = "none") 

show <- animate(ani, nframes = 30, fps = 30, 
                renderer = gifski_renderer(loop = FALSE), start_pause = 15)

show

Solution

  • width and height parameters for the size of the output animation in pixels are options for animate

    animate(ani, nframes = 30, fps = 30, width = 900, 
              renderer = gifski_renderer(loop = FALSE), start_pause = 15)