DEV Community

Liam A.
Liam A.

Posted on

Attempt to make an animated map in R

I have a set of data sets (from the U.S. Census Bureau’s American Community Survey) that list how many people drive to work with a commute length that is one of the following time intervals: 14 minutes or less 15-29 minutes 30-44 minutes 45-59 minutes 60 minutes or more.

The data is polygons and I am coding in R Studio. What I would like to do is show an animation of 5 plots, one plot for each of these five time intervals listed above. Specifically I am looking at data by census tract in five Maryland counties which are suburbs of Washington, D.C.: Calvert, Charles, Frederick, Montgomery, and Prince George's counties.

After tidying the data, right now I have the code posted below as an attempt to make an animated series of plots, using ggplot and gganimate. I have largely been inspired by this example, but I also looked at this article/post and I have incorporated these pieces of advice into the code below.
To be clear, my plots I’ve made so far are intended to have plot_DrAl1 be the one showing the number of people driving to work alone whose commute is 14 minutes or less, while plot_DrAl2 is for people with commutes of 15-29 minutes, plot_DrAl3 is for people with commutes of 30-44 minutes and plot_DrAl4 is for people with commutes of 45-59 minutes. Eventually, I aim to add a fifth plot displaying the number of people driving to work alone with commutes of 60 minutes or more

If someone has an idea of what I am doing wrong or how I can better achieve my goal, please let me know! And as a disclaimer, yes I have already posted this question on Stackoverflow but after reading this post I am wondering whether I will ever get an answer on SO

My Code at this point:

set-up

library(gganimate)
library(tidyverse)
library(sf)
library(readx1)
library(tidycensus)
library(tmap)
library(leaflet)
library(gapminder)
library(maps)
library(httr)
library(hrbrthemes)
library(GISTools)
library(sp)
library(raster)
setwd("Z:/ENVS_117/final_project/tidycensus_data")
census_api_key("ca1e6bccd0bcdbf2cd3462c3f055921aae8e3957")

PLOTS

theme_set(theme_bw())

plot_DrAl1 <- ggplot() +
geom_polygon(data=dc_area, aes(Longitude, Lat), fill = "#e6d4ff") +
geom_polygon(data=cars_0014, aes(x = Longitude, y = Lat, fill = Perc_DrAl14)) +

# gganimate parts
transition_states(Perc_DrAl14, transition_length = 2, state_length = 20) +
enter_fade() +
exit_fade() +

# Styling
coord_equal(xlim = c(-75000, 825000), ylim = c(0, 1200000)) +
theme(panel.grid.major = 4,
panel.grid.minor = 0,
axis.text = element_blank(),
axis.title = element_blank(),
axis.ticks = element_blank(),
panel.background = element_rect(fill = "grey99", colour = "grey80"),
plot.title = element_text(hjust = 0, size = 16, vjust=0))

print(plot_DrAl1 + plot_DrAl2 + plot_DrAl3 - plot_DrAl4 + plot_layout(ncol = 1, heights = c(5, 1)))
}

}, movie.name = "windDevelopment.gif", interval = 1, ani.width = 1000, ani.height = 700))

animate(plot_DrAl, length = 15, width = 700, height = 400)

Top comments (1)

Collapse
 
daveparr profile image
Dave Parr

Did you ever get an answer on your SO post? also, what exactly is the error or problem you are facing? I'm having trouble working out what the issue is that you have hit.