HW_2_Bexar County

Author

Coda Rayo-Garza

DEM 7093: Basic Maps Using Tidycensus + Saving Dataframe as Shapefile for QGIS

── Attaching packages ─────────────────────────────────────── tidyverse 1.3.2 ──
✔ ggplot2 3.4.0      ✔ purrr   1.0.0 
✔ tibble  3.1.8      ✔ dplyr   1.0.10
✔ tidyr   1.2.1      ✔ stringr 1.5.0 
✔ readr   2.1.3      ✔ forcats 0.5.2 
── Conflicts ────────────────────────────────────────── tidyverse_conflicts() ──
✖ dplyr::filter() masks stats::filter()
✖ dplyr::lag()    masks stats::lag()
Linking to GEOS 3.9.3, GDAL 3.5.2, PROJ 8.2.1; sf_use_s2() is TRUE

Examine data profile tables

load_variables() function loads all the variables in the ACS for a given year

Search for variables by keywords in the label

# A tibble: 24 × 2
   name       label                                                             
   <chr>      <chr>                                                             
 1 DP03_0062  Estimate!!INCOME AND BENEFITS (IN 2021 INFLATION-ADJUSTED DOLLARS…
 2 DP03_0062P Percent!!INCOME AND BENEFITS (IN 2021 INFLATION-ADJUSTED DOLLARS)…
 3 DP03_0063  Estimate!!INCOME AND BENEFITS (IN 2021 INFLATION-ADJUSTED DOLLARS…
 4 DP03_0063P Percent!!INCOME AND BENEFITS (IN 2021 INFLATION-ADJUSTED DOLLARS)…
 5 DP03_0067  Estimate!!INCOME AND BENEFITS (IN 2021 INFLATION-ADJUSTED DOLLARS…
 6 DP03_0067P Percent!!INCOME AND BENEFITS (IN 2021 INFLATION-ADJUSTED DOLLARS)…
 7 DP03_0068  Estimate!!INCOME AND BENEFITS (IN 2021 INFLATION-ADJUSTED DOLLARS…
 8 DP03_0068P Percent!!INCOME AND BENEFITS (IN 2021 INFLATION-ADJUSTED DOLLARS)…
 9 DP03_0069  Estimate!!INCOME AND BENEFITS (IN 2021 INFLATION-ADJUSTED DOLLARS…
10 DP03_0069P Percent!!INCOME AND BENEFITS (IN 2021 INFLATION-ADJUSTED DOLLARS)…
# … with 14 more rows
# v15_Profile%>%
#   filter(grepl(pattern = "Built 2000 to 2009", x = label))%>%
#   select(name, label)

Extract from ACS summary file data

Getting data profile for Bexar County from ACS 5-Year dataset. I am getting total population and median income. The function geometry=TRUE gives me spatial data and output=“wide” puts each variable in a column of the dataset and each row becomes a census tract.

#|warning: false
#|message: false
#|echo:fenced


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
Warning: • You have not set a Census API key. Users without a key are limited to 500
queries per day and may experience performance limitations.
ℹ For best results, get a Census API key at
http://api.census.gov/data/key_signup.html and then supply the key to the
`census_api_key()` function to use it throughout your tidycensus session.
This warning is displayed once per session.
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

  |                                                                            
  |                                                                      |   0%
  |                                                                            
  |=                                                                     |   1%
  |                                                                            
  |=                                                                     |   2%
  |                                                                            
  |==                                                                    |   3%
  |                                                                            
  |===                                                                   |   4%
  |                                                                            
  |====                                                                  |   5%
  |                                                                            
  |====                                                                  |   6%
  |                                                                            
  |=====                                                                 |   7%
  |                                                                            
  |=====                                                                 |   8%
  |                                                                            
  |======                                                                |   8%
  |                                                                            
  |======                                                                |   9%
  |                                                                            
  |=======                                                               |   9%
  |                                                                            
  |=======                                                               |  10%
  |                                                                            
  |=======                                                               |  11%
  |                                                                            
  |========                                                              |  11%
  |                                                                            
  |========                                                              |  12%
  |                                                                            
  |=========                                                             |  13%
  |                                                                            
  |=========                                                             |  14%
  |                                                                            
  |==========                                                            |  15%
  |                                                                            
  |===========                                                           |  15%
  |                                                                            
  |===========                                                           |  16%
  |                                                                            
  |============                                                          |  17%
  |                                                                            
  |=============                                                         |  18%
  |                                                                            
  |=============                                                         |  19%
  |                                                                            
  |==============                                                        |  19%
  |                                                                            
  |==============                                                        |  20%
  |                                                                            
  |==============                                                        |  21%
  |                                                                            
  |===============                                                       |  21%
  |                                                                            
  |===============                                                       |  22%
  |                                                                            
  |================                                                      |  23%
  |                                                                            
  |=================                                                     |  24%
  |                                                                            
  |=================                                                     |  25%
  |                                                                            
  |==================                                                    |  25%
  |                                                                            
  |==================                                                    |  26%
  |                                                                            
  |===================                                                   |  27%
  |                                                                            
  |====================                                                  |  28%
  |                                                                            
  |====================                                                  |  29%
  |                                                                            
  |=====================                                                 |  30%
  |                                                                            
  |======================                                                |  31%
  |                                                                            
  |======================                                                |  32%
  |                                                                            
  |=======================                                               |  33%
  |                                                                            
  |========================                                              |  34%
  |                                                                            
  |========================                                              |  35%
  |                                                                            
  |=========================                                             |  35%
  |                                                                            
  |=========================                                             |  36%
  |                                                                            
  |==========================                                            |  37%
  |                                                                            
  |==========================                                            |  38%
  |                                                                            
  |===========================                                           |  38%
  |                                                                            
  |===========================                                           |  39%
  |                                                                            
  |============================                                          |  39%
  |                                                                            
  |============================                                          |  40%
  |                                                                            
  |============================                                          |  41%
  |                                                                            
  |=============================                                         |  41%
  |                                                                            
  |=============================                                         |  42%
  |                                                                            
  |==============================                                        |  42%
  |                                                                            
  |==============================                                        |  43%
  |                                                                            
  |===============================                                       |  44%
  |                                                                            
  |===============================                                       |  45%
  |                                                                            
  |================================                                      |  45%
  |                                                                            
  |================================                                      |  46%
  |                                                                            
  |=================================                                     |  47%
  |                                                                            
  |=================================                                     |  48%
  |                                                                            
  |==================================                                    |  48%
  |                                                                            
  |==================================                                    |  49%
  |                                                                            
  |===================================                                   |  50%
  |                                                                            
  |====================================                                  |  51%
  |                                                                            
  |====================================                                  |  52%
  |                                                                            
  |=====================================                                 |  52%
  |                                                                            
  |=====================================                                 |  53%
  |                                                                            
  |=====================================                                 |  54%
  |                                                                            
  |======================================                                |  54%
  |                                                                            
  |======================================                                |  55%
  |                                                                            
  |=======================================                               |  55%
  |                                                                            
  |=======================================                               |  56%
  |                                                                            
  |========================================                              |  57%
  |                                                                            
  |========================================                              |  58%
  |                                                                            
  |=========================================                             |  58%
  |                                                                            
  |=========================================                             |  59%
  |                                                                            
  |==========================================                            |  60%
  |                                                                            
  |===========================================                           |  61%
  |                                                                            
  |===========================================                           |  62%
  |                                                                            
  |============================================                          |  62%
  |                                                                            
  |============================================                          |  63%
  |                                                                            
  |=============================================                         |  64%
  |                                                                            
  |==============================================                        |  65%
  |                                                                            
  |==============================================                        |  66%
  |                                                                            
  |===============================================                       |  67%
  |                                                                            
  |===============================================                       |  68%
  |                                                                            
  |================================================                      |  69%
  |                                                                            
  |=================================================                     |  69%
  |                                                                            
  |=================================================                     |  70%
  |                                                                            
  |==================================================                    |  71%
  |                                                                            
  |===================================================                   |  72%
  |                                                                            
  |===================================================                   |  73%
  |                                                                            
  |====================================================                  |  74%
  |                                                                            
  |=====================================================                 |  76%
  |                                                                            
  |======================================================                |  77%
  |                                                                            
  |======================================================                |  78%
  |                                                                            
  |=======================================================               |  78%
  |                                                                            
  |=======================================================               |  79%
  |                                                                            
  |========================================================              |  80%
  |                                                                            
  |========================================================              |  81%
  |                                                                            
  |=========================================================             |  81%
  |                                                                            
  |=========================================================             |  82%
  |                                                                            
  |==========================================================            |  82%
  |                                                                            
  |==========================================================            |  83%
  |                                                                            
  |===========================================================           |  84%
  |                                                                            
  |===========================================================           |  85%
  |                                                                            
  |============================================================          |  85%
  |                                                                            
  |============================================================          |  86%
  |                                                                            
  |=============================================================         |  87%
  |                                                                            
  |=============================================================         |  88%
  |                                                                            
  |==============================================================        |  88%
  |                                                                            
  |==============================================================        |  89%
  |                                                                            
  |===============================================================       |  90%
  |                                                                            
  |===============================================================       |  91%
  |                                                                            
  |================================================================      |  91%
  |                                                                            
  |================================================================      |  92%
  |                                                                            
  |=================================================================     |  92%
  |                                                                            
  |=================================================================     |  93%
  |                                                                            
  |=================================================================     |  94%
  |                                                                            
  |==================================================================    |  94%
  |                                                                            
  |==================================================================    |  95%
  |                                                                            
  |===================================================================   |  96%
  |                                                                            
  |====================================================================  |  97%
  |                                                                            
  |====================================================================  |  98%
  |                                                                            
  |===================================================================== |  98%
  |                                                                            
  |===================================================================== |  99%
  |                                                                            
  |======================================================================| 100%
head(sa_acs)
Simple feature collection with 6 features and 6 fields
Geometry type: MULTIPOLYGON
Dimension:     XY
Bounding box:  xmin: -98.56606 ymin: 29.36662 xmax: -98.44832 ymax: 29.54044
Geodetic CRS:  NAD83
        GEOID                                      NAME DP05_0001E DP05_0001M
1 48029190601 Census Tract 1906.01, Bexar County, Texas       4514        788
2 48029181100    Census Tract 1811, Bexar County, Texas       6359        523
3 48029140700    Census Tract 1407, Bexar County, Texas       4838        303
4 48029180602 Census Tract 1806.02, Bexar County, Texas       3751        536
5 48029140800    Census Tract 1408, Bexar County, Texas       5124        453
6 48029170200    Census Tract 1702, Bexar County, Texas       5599        617
  DP03_0062E DP03_0062M                       geometry
1      46818      14436 MULTIPOLYGON (((-98.52602 2...
2      64930       6521 MULTIPOLYGON (((-98.56225 2...
3      36222       6656 MULTIPOLYGON (((-98.47348 2...
4      38389       7038 MULTIPOLYGON (((-98.56606 2...
5      43299       3656 MULTIPOLYGON (((-98.4778 29...
6      25755       7141 MULTIPOLYGON (((-98.5283 29...

Create a county FIPS code - 5 digit

# 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,
         medinc=DP03_0062E) %>%
  st_transform(crs = 2919)%>%
  na.omit()

Optional* Write data out to a shapefile

Mapping Variables with GGplot

#|code-fold: true

library(patchwork)
library(ggsn)
Loading required package: grid
#library(XploreR)
source("https://raw.githubusercontent.com/coreysparks/Rcode/master/mutate_map_funs.R")

income_map<-sa_acs2 %>%
  mutate_map_brks(medinc, n=6, style="quantile")%>%
           mutate_map_brks(medinc, n=6, style="jenks")



p1<-ggplot(income_map, aes(fill = medinc_qbrks)) + 
  geom_sf(color="grey50") + 
  ggtitle("Median Household Income", 
          subtitle = "Bexar County Texas, 2019 - Quantile Breaks")+
    scale_fill_brewer(palette = "Reds") + 
  scale_color_brewer(palette = "Reds")+
  theme(axis.text.x = element_blank(),
        axis.text.y = element_blank())+
  north(income_map)+
  scalebar(income_map, location="bottomleft",
           dist=5, transform = T,
           dist_unit = "km",
           model="WGS84",
           st.size =2 )
p1

p2<-ggplot(income_map, aes(fill = medinc_jbrks)) + 
  geom_sf(color="grey50") + 
  ggtitle("Median Household Income", 
          subtitle = "Bexar County Texas, 2019 - Jenks Breaks")+
  scale_fill_brewer(palette = "Reds") + 
  scale_color_brewer(palette = "Reds")+
    theme(axis.text.x = element_blank(),
          axis.text.y = element_blank())+
  north(income_map)+
  scalebar(income_map, 
           location="bottomleft",
           dist=5,
           transform = T,
           dist_unit = "km",
           model="WGS84",
           st.size =2)
p2

library(tmap)
library(tmaptools)


tm_shape(sa_acs2)+
  tm_polygons("medinc",
              title="Median Income",
              palette="Greens",
              style="pretty", n=5 )+
  tm_format("World",
            title="Author:Coda Rayo-Garza",
            legend.outside=T)+
  tm_layout(main.title = "Bexar County Median Income Estimates:Pretty Breaks",
            main.title.position = "center")+
  tm_scale_bar()+
  tm_compass()

tm_shape(sa_acs2)+
  tm_polygons("medinc",
              title="Median Income",
              palette="Greens",
              style="fisher",
              n=5,
              legend.hist=T )+
  tm_format("World",
            title="Author:Coda Rayo-Garza", 
            legend.outside=T)+
  tm_layout(main.title = "Bexar County Median Income Estimates:Jenks Breaks",
            main.title.position = "center")+
  tm_scale_bar()+
  tm_compass()

Making An Interactive Map Using Mapview

library(mapview)
library(RColorBrewer)
income_map$medinc_jbrks<-relevel(income_map$medinc_jbrks,ref = "0-10463" )

##ppov_map$ppov_jbrks was from the ggplot section

pal <- colorRampPalette(brewer.pal(7, "Blues")) #set colors

# to change the pivot table that populates when you click on a geo element on the map:
# sub <- ppov_map %>% 
  # select(Poverty_Rate = ppov_jbrks, ppov, Name = NAME)

mapview(income_map,
        zcol="medinc_jbrks",
        legend=T,
        map.types="OpenStreetMap",
        layer.name="Household Median Income")