avs
avs

Reputation: 678

Wiggling lines when animating ggplot choropleth with gganimate

I have a problem when using gganimate to animate a choropleth map made with ggplot2.

A selection of my data is available here and the code is:

library(tidyverse)
library(gganimate)

part_data <- readRDS(file = "part_data.Rds")

p <- part_data %>%
  ggplot(aes(x = long, y = lat, group = group)) +
  geom_polygon(aes(fill = confirmed), color = "grey70", size = 0.05) +
  coord_map() +
  scale_fill_distiller(trans = "log10", direction = 1, palette = "YlOrRd", na.value = "white") + 
  transition_time(time = date)

animate(p,
        fps = 3, 
        duration = 5,
        renderer = gifski_renderer("countyevolution.gif"),
        width = 1200, height = 750, res = 100)

which yields this GIF: GIF

Perhaps it's difficult to see but if you look closely you'll see that the borders between the counties are wiggling. This does not happen when I use transition_manual without transitions between the dates, so it must come from these transitions. But why?

Is it possible to somehow tell gganimate to keep the borders the same and only render the fill for each frame? Or can I somehow else make the border less obvious? I tried decreasing the size, but that does not seem to make a difference. Also, the borders seem a bit jagged.

Upvotes: 3

Views: 240

Answers (1)

This also happened to em recently: animated plots that contain maps or other static elements become wiggly or blink. I realized the wiggle happens because gganimate is also animating the map lines in each step of the animation. This explains why using other transition modes or removing transition animations solve the blinking.

As stated in this question, to exclude a ggplot layer from being animated you need to add a data argument. This way, the map or plot in the background will remain static, and the rest of the plot will animate.

Upvotes: 1

Related Questions