Skip to content

Instantly share code, notes, and snippets.

View walkerke's full-sized avatar

Kyle Walker walkerke

View GitHub Profile
library(tidyverse)
library(animation)
library(readxl)
library(extrafont)
# Download file from: https://esa.un.org/unpd/wup/cd-rom/WUP2014_XLS_CD_FILES/WUP2014-F11a-30_Largest_Cities.xls
df <- read_excel('WUP2014-F11a-30_Largest_Cities.xls', skip = 17,
col_names = c("index", "year", "order", "ccode",
"country", "city", "name", "note", "lat",
library(tidycensus)
library(tidyverse)
library(tigris)
library(sf)
options(tigris_class = "sf")
options(tigris_use_cache = TRUE)
rent <- get_acs(geography = "tract", variables = "DP04_0134",
state = c("WA", "OR", "CA"), geometry = TRUE)
library(tidycensus)
library(tidyverse)
library(tigris)
library(sf)
library(ggbeeswarm)
library(viridis)
library(extrafont)
options(tigris_use_cache = TRUE)
df <- get_acs(geography = "tract", state = "TX",
# devtools::install_github("walkerke/tidycensus")
library(tidycensus)
library(tidyverse)
library(geofacet)
library(stringr)
library(extrafont)
# 2010 Census
age <- get_decennial(geography = "county", state = "AZ", table = "P012",
# devtools::install_github("walkerke/tidycensus")
library(tidycensus)
library(tidyverse)
library(geofacet)
library(stringr)
library(extrafont)
# 2010 Census
age <- get_decennial(geography = "county", state = "AK", table = "P012",
library(tidycensus)
library(tidyverse)
library(geofacet)
library(stringr)
library(extrafont)
age <- get_acs(geography = "county", state = "AL", table = "B01001",
summary_var = "B01001_001") %>%
mutate(variable = str_replace(variable, "B01001_0", "")) %>%
filter(!variable %in% c("01", "02", "26"))
name sex age percent
Alabama Male 0-4 2.949170316451792
Alabama Male 5-9 3.332079040980404
Alabama Male 10-14 3.2076367898340634
Alabama Male 15-19 3.5503875968992245
Alabama Male 20-24 3.3283572882610573
Alabama Male 25-29 3.356424649929061
Alabama Male 30-34 3.0474369255443836
Alabama Male 35-39 2.9739477309645714
Alabama Male 40-44 3.013571032015298
library(tidycensus)
library(tidyverse)
# If not set, un-comment below and install your Census API key (https://api.census.gov/data/key_signup.html)
# census_api_key("YOUR KEY HERE", install = TRUE)
get_acs(geography = "metropolitan statistical area/micropolitan statistical area",
variables = "DP03_0021PE",
summary_var = "B01003_001",
survey = "acs1",

Here is the process I used to create the 2016 presidential election by county map with zero-population blocks removed.

  1. I obtained US county boundaries and Census block boundaries by state from NHGIS, http://www.nhgis.org, along with Table P1 (Total Population) from the 2010 US Census. Your NHGIS IDs would be different.
  2. I ran the following R code to walk through every block boundary dataset, identify the no-population blocks, then write out the result as a shapefile:
library(tidyverse)
library(sf)
library(tigris)
X Y metro metro_id address city state zip
-84.390267 33.747917 Atlanta-Sandy Springs-Marietta, GA Metro Area 12060 55 Trinity Ave Atlanta GA 30303
-97.746682 30.26499 Austin-Round Rock-San Marcos, TX Metro Area 12420 301 W 2nd St Austin TX 78701
-76.610257 39.290691 Baltimore-Towson, MD Metro Area 12580 100 N Holliday Street Baltimore MD 21202
-86.810567 33.520202 Birmingham-Hoover, AL Metro Area 13820 710 20th Street N Birmingham AL 35203
-71.057294 42.360166 Boston-Cambridge-Quincy, MA-NH Metro Area 14460 1 City Hall Square Boston MA 02201
-78.878837 42.886517 Buffalo-Niagara Falls, NY Metro Area 15380 65 Niagara Square Buffalo NY 14202
-80.837581 35.222488 Charlotte-Gastonia-Rock Hill, NC-SC Metro Area 16740 600 E Trade St Charlotte NC 28202
-87.632324 41.883814 Chicago-Joliet-Naperville, IL-IN-WI Metro Area 16980 121 N La Salle St Chicago IL 60602
-84.518806 39.104273 Cincinnati-Middletown, OH-KY-IN Metro Area 17140 801 Plum St Cincinnati OH 45202