在R中制作动画地图

时间:2020-03-10 04:05:53

标签: r ggplot2 maps gganimate tidycensus

我有一组数据集(来自美国人口普查局的美国社区调查),列出了有多少人开车通勤,通勤时长为以下时间间隔之一: 14分钟以内 15-29分钟 30-44分钟 45-59分钟 60分钟以上

数据是多边形,我正在R Studio中进行编码。我想做的是显示5个情节的动画,上面列出的这5个时间间隔中的每个情节都有一个情节。具体来说,我正在按人口普查数据查看华盛顿特区郊区的五个马里兰州县:卡尔弗特,查尔斯,弗雷德里克,蒙哥马利和乔治王子县。

整理数据后,现在我有以下代码,尝试使用ggplot和gganimate制作一系列动画图。我主要基于代码off of this example,但我也查看了at this article/post。明确地说,到目前为止,我的地块旨在使plot_DrAl1成为通勤时间在14分钟或更短时间内独自开车上班的人数,而plot_DrAl2是针对15-29分钟的通勤人群, plot_DrAl3适用于通勤时间为30-44分钟的人,plot_DrAl4适用于通勤时间为45-59分钟的人。 最终,我的目标是添加第五幅图,显示60分钟或更长时间的通勤者独自开车上班的人数

如果有人对我做错了什么或如何更好地实现自己的目标有所了解,请告诉我!我已经看过Stackoverflow上发布的一些相对类似的问题,但没有一个能够完全解决我的问题。

#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))

  plot_DrAl2 <- ggplot() +
    geom_polygon(data=dc_area, aes(Longitude, Lat), fill = "#e6d4ff") +
    geom_polygon(data=cars_1529, aes(x = `Longitude`, y = `Lat`, fill = `Perc_DrAl29`)) +

    # gganimate parts
    transition_states(`Perc_DrAl29`, 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))

  plot_DrAl3 <- ggplot() +
    geom_polygon(data=dc_area, aes(Longitude, Lat), fill = "#e6d4ff") +
    geom_polygon(data=cars_3044, aes(x = `Longitude`, y = `Lat`, fill = `Perc_DrAl44`)) +

    # gganimate parts
    transition_states(`Perc_DrAl44`, 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))

  plot_DrAl4 <- ggplot() +
    geom_polygon(data=dc_area, aes(Longitude, Lat), fill = "#e6d4ff") +
    geom_polygon(data=cars_4559, aes(x = `Longitude`, y = `Lat`, fill = `Perc_DrAl59`)) +

    # gganimate parts
    transition_states(`Perc_DrAl59`, 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)

0 个答案:

没有答案