I'm trying to animate a shapefile using gganimate, but when I run, nothing happens and R crashes. I've attached the code below, not sure what to do? Thanks.
ca_counties <- read_sf("California_County_Boundaries_5206538824092848308.geojson")
plot(st_geometry(ca_counties))
wnv_cases <- read_csv("wnv_ca.csv")
joined <- st_as_sf(full_join(wnv_cases, ca_counties, by = c("County" = "COUNTY_NAME")))
#joined
test_map <- ggplot() + geom_sf(data=joined %>% slice(1:150),
aes(fill=Cases)) +
theme_void()
test_animation <- test_map + transition_time(as.numeric(Year))
animate(test_animation)
Ran the animate command, expecting the plot to become animated in the plot pane, but R couldn't complete it and I had to restart R to get it to stop running, and no plot showed up at all.
gganimate
can take a long time, especially if rendering data that are relatively large and/or have a lot of transitions. For instance, the animation in this answer took ~53 minutes to render, albeit on a relatively low-spec PC. If the stop symbol is showing (and a crash does not occur), your animate()
is working, and it will likely render if you wait.
Having said that, the counties data you are using is relatively high resolution, arguably it is unnecessarily so for the scale you are plotting at. Here is an alternative using the tigris
package to load a lower resolution sf of the counties geometries for California.
As you did not provide your data, I have sourced some WNV data from the CDC's Historic Data (1999-2023) page. To keep it simple, I have just used the years 2021-2023 and prepped it beforehand. The "Cases" column refers to the "Reported Human Cases" data in the original CSV files.
library(tigris)
library(dplyr)
library(tidyr)
library(ggplot2)
library(gganimate)
# Load California counties
ca_counties <- counties(state = "CA", year = 2022)
# Create vector of years in data (modify based on your actual data)
years <- 2021:2023
# Duplicate each feature n times for each year in data and add Year column
ca_counties <- uncount(ca_counties, length(years)) |>
mutate(Year = years, .by = NAME)
# Example WNV data for 2021-2023
# previously prepped, downloaded from
# https://www.cdc.gov/west-nile-virus/data-maps/historic-data.html
wnv_cases <- structure(list(County = c("Alameda", "Amador", "Butte", "Colusa",
"Contra Costa", "El Dorado", "Fresno", "Glenn", "Kern", "Kings",
"Lake", "Los Angeles", "Madera", "Merced", "Napa", "Nevada",
"Orange", "Placer", "Riverside", "Sacramento", "San Bernardino",
"San Diego", "San Francisco", "San Joaquin", "San Luis Obispo",
"San Mateo", "Santa Barbara", "Santa Clara", "Santa Cruz", "Shasta",
"Solano", "Sonoma", "Stanislaus", "Sutter", "Tehama", "Tulare",
"Ventura", "Yolo", "Yuba", "Alameda", "Butte", "Colusa", "Contra Costa",
"Fresno", "Glenn", "Imperial", "Kern", "Kings", "Lake", "Los Angeles",
"Madera", "Merced", "Napa", "Nevada", "Orange", "Placer", "Riverside",
"Sacramento", "San Bernardino", "San Diego", "San Joaquin", "San Luis Obispo",
"San Mateo", "Santa Clara", "Shasta", "Solano", "Sonoma", "Stanislaus",
"Sutter", "Tehama", "Tulare", "Ventura", "Yolo", "Yuba", "Alameda",
"Amador", "Butte", "Calaveras", "Colusa", "Contra Costa", "El Dorado",
"Fresno", "Glenn", "Imperial", "Inyo", "Kern", "Kings", "Lake",
"Los Angeles", "Madera", "Marin", "Merced", "Monterey", "Napa",
"Nevada", "Orange", "Placer", "Riverside", "Sacramento", "San Benito",
"San Bernardino", "San Diego", "San Joaquin", "San Luis Obispo",
"San Mateo", "Santa Clara", "Shasta", "Siskiyou", "Solano", "Sonoma",
"Stanislaus", "Sutter", "Tehama", "Tulare", "Ventura", "Yolo",
"Yuba"), Year = c(2021L, 2021L, 2021L, 2021L, 2021L, 2021L, 2021L,
2021L, 2021L, 2021L, 2021L, 2021L, 2021L, 2021L, 2021L, 2021L,
2021L, 2021L, 2021L, 2021L, 2021L, 2021L, 2021L, 2021L, 2021L,
2021L, 2021L, 2021L, 2021L, 2021L, 2021L, 2021L, 2021L, 2021L,
2021L, 2021L, 2021L, 2021L, 2021L, 2022L, 2022L, 2022L, 2022L,
2022L, 2022L, 2022L, 2022L, 2022L, 2022L, 2022L, 2022L, 2022L,
2022L, 2022L, 2022L, 2022L, 2022L, 2022L, 2022L, 2022L, 2022L,
2022L, 2022L, 2022L, 2022L, 2022L, 2022L, 2022L, 2022L, 2022L,
2022L, 2022L, 2022L, 2022L, 2023L, 2023L, 2023L, 2023L, 2023L,
2023L, 2023L, 2023L, 2023L, 2023L, 2023L, 2023L, 2023L, 2023L,
2023L, 2023L, 2023L, 2023L, 2023L, 2023L, 2023L, 2023L, 2023L,
2023L, 2023L, 2023L, 2023L, 2023L, 2023L, 2023L, 2023L, 2023L,
2023L, 2023L, 2023L, 2023L, 2023L, 2023L, 2023L, 2023L, 2023L,
2023L, 2023L), Cases = c(0, 0, 13, 0, 2, 1, 14, 2, 8, 8, 0, 16,
3, 6, 0, 0, 3, 2, 3, 6, 1, 3, 1, 8, 2, 1, 2, 3, 1, 3, 2, 0, 5,
0, 0, 8, 0, 3, 0, 1, 3, 1, 1, 30, 1, 0, 22, 7, 0, 61, 3, 7, 0,
0, 9, 2, 0, 5, 4, 3, 4, 0, 1, 1, 1, 2, 0, 15, 1, 3, 15, 0, 3,
1, 1, 0, 15, 1, 1, 10, 3, 23, 4, 1, 0, 16, 8, 6, 53, 8, 0, 8,
2, 0, 1, 5, 6, 18, 50, 0, 28, 0, 15, 2, 3, 3, 6, 0, 5, 1, 31,
7, 0, 25, 1, 37, 5)), row.names = c(NA, -117L), class = c("tbl_df",
"tbl", "data.frame"))
# Join wnv_cases to duplicated version of ca_counties
joined <- left_join(ca_counties, wnv_cases, by = c("NAME" = "County", "Year"))
# Create animated plot
test_map <- ggplot() +
geom_sf(data = joined,
aes(fill = Cases)) +
theme_void()
test_animation <- test_map +
transition_states(Year,
transition_length = 1,
state_length = 1) +
labs(title = "Year: {closest_state}") +
enter_fade() +
exit_fade() +
ease_aes("linear")
ani <- animate(test_animation)
ani