I want to make a animated choropleth map of US counties with the number of confirmed COVID-19 cases over time (yes, yet another coronavirus plot). Here is a link to a three day selection of the data (suggestions for a more permanent place to host it are welcome). Here is the code that creates the static map (comment the filter to include all dates):
library(tidyverse)
library(gganimate)
library(ggmap)
library(maps)
library(scales)
p <- part_data %>%
filter(date == as.Date("2020-03-30")) %>%
ggplot(aes(x = long, y = lat, group = group)) +
geom_polygon(aes(fill = confirmed_new), color = "grey70", size = 0.05) +
geom_path(data = state_map, colour = "black") +
coord_map() +
scale_fill_distiller(trans = "log10", direction = 1, palette = "YlOrRd", na.value = "white", limits = c(1, 1E4), labels = comma)
Which gives this pretty nice plot:
But how do I now make an animation that moves through the dates? I tried
p +
transition_time(date)
and
p +
transition_states(date)
but in both cases R just seems to freeze, before the rendering progress bar even appears, and after a while I get the error Error: cannot allocate vector of size 128.0 Mb
. The problem is that I don't know if I'm even using the correct approach here. In the gganimate
documentation I read that the group
aesthetic is used to link rows that belong to the same graphic element, in this case county. But group
is also used by ggplot
itself to make the plot, could that be the issue? Any help is appreciated. Thanks.