0

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.

jamesguy0121
  • 1,124
  • 11
  • 28
  • I would have a look to the data you are not able to share, specially to the ids you use for joining. I know it sounds quite obvious and you might already checked that, but it seems to be an issue when joining, good luck! – dieghernan Apr 04 '20 at 13:34
  • Yea, this is what I was thinking as well. I isolated two of the block groups that were moving around and didn't see where there were any obvious errors...I'll keep digging though. Thanks. – jamesguy0121 Apr 06 '20 at 17:01

1 Answers1

0

I recently had a similar issue. I resolved it by adding the group argument in my aesthetic and specifying the census tract. For example:

ggplot(data = travisJoined) + 
geom_sf(aes(geometry = geometry, fill = count, group = GEOID10)) +
theme_bw() + 
scale_fill_distiller(palette = "Spectral") + 
transition_states(date, 
                transition_length = 3, 
                state_length = 10)