diff --git a/resources/Mapping.Rmd b/resources/Mapping.Rmd index 689e4a2f..191dafe2 100644 --- a/resources/Mapping.Rmd +++ b/resources/Mapping.Rmd @@ -22,6 +22,30 @@ Maps can be tricky in R! There are many packages to choose from. - Some require API keys (e.g., `ggmap`, `tidycensus`) - Some are interactive (e.g., `leaflet`) +## What does a map in R look like? + +```{r message=FALSE, warning=FALSE, echo=FALSE} +library(tidyverse) # `geom_sf()` from ggplot2 +library(tidycensus) # `get_acs()` function for American Community Survey data + +wa_income <- get_acs( + geography = "tract", + variables = "B19013_001", # Income key + state = "WA", + year = 2022, + geometry = TRUE +) + +ggplot(data = wa_income, aes(fill = estimate)) + + geom_sf() + + labs(title = "Median Household Income by Census Tract", + x = "Longitude", + y = "Latitude") + + theme_classic() + + scale_fill_viridis_c() + + theme(legend.title = element_blank()) +``` + ## Data formats - boundary data ```{r message=FALSE} @@ -53,10 +77,14 @@ These objects store geometric shapes (like points, lines, or polygons) along wit the `geom` column is a `MULTIPOLYGON` — a geometry type representing complex shapes, which may consist of multiple polygons (e.g., islands or non-contiguous regions). +Federal Information Processing System (FIPS) Codes for States and Counties are numbers which uniquely identify geographic areas. See [this codebook](https://transition.fcc.gov/oet/info/maps/census/fips/fips.txt). + ## `ggplot` has spatial functions{.codesmall} `geom_polygon()` works with boundary data +Let's plot county outlines and major cities. + ```{r message=FALSE} library(tidyverse) # `map_data()` from ggplot2 library(maps) # `us.cities` data @@ -101,6 +129,8 @@ title(main = "Washington State Cities") ## `usmap` is compatible with ggplot +Let's fill each county based on its population. + ```{r message=FALSE} library(tidyverse) library(usmap) # `countypop` data and the `plot_usmap()` function @@ -170,13 +200,15 @@ plot_4 Use `geom_sf()` function with SF data. +Let's fill each census tract by median household income. + ```{r message=FALSE, warning=FALSE} library(tidyverse) # `geom_sf()` from ggplot2 library(tidycensus) # `get_acs()` function for American Community Survey data wa_income <- get_acs( geography = "tract", - variables = "B19013_001", # Income key + variables = "B19013_001", # Median income code state = "WA", year = 2022, geometry = TRUE diff --git a/resources/Mapping.html b/resources/Mapping.html index 783d89ed..29c33015 100644 --- a/resources/Mapping.html +++ b/resources/Mapping.html @@ -3270,6 +3270,12 @@

  • Some are interactive (e.g., leaflet)
  • +

    What does a map in R look like?

    + +
      |                                                                              |                                                                      |   0%  |                                                                              |=                                                                     |   1%  |                                                                              |==                                                                    |   2%  |                                                                              |==                                                                    |   4%  |                                                                              |===                                                                   |   5%  |                                                                              |====                                                                  |   6%  |                                                                              |=====                                                                 |   7%  |                                                                              |=======                                                               |   9%  |                                                                              |=======                                                               |  11%  |                                                                              |=========                                                             |  13%  |                                                                              |===========                                                           |  15%  |                                                                              |============                                                          |  17%  |                                                                              |============                                                          |  18%  |                                                                              |=============                                                         |  19%  |                                                                              |==============                                                        |  20%  |                                                                              |===============                                                       |  21%  |                                                                              |================                                                      |  22%  |                                                                              |================                                                      |  23%  |                                                                              |=================                                                     |  25%  |                                                                              |==================                                                    |  26%  |                                                                              |===================                                                   |  27%  |                                                                              |====================                                                  |  28%  |                                                                              |=====================                                                 |  29%  |                                                                              |=====================                                                 |  31%  |                                                                              |======================                                                |  32%  |                                                                              |=======================                                               |  33%  |                                                                              |========================                                              |  34%  |                                                                              |=========================                                             |  35%  |                                                                              |==========================                                            |  38%  |                                                                              |===========================                                           |  39%  |                                                                              |============================                                          |  40%  |                                                                              |=============================                                         |  41%  |                                                                              |==============================                                        |  42%  |                                                                              |==============================                                        |  44%  |                                                                              |===============================                                       |  45%  |                                                                              |================================                                      |  46%  |                                                                              |=================================                                     |  47%  |                                                                              |==================================                                    |  48%  |                                                                              |===================================                                   |  49%  |                                                                              |====================================                                  |  52%  |                                                                              |=====================================                                 |  53%  |                                                                              |======================================                                |  54%  |                                                                              |=======================================                               |  55%  |                                                                              |=========================================                             |  59%  |                                                                              |==========================================                            |  60%  |                                                                              |===========================================                           |  61%  |                                                                              |=============================================                         |  65%  |                                                                              |==============================================                        |  66%  |                                                                              |===============================================                       |  67%  |                                                                              |================================================                      |  68%  |                                                                              |=================================================                     |  70%  |                                                                              |==================================================                    |  71%  |                                                                              |==================================================                    |  72%  |                                                                              |===================================================                   |  73%  |                                                                              |====================================================                  |  74%  |                                                                              |=====================================================                 |  75%  |                                                                              |======================================================                |  77%  |                                                                              |======================================================                |  78%  |                                                                              |=======================================================               |  79%  |                                                                              |========================================================              |  80%  |                                                                              |=========================================================             |  81%  |                                                                              |==========================================================            |  82%  |                                                                              |===========================================================           |  84%  |                                                                              |===========================================================           |  85%  |                                                                              |============================================================          |  86%  |                                                                              |===============================================================       |  90%  |                                                                              |================================================================      |  91%  |                                                                              |===================================================================   |  95%  |                                                                              |====================================================================  |  98%  |                                                                              |===================================================================== |  99%  |                                                                              |======================================================================| 100%
    + +

    +

    Data formats - boundary data

    library(tidyverse)
    @@ -3322,10 +3328,14 @@ 

    the geom column is a MULTIPOLYGON — a geometry type representing complex shapes, which may consist of multiple polygons (e.g., islands or non-contiguous regions).

    +

    Federal Information Processing System (FIPS) Codes for States and Counties are numbers which uniquely identify geographic areas. See this codebook.

    +

    ggplot has spatial functions

    geom_polygon() works with boundary data

    +

    Let’s plot county outlines and major cities.

    +
    library(tidyverse) # `map_data()` from ggplot2
     library(maps) # `us.cities` data
     
    @@ -3358,6 +3368,8 @@ 

    usmap is compatible with ggplot

    +

    Let’s fill each county based on its population.

    +
    library(tidyverse)
     library(usmap) # `countypop` data and the `plot_usmap()` function
     
    @@ -3417,19 +3429,19 @@ 

    Use geom_sf() function with SF data.

    +

    Let’s fill each census tract by median household income.

    +
    library(tidyverse) # `geom_sf()` from ggplot2
     library(tidycensus) # `get_acs()` function for American Community Survey data
     
     wa_income <- get_acs(
       geography = "tract", 
    -  variables = "B19013_001", # Income key 
    +  variables = "B19013_001", # Median income code
       state = "WA", 
       year = 2022,
       geometry = TRUE
     )
    -
      |                                                                              |                                                                      |   0%  |                                                                              |=                                                                     |   1%  |                                                                              |==                                                                    |   2%  |                                                                              |==                                                                    |   4%  |                                                                              |===                                                                   |   5%  |                                                                              |====                                                                  |   6%  |                                                                              |=====                                                                 |   7%  |                                                                              |======                                                                |   8%  |                                                                              |=======                                                               |   9%  |                                                                              |=======                                                               |  11%  |                                                                              |========                                                              |  12%  |                                                                              |=========                                                             |  13%  |                                                                              |==========                                                            |  14%  |                                                                              |===========                                                           |  15%  |                                                                              |============                                                          |  17%  |                                                                              |============                                                          |  18%  |                                                                              |=============                                                         |  19%  |                                                                              |==============                                                        |  20%  |                                                                              |===============                                                       |  21%  |                                                                              |================                                                      |  22%  |                                                                              |================                                                      |  23%  |                                                                              |=================                                                     |  25%  |                                                                              |==================                                                    |  26%  |                                                                              |===================                                                   |  27%  |                                                                              |====================                                                  |  28%  |                                                                              |=====================                                                 |  29%  |                                                                              |=====================                                                 |  31%  |                                                                              |======================                                                |  32%  |                                                                              |=======================                                               |  33%  |                                                                              |========================                                              |  34%  |                                                                              |=========================                                             |  35%  |                                                                              |==========================                                            |  37%  |                                                                              |==========================                                            |  38%  |                                                                              |===========================                                           |  39%  |                                                                              |============================                                          |  40%  |                                                                              |=============================                                         |  41%  |                                                                              |==============================                                        |  42%  |                                                                              |==============================                                        |  44%  |                                                                              |===============================                                       |  45%  |                                                                              |================================                                      |  46%  |                                                                              |=================================                                     |  47%  |                                                                              |==================================                                    |  48%  |                                                                              |===================================                                   |  49%  |                                                                              |===================================                                   |  51%  |                                                                              |====================================                                  |  52%  |                                                                              |=====================================                                 |  53%  |                                                                              |======================================                                |  54%  |                                                                              |=======================================                               |  55%  |                                                                              |========================================                              |  56%  |                                                                              |========================================                              |  58%  |                                                                              |=========================================                             |  59%  |                                                                              |==========================================                            |  60%  |                                                                              |===========================================                           |  61%  |                                                                              |============================================                          |  62%  |                                                                              |=============================================                         |  64%  |                                                                              |=============================================                         |  65%  |                                                                              |===============================================                       |  67%  |                                                                              |================================================                      |  68%  |                                                                              |=================================================                     |  70%  |                                                                              |==================================================                    |  71%  |                                                                              |==================================================                    |  72%  |                                                                              |===================================================                   |  73%  |                                                                              |====================================================                  |  74%  |                                                                              |=====================================================                 |  75%  |                                                                              |======================================================                |  77%  |                                                                              |======================================================                |  78%  |                                                                              |=======================================================               |  79%  |                                                                              |========================================================              |  80%  |                                                                              |=========================================================             |  81%  |                                                                              |===========================================================           |  84%  |                                                                              |============================================================          |  86%  |                                                                              |=============================================================         |  87%  |                                                                              |=================================================================     |  93%  |                                                                              |==================================================================    |  94%  |                                                                              |===================================================================   |  95%  |                                                                              |====================================================================  |  97%  |                                                                              |====================================================================  |  98%  |                                                                              |===================================================================== |  99%  |                                                                              |======================================================================| 100%
    -

    tidycensus is helpful for tract level

    ggplot(data = wa_income, aes(fill = estimate)) + 
    diff --git a/resources/Mapping.pdf b/resources/Mapping.pdf
    index dd86fdb5..e8441a90 100644
    Binary files a/resources/Mapping.pdf and b/resources/Mapping.pdf differ