I am trying to animate a map of Texas over time, such that the fill color changes depending on if an event
happened.
However, I keep getting the following error message: Error: arguments have different crs
.
I suspect the error is related to the fill in data_map
but I'm not entirely sure. Any ideas on what this error message means and/or how to animate this time series of Texas?
I have been using section 3.3 of this website (link here) as a guide.
Example Data:
library(ggplot2)
library(dplyr)
library(geodata)
library(sf)
library(gganimate)
library(gifski)
# Polygon of the state of Texas in USA
tx <- gadm(country = 'USA', level = 1, path=tempdir()) %>%
st_as_sf() %>%
filter(NAME_1 %in% 'Texas') %>%
mutate(State = NAME_1) %>%
select(State, geometry) %>%
st_transform(crs = 4269)
# Data to use for map
df1 <- data.frame(matrix(ncol = 4, nrow = 3))
colnames(df1)[1:4] <- c('State','date_start','date_end','event')
df1$State <- 'Texas'
df1$date_start <- c(as.Date('2020-01-02'),
as.Date('2020-01-10'),
as.Date('2020-01-16'))
df1$date_end <- c(as.Date('2020-01-05'),
as.Date('2020-01-13'),
as.Date('2020-01-24'))
df1$event <- c('moderate','strong','severe')
df1 <- left_join(df1,tx, by = 'State')
df2 <- data.frame(matrix(ncol = 1, nrow = 31))
colnames(df2)[1] <- 'date'
df2$date <- seq.Date(as.Date('2020-01-01'),
as.Date('2020-01-31'),
1)
dat <- left_join(df2, df1, by = join_by(between(date, date_start, date_end))) %>%
select(date,event,geometry) %>%
mutate(geometry = tx$geometry)
# Create basemap of Texas
base_map <- ggplot(data = dat) +
geom_sf(mapping = aes(geometry = geometry),
color = "black", fill = "white") +
theme_void()
base_map
# Add data to basemap
data_map <- base_map +
geom_sf(data = dat,
mapping = aes(geometry = geometry,
fill = event)) +
transition_time(date)
# Animate map
animate(data_map, nframes = 31) # throws error message