This may be difficult, because I cannot share my data, and when I tried to reproduce the error in another example it did not work. I am attempting to make a gganimate figure of census block groups to see how a variable changes over time. I'm having an issue with the animation where several census block groups will float and reshape into a different block between states. Here is the animation that is not working properly. This is the code used to generate the plot.
library(tigris)
library(ggplot2)
library(sf)
library(gganimate)
library(transformr)
options(tigris_class = "sf")
travis <- block_groups(48, c(453))
# join travis with count data
travis2 <- rbind_tigris(geo_join(travis, travisSD[[1]], "GEOID", "origin_census_block_group"),
geo_join(travis, travisSD[[58]], "GEOID", "origin_census_block_group"))
p <- ggplot(data = travis2) +
geom_sf(aes(geometry = geometry)) +
geom_sf(aes(fill = proportionLeftHome)) +
theme_bw() +
scale_fill_distiller(palette = "Spectral") +
transition_states(date,
transition_length = 3,
state_length = 10) +
ease_aes('linear')
animate(p)
When I tried to replicate the problem with made up data I was not able to do it. Here is what I tried, but it works fine.`
library(tigris)
library(ggplot2)
library(sf)
library(gganimate)
library(transformr)
set.seed(42)
options(tigris_class = "sf")
travis <- block_groups(48, c(453))
travis1 <- travis
travis2 <- travis
travis1$date <- as.Date("2020/02/01")
travis1$count <- runif(580, 0, 1000)
travis2$date <- as.Date("2020/03/01")
travis2$count <- runif(580, 0, 1000)
travisJoined <- rbind_tigris(travis1, travis2)
p <- ggplot(data = travisJoined) +
geom_sf(aes(geometry = geometry)) +
geom_sf(aes(fill = count)) +
theme_bw() +
scale_fill_distiller(palette = "Spectral") +
transition_states(date,
transition_length = 3,
state_length = 10)
animate(p)
Does anybody know what the possible issue might be here? I for the life of me can't figure it out. I've been able to identify a couple of the block groups that are causing issues, but when I look at the data for them I can't see any obvious reason as to why this would be happening.
I recently had a similar issue. I resolved it by adding the group argument in my aesthetic and specifying the census tract. For example: