geofacet
geofacet copied to clipboard
new grid: 'us_state_grid2_withPR'
Adaptation of us_state_grid2 which adds Puerto Rico (PR) to the bottom right.
Relevant map: https://upload.wikimedia.org/wikipedia/commons/1/13/United_States_in_its_region_%28Lower_50_and_Puerto_Rico_special%29.svg
Grid data:
"row","col","code","name"
6,7,"AL","Alabama"
1,1,"AK","Alaska"
6,2,"AZ","Arizona"
6,5,"AR","Arkansas"
6,1,"CA","California"
5,3,"CO","Colorado"
2,10,"CT","Connecticut"
4,11,"DE","Delaware"
7,9,"FL","Florida"
6,8,"GA","Georgia"
8,1,"HI","Hawaii"
4,2,"ID","Idaho"
4,6,"IL","Illinois"
4,7,"IN","Indiana"
4,5,"IA","Iowa"
5,4,"KS","Kansas"
5,7,"KY","Kentucky"
7,5,"LA","Louisiana"
1,12,"ME","Maine"
5,10,"MD","Maryland"
2,11,"MA","Massachusetts"
3,7,"MI","Michigan"
3,5,"MN","Minnesota"
6,6,"MS","Mississippi"
5,5,"MO","Missouri"
3,2,"MT","Montana"
4,4,"NE","Nebraska"
5,1,"NV","Nevada"
1,11,"NH","New Hampshire"
3,10,"NJ","New Jersey"
6,3,"NM","New Mexico"
2,9,"NY","New York"
5,9,"NC","North Carolina"
3,3,"ND","North Dakota"
4,8,"OH","Ohio"
6,4,"OK","Oklahoma"
4,1,"OR","Oregon"
3,9,"PA","Pennsylvania"
3,11,"RI","Rhode Island"
6,9,"SC","South Carolina"
3,4,"SD","South Dakota"
5,6,"TN","Tennessee"
7,4,"TX","Texas"
5,2,"UT","Utah"
1,10,"VT","Vermont"
4,9,"VA","Virginia"
3,1,"WA","Washington"
5,8,"WV","West Virginia"
3,6,"WI","Wisconsin"
4,3,"WY","Wyoming"
4,10,"DC","District of Columbia"
8,11,"PR","Puerto Rico"
Example using this grid:
library(geofacet)
library(tidyverse)
library(tidycensus)
d20 <- get_decennial(
"state",
variables=c("HU_Tot"="H1_001N",
"HU_Occ"="H1_002N",
"Pop_Tot"="P1_001N",
"Pop_GQ"="P5_001N"),
year=2020) %>%
mutate(
Year=2020,
Type="Decennial")
#> Getting data from the 2020 decennial Census
#> Using the PL 94-171 Redistricting Data summary file
#> Note: 2020 decennial Census data use differential privacy, a technique that
#> introduces errors into data to preserve respondent confidentiality.
#> i Small counts should be interpreted with caution.
#> i See https://www.census.gov/library/fact-sheets/2021/protecting-the-confidentiality-of-the-2020-census-redistricting-data.html for additional guidance.
#> This message is displayed once per session.
d10 <- get_decennial(
"state",
variables=c("HU_Tot"="H003001",
"HU_Occ"="H003002",
"Pop_Tot"="H010001",
"Pop_GQ"="P042001"),
year=2010) %>%
mutate(
Year=2010,
Type="Decennial")
#> Getting data from the 2010 decennial Census
#> Using Census Summary File 1
get_acs_spec <- function(yr){
get_acs(
"state",
variables=c("HU_Tot"="B25002_001",
"HU_Occ"="B25002_002",
"Pop_Tot"="B01001_001",
"Pop_GQ"="B26001_001"),
survey="acs1",
year=yr
) %>%
mutate(
Year=yr,
Type="ACS") %>%
rename(value=estimate)
}
dacs <- 2010:2019 %>% map_df(get_acs_spec)
#> The 1-year ACS provides data for geographies with populations of 65,000 and greater.
#> Getting data from the 2010 1-year ACS
#> The 1-year ACS provides data for geographies with populations of 65,000 and greater.
#> Getting data from the 2011 1-year ACS
#> The 1-year ACS provides data for geographies with populations of 65,000 and greater.
#> Getting data from the 2012 1-year ACS
#> The 1-year ACS provides data for geographies with populations of 65,000 and greater.
#> Getting data from the 2013 1-year ACS
#> The 1-year ACS provides data for geographies with populations of 65,000 and greater.
#> Getting data from the 2014 1-year ACS
#> The 1-year ACS provides data for geographies with populations of 65,000 and greater.
#> Getting data from the 2015 1-year ACS
#> The 1-year ACS provides data for geographies with populations of 65,000 and greater.
#> Getting data from the 2016 1-year ACS
#> The 1-year ACS provides data for geographies with populations of 65,000 and greater.
#> Getting data from the 2017 1-year ACS
#> The 1-year ACS provides data for geographies with populations of 65,000 and greater.
#> Getting data from the 2018 1-year ACS
#> The 1-year ACS provides data for geographies with populations of 65,000 and greater.
#> Getting data from the 2019 1-year ACS
d_all <- bind_rows(dacs, d10, d20) %>%
# filter(GEOID!="72") %>%
mutate(
StAbb=cdlTools::fips(GEOID, to="abbreviation")
) %>%
arrange(StAbb, variable, desc(Type), Year) %>%
group_by(StAbb, variable) %>%
mutate(
Ratio=value/value[1]
) %>%
ungroup()
my_grid <- us_state_grid2 %>%
add_row(row=8, col=11, code="PR", name="Puerto Rico")
d_all %>%
filter(variable=="HU_Tot") %>%
ggplot(aes(x=Year, y=Ratio, group=Type, colour=Type)) +
geom_line() +
facet_geo(~StAbb, grid=my_grid) +
scale_x_continuous(breaks=c(2010, 2015, 2020), labels = function(x) paste0("'", substr(x, 3, 4))) +
ggtitle("Ratio of total housing units to 2010 Decennial") +
theme(legend.position = "bottom")
#> Note: You provided a user-specified grid. If this is a generally-useful
#> grid, please consider submitting it to become a part of the geofacet
#> package. You can do this easily by calling:
#> grid_submit(__grid_df_name__)

Created on 2021-10-06 by the reprex package (v2.0.1)