The American Community Survey’s DP02_0114P
variable
estimates the percentage of people age 5 and older who speak a language
other than English while at home. This R script maps that variable by
Tennessee school district.
if (!require("tidyverse")) install.packages("tidyverse")
if (!require("tidycensus")) install.packages("tidycensus")
if (!require("mapview")) install.packages("mapview")
if (!require("sf")) install.packages("sf")
library(tidyverse)
library(tidycensus)
library(mapview)
mapviewOptions(basemaps.color.shuffle = FALSE)
library(sf)
#census_api_key("PasteYourAPIKeyBetweenTheseQuoteMarks")
DetailedTables <- load_variables(2022, "acs5", cache = TRUE)
SubjectTables <- load_variables(2022, "acs5/subject", cache = TRUE)
ProfileTables <- load_variables(2022, "acs5/profile", cache = TRUE)
Elementary <- get_acs(
geography = "school district (elementary)",
state = "TN",
variables = c(MyVar_ = "DP02_0114P"),
year = 2022,
survey = "acs5",
output = "wide",
geometry = TRUE)
Secondary <- get_acs(
geography = "school district (secondary)",
state = "TN",
variables = c(MyVar_ = "DP02_0114P"),
year = 2022,
survey = "acs5",
output = "wide",
geometry = TRUE)
Unified <- get_acs(
geography = "school district (unified)",
state = "TN",
variables = c(MyVar_ = "DP02_0114P"),
year = 2022,
survey = "acs5",
output = "wide",
geometry = TRUE)
mydata <- rbind(Unified,Secondary)
mydata <-
separate_wider_delim(mydata,
NAME,
delim = ", ",
names = c("District", "State"))
mydata <- arrange(mydata, desc(MyVar_E))
write.csv(mydata, "mydata.csv", row.names = FALSE)
mydata_sf <- st_as_sf(mydata)
mydata_sf %>%
ggplot(aes(fill = MyVar_E)) +
geom_sf(color = NA) +
scale_fill_viridis_c(option = "magma")
mydata_mapview <- mapview(mydata_sf, zcol = "MyVar_E",
col.regions = RColorBrewer::brewer.pal(9, "Blues"), alpha.regions = .5,
layer.name = "Pct. non-English at home",#Customizable
popup = TRUE)
mydata_mapview
Here is the script in action, with its output:
if (!require("tidyverse")) install.packages("tidyverse")
if (!require("tidycensus")) install.packages("tidycensus")
if (!require("mapview")) install.packages("mapview")
if (!require("sf")) install.packages("sf")
library(tidyverse)
library(tidycensus)
library(mapview)
mapviewOptions(basemaps.color.shuffle = FALSE)
library(sf)
#census_api_key("PasteYourAPIKeyBetweenTheseQuoteMarks")
DetailedTables <- load_variables(2022, "acs5", cache = TRUE)
SubjectTables <- load_variables(2022, "acs5/subject", cache = TRUE)
ProfileTables <- load_variables(2022, "acs5/profile", cache = TRUE)
Elementary <- get_acs(
geography = "school district (elementary)",
state = "TN",
variables = c(MyVar_ = "DP02_0114P"),
year = 2022,
survey = "acs5",
output = "wide",
geometry = TRUE)
##
|
| | 0%
|
|== | 3%
|
|======================= | 33%
|
|================================== | 49%
|
|======================================================== | 79%
|
|================================================================== | 95%
|
|======================================================================| 100%
Secondary <- get_acs(
geography = "school district (secondary)",
state = "TN",
variables = c(MyVar_ = "DP02_0114P"),
year = 2022,
survey = "acs5",
output = "wide",
geometry = TRUE)
##
|
| | 0%
|
|== | 3%
|
|======================= | 33%
|
|================================== | 49%
|
|======================================================= | 79%
|
|======================================================================| 100%
Unified <- get_acs(
geography = "school district (unified)",
state = "TN",
variables = c(MyVar_ = "DP02_0114P"),
year = 2022,
survey = "acs5",
output = "wide",
geometry = TRUE)
##
|
| | 0%
|
|== | 3%
|
|===== | 7%
|
|======= | 10%
|
|======== | 12%
|
|========== | 15%
|
|============ | 18%
|
|============== | 19%
|
|================= | 24%
|
|=================== | 27%
|
|====================== | 31%
|
|======================== | 34%
|
|========================= | 36%
|
|=========================== | 39%
|
|============================= | 42%
|
|================================ | 46%
|
|================================= | 48%
|
|======================================= | 55%
|
|========================================= | 58%
|
|========================================== | 60%
|
|============================================ | 63%
|
|================================================= | 70%
|
|================================================== | 72%
|
|==================================================== | 75%
|
|========================================================== | 82%
|
|=============================================================== | 90%
|
|======================================================================| 100%
mydata <- rbind(Unified,Secondary)
mydata <-
separate_wider_delim(mydata,
NAME,
delim = ", ",
names = c("District", "State"))
mydata <- arrange(mydata, desc(MyVar_E))
write.csv(mydata, "mydata.csv", row.names = FALSE)
mydata_sf <- st_as_sf(mydata)
mydata_sf %>%
ggplot(aes(fill = MyVar_E)) +
geom_sf(color = NA) +
scale_fill_viridis_c(option = "magma")
mydata_mapview <- mapview(mydata_sf, zcol = "MyVar_E",
col.regions = RColorBrewer::brewer.pal(9, "Blues"), alpha.regions = .5,
layer.name = "Pct. non-English at home",#Customizable
popup = TRUE)
mydata_mapview