Homework2

Author

Lydia Okabe

Quarto

Quarto enables you to weave together content and executable code into a finished document. To learn more about Quarto see https://quarto.org.

Running Code

When you click the Render button a document will be generated that includes both content and the output of embedded code. You can embed code like this:

1 + 1
[1] 2

You can add options to executable code like this

[1] 4

The echo: false option disables the printing of code (only output is displayed). 31b8e172-b470-440e-83d8-e6b185028602:dAB5AHAAZQA6AFoAUQBBAHgAQQBEAGcAQQBNAFEAQQA1AEEARABZAEEATQBBAEEAMQBBAEMAMABBAE0AQQBCAGgAQQBHAE0AQQBaAEEAQQB0AEEARABRAEEAWgBnAEIAaABBAEcAVQBBAEwAUQBBADQAQQBHAEkAQQBPAFEAQQA1AEEAQwAwAEEATwBRAEIAagBBAEQARQBBAFkAZwBBADMAQQBHAEkAQQBaAEEAQQAzAEEARwBNAEEATQBBAEIAbQBBAEQARQBBAAoAcABvAHMAaQB0AGkAbwBuADoATgBRAEEAMwBBAEQATQBBAAoAcAByAGUAZgBpAHgAOgAKAHMAbwB1AHIAYwBlADoATABRAEEAdABBAEMAMABBAEMAZwBCADAAQQBHAGsAQQBkAEEAQgBzAEEARwBVAEEATwBnAEEAZwBBAEMASQBBAFIAQQBCAEYAQQBFADAAQQBJAEEAQQAxAEEARABBAEEATwBRAEEAegBBAEMAOABBAE4AdwBBAHcAQQBEAGsAQQBNAHcAQQBnAEEARQB3AEEAWQBRAEIAaQBBAEMAQQBBAE0AUQBBAGcAQQBIAFUAQQBjAHcAQgBwAEEARwA0AEEAWgB3AEEAZwBBAEMAQQBBAFUAZwBBAGcAQQBDADAAQQBJAEEAQgBDAEEARwBFAEEAYwB3AEIAcABBAEcATQBBAEkAQQBCAE4AQQBHAEUAQQBjAEEAQQBnAEEARwAwAEEAWQBRAEIAcgBBAEcAawBBAGIAZwBCAG4AQQBDAEkAQQBDAGcAQgBoAEEASABVAEEAZABBAEIAbwBBAEcAOABBAGMAZwBBADYAQQBDAEEAQQBJAGcAQgBLAEEASABVAEEAYgBBAEIAcABBAEcARQBBAEkAQQBCAEwAQQBHAEUAQQBlAFEAQQBnAEEARgBjAEEAYgB3AEIAcwBBAEcAWQBBAEwAQQBBAGcAQQBGAEEAQQBhAEEAQQB1AEEARQBRAEEATABnAEEAaQBBAEEAbwBBAFoAQQBCAGgAQQBIAFEAQQBaAFEAQQA2AEEAQwBBAEEASQBnAEEAeQBBAEQAQQBBAE0AZwBBAHoAQQBDADAAQQBNAEEAQQB4AEEAQwAwAEEATQBnAEEAegBBAEMASQBBAEMAZwBCAHYAQQBIAFUAQQBkAEEAQgB3AEEASABVAEEAZABBAEEANgBBAEEAbwBBAEkAQQBBAGcAQQBIAEEAQQBaAEEAQgBtAEEARgA4AEEAWgBBAEIAdgBBAEcATQBBAGQAUQBCAHQAQQBHAFUAQQBiAGcAQgAwAEEARABvAEEASQBBAEIAawBBAEcAVQBBAFoAZwBCAGgAQQBIAFUAQQBiAEEAQgAwAEEAQQBvAEEASQBBAEEAZwBBAEcAZwBBAGQAQQBCAHQAQQBHAHcAQQBYAHcAQgBrAEEARwA4AEEAWQB3AEIAMQBBAEcAMABBAFoAUQBCAHUAQQBIAFEAQQBPAGcAQQBnAEEARwBRAEEAWgBRAEIAbQBBAEcARQBBAGQAUQBCAHMAQQBIAFEAQQBDAGcAQgBoAEEARwB3AEEAZAB3AEIAaABBAEgAawBBAGMAdwBCAGYAQQBHAEUAQQBiAEEAQgBzAEEARwA4AEEAZAB3AEIAZgBBAEcAZwBBAGQAQQBCAHQAQQBHAHcAQQBPAGcAQQBnAEEASABrAEEAWgBRAEIAegBBAEEAbwBBAEwAUQBBAHQAQQBDADAAQQAKAHMAdQBmAGYAaQB4ADoA:31b8e172-b470-440e-83d8-e6b185028602

setwd("C:/Users/okabe/OneDrive/Pictures/Stats 2")

Get a Census developer API Key

Obtain one at the Census Developer website

Save your API key to your working directory

use

tidycensus::census_api_key(key = "bd8624dfde3ddc5f432d85043185710be3e76257", install = T)

#tidycensus::census_api_key(key =  "bd8624dfde3ddc5f432d85043185710be3e76257", install = T)

One time to install your key for use in tidycensus

library(tidycensus)
library(tidyverse)
library(sf)
library(ggplot2)
library(classInt)

Examine data profile tables

The load_variables() function will load all available variables in the ACS for a given year

v15_Profile <- load_variables(year = 2019 ,
                              dataset =  "acs5/profile",
                              cache = TRUE)

Calling View(v15_Profile) will let you interactively browse and filter the ACS variables, this is one way to search for what you’re looking for.

Search for variables by keywords in the label

v15_Profile%>%
  filter(grepl(pattern = "Median", x = label))%>%
  select(name, label)
# A tibble: 24 × 2
   name       label                                                             
   <chr>      <chr>                                                             
 1 DP03_0062  Estimate!!INCOME AND BENEFITS (IN 2019 INFLATION-ADJUSTED DOLLARS…
 2 DP03_0062P Percent!!INCOME AND BENEFITS (IN 2019 INFLATION-ADJUSTED DOLLARS)…
 3 DP03_0086  Estimate!!INCOME AND BENEFITS (IN 2019 INFLATION-ADJUSTED DOLLARS…
 4 DP03_0086P Percent!!INCOME AND BENEFITS (IN 2019 INFLATION-ADJUSTED DOLLARS)…
 5 DP03_0090  Estimate!!INCOME AND BENEFITS (IN 2019 INFLATION-ADJUSTED DOLLARS…
 6 DP03_0090P Percent!!INCOME AND BENEFITS (IN 2019 INFLATION-ADJUSTED DOLLARS)…
 7 DP03_0092  Estimate!!INCOME AND BENEFITS (IN 2019 INFLATION-ADJUSTED DOLLARS…
 8 DP03_0092P Percent!!INCOME AND BENEFITS (IN 2019 INFLATION-ADJUSTED DOLLARS)…
 9 DP03_0093  Estimate!!INCOME AND BENEFITS (IN 2019 INFLATION-ADJUSTED DOLLARS…
10 DP03_0093P Percent!!INCOME AND BENEFITS (IN 2019 INFLATION-ADJUSTED DOLLARS)…
# … with 14 more rows

##do the same thing

v15_Profile%>%
  filter(grepl(pattern = "Built 2000 to 2009", x = label))%>%
  select(name, label)
# A tibble: 2 × 2
  name       label                                                              
  <chr>      <chr>                                                              
1 DP04_0019  Estimate!!YEAR STRUCTURE BUILT!!Total housing units!!Built 2000 to…
2 DP04_0019P Percent!!YEAR STRUCTURE BUILT!!Total housing units!!Built 2000 to …

Extract from ACS summary file data

The tidycensus package has a function get_acs() that will download data from the Census API for you automatically assuming you’ve installed your key from above

Here I get data profile variables from 2017 for Bexar County, TX Census Tracts

Here is a query where we extract several variables from the 2017 ACS for Bexar County, Texas. We can also get the spatial data by requesting geometry=TRUE.

Using output="wide" will put each variable in a column of the data set, with each row being a census tract.

sa_acs<-get_acs(geography = "tract",
                state="TX",
                county = "Bexar",
                year = 2019,
                variables=c("DP05_0001E", "DP03_0062E") ,
                geometry = T,
                output = "wide")
Getting data from the 2015-2019 5-year ACS
Downloading feature geometry from the Census website.  To cache shapefiles for use in future sessions, set `options(tigris_use_cache = TRUE)`.
Using the ACS Data Profile
head(sa_acs)

Here, I create some other variables that we may need later

# create a county FIPS code - 5 digit
sa_acs$county<-substr(sa_acs$GEOID, 1, 5)
# rename variables and filter missing cases
sa_acs2<-sa_acs%>%
  mutate(totpop= DP05_0001E,
         income=DP03_0062E) %>%  
  st_transform(crs = 2919)%>%
  na.omit()
#st_geometry(sa_acs2)<-NULL
#foreign::write.dbf(acs,file = "C:/Users/okabe/OneDrive/Pictures/Stats 2/Bexar_tracts.dbf")

Slightly easier way using tmap

The tmap package is an attractive alternative to using ggplot() when making maps, and makes basic cartographic principles easier.

Note style="fisher" is equivalent to style="jenks" and scales better to larger data.

library(tmap)
library(tmaptools)
tm_shape(sa_acs2)+
  tm_polygons("income",
              title="% Median Household Income",
              palette="Purples",
              style="pretty", n=5 )+
  tm_format("World",
            title="Author: Lydia Okabe",title.size= 5,
            legend.outside=T)+
  tm_layout(main.title = "Bexar County Median Income Estimates - Pretty", title.size= 20,
            main.title.position = "center")+
  tm_scale_bar()+
  tm_compass()