Tami L Crawford
August 13, 2016
Some detailed notes on how to get a dataset and display a county level data on a choropleth map using choroplethr
US Census population estimates * are available by county - Downloadable Data Sets
| Year | Corresponds to |
|---|---|
| 1 | 4/1/20 Census |
| 2 | 4/1/2010 estimate |
| 3 | 7/1/2010 estimate |
| 4 | 7/1/2011 estimate |
| 5 | 7/1/2012 estimate |
| 6 | 7/1/2013 estimate |
| 7 | 7/1/2014 estimate |
| 8 | 7/1/2015 estimate |
These examples use the following packages (uaw install.packages to install before running the examples)
filter(YEAR == 6)
transmute(region=as.numeric(COUNTY) + as.numeric(STATE)*1000, value = TOT_POP)
group_by(region) %>%
summarise(value = sum(value)) %>%
library(choroplethr)
library(ggplot2)
library(choroplethrMaps)
library(dplyr)
data <-
read.csv("https://www.census.gov/popest/data/counties/asrh/2015/files/CC-EST2015-ALLDATA-45.csv",skipNul=TRUE) %>%
filter(YEAR == 6) %>%
transmute(region=as.numeric(COUNTY) + as.numeric(STATE)*1000, value = TOT_POP) %>%
group_by(region) %>%
summarise(value = sum(value)) %>%
select(region,value)
ch = CountyChoropleth$new(data)
ch$title = "State Population - 2014"
ch$set_zoom("south carolina")
ch$ggplot_scale = scale_fill_brewer(name="Population (2014)", type="seq", palette=2)
ch$render()
Another example, using the same dataset but displaying the majority race for each county
Read the data and filter by year, then combine male and female totals by race, group by the county, and find the majority race, below shows only what is different from 1st example
transmute(region= as.numeric(COUNTY) + as.numeric(STATE)*1000,
num.black = BA_MALE + BA_FEMALE,
num.white = WA_MALE + WA_FEMALE,
num.indian = IA_FEMALE + IA_MALE,
num.asian = AA_FEMALE + AA_MALE,
num.hawaiian = NA_FEMALE + NA_MALE) %>%
group_by(region) %>%
summarise(num.black = sum(num.black),
num.white = sum(num.white),
num.indian = sum(num.indian),
num.asian = sum(num.asian),
num.hawaiian = sum(num.hawaiian))
data$mrace <- apply(data,1,majority.race)
data <- select(data,region,value=mrace)
the complete data processing code for this example
Function to create majority race based on the totals for each race (probably not the best way to do this…)
majority.race <- function(a) {
num.black = a["num.black"]
num.white = a["num.white"]
num.indian = a["num.indian"]
num.asian = a["num.asian"]
num.hawaiian = a["num.hawaiian"]
if (num.black > num.white && num.black > num.indian && num.black > num.asian && num.black > num.hawaiian) {
code = "BLACK"
} else if (num.white > num.black && num.white > num.indian && num.white > num.asian && num.white > num.hawaiian) {
code = "WHITE"
} else if (num.asian > num.black && num.asian > num.indian && num.asian > num.white && num.asian > num.hawaiian) {
code = "ASIAN"
} else if (num.indian > num.black && num.indian > num.white && num.indian > num.white && num.indian > num.hawaiian) {
code = "INDIAN"
} else if (num.hawaiian > num.black && num.hawaiian > num.indian && num.hawaiian > num.white && num.hawaiian > num.white) {
code = "HAWAIIAN" # return ("HAWAIIAN")
} else {
code = "UNKNOWN"
}
code
}
data <-
read.csv("https://www.census.gov/popest/data/counties/asrh/2015/files/CC-EST2015-ALLDATA-45.csv",skipNul=TRUE)
data <-
filter(data, YEAR == yearcode) %>%
transmute(region= as.numeric(COUNTY) + as.numeric(STATE)*1000,
num.black = BA_MALE + BA_FEMALE,
num.white = WA_MALE + WA_FEMALE,
num.indian = IA_FEMALE + IA_MALE,
num.asian = AA_FEMALE + AA_MALE,
num.hawaiian = NA_FEMALE + NA_MALE) %>%
group_by(region) %>%
summarise(num.black = sum(num.black),
num.white = sum(num.white),
num.indian = sum(num.indian),
num.asian = sum(num.asian),
num.hawaiian = sum(num.hawaiian))
data$mrace <- apply(data,1,majority.race)
data <- select(data,region,value=mrace)
ch = CountyChoropleth$new(data)
ch$title = paste("State Majority Race - ", 2014)
ch$set_zoom("south carolina")
ch$ggplot_scale = scale_fill_brewer(name=("Race (2014)", type="qual", palette=2)
ch$render()