Area Planning Work with R

Eli Pousson

About

Package development

What packages am I using?

My data analysis and mapping work depends on a tall stack of open-source packages especially from the {tidyverse} along with the rOpenSci and rspatial communities.

I have a lot of favorite R packages across a few different categories:

  • package development
  • programming
  • visualization
  • data management

Packages I use for package development

  • {usethis}
  • {devtools}
  • {sinew}
  • {testthat}
  • {pkgdown}

Packages I use for programming

  • {sf}
  • {dplyr} (and {vctrs})
  • {rlang}
  • {cli}
  • {httr2}

Packages I use for visualization

  • mapboxapi
  • {ggplot2} (with Adobe Illustrator sometimes)
  • {mapview}
  • feltr* (with Felt)
  • {gt}

Packages I use for data management

  • {osmdata} (with OpenStreetMap)
  • {rairtable} development version* (with Airtable)
  • {googlesheets4} (with Google Sheets)
  • {officer} (with Microsoft Word)

When did I start building packages?

What types of packages am I working on?

  • local data packages
  • data access packages
  • analysis and visualization packages

How did I get to this current approach?

Data packages 👉 Utility packages 👉 Visualization packages

mapbaltimore

sfext

maplayer

maplayer

What are some local data projects am I working on?

A few packages:

  • {bcpss}: Tidy survey, program, and zone data from and about Baltimore City Public Schools.
  • {marylandedu}: Tidy enrollment and demographic data from Maryland State Department of Education.
  • {baltimorecensus}: Accessible U.S. Census geographies.
  • {baltimoremls}: Utility functions for Bright MLS sales data.

What are some local data projects am I working on?

A few data analyses (all WIP):

What am I hoping to try next?

I’m still learning new approaches all the time. Here are a few packages I’m excited to learn:

  • {targets}
  • {rdeck}
  • {arcgislayers}

Applications of R in Planning

John Ruhrah EMS INSPIRE Plan

All the little things

  • Maps
  • Tables
  • Graphics
  • Writing

Making the little things takes a lot of work

  • Data import and tidying
  • Data transformation and analysis
  • Managing text
  • Managing multiple output formats

Building demographic data tables with {gt} and {getACS}

  • What do you do in advance?
  • What do you do as needed?

Setup

# pak::pkg_install("elipousson/getACS")
library(getACS)
library(tidyverse)

First steps

  • Get data
  • Combine with metadata
  • Recalculate variables
  • Use areal interpolation as needed

Using getACS to get data, combine with metadata, and recalculate estimates as a share of the denominator:

delmarva_acs <- map(
  c("MD", "DC", "VA"),
  ~ get_acs_geographies(
  geography = c("county", "state"),
  table = "B08134",
  state = .x,
  quiet = TRUE
  )
)

dc_acs <- delmarva_acs[[2]]

dc_acs
# A tibble: 240 × 24
   GEOID NAME  variable column_id table_id estimate   moe perc_estimate perc_moe
   <chr> <chr> <chr>    <chr>     <chr>       <dbl> <dbl>         <dbl>    <dbl>
 1 11001 Dist… B08134_… B08134001 B08134     297447  3393          1        0.02
 2 11001 Dist… B08134_… B08134002 B08134      15708  1235          0.05     0   
 3 11001 Dist… B08134_… B08134003 B08134      22069  1093          0.07     0   
 4 11001 Dist… B08134_… B08134004 B08134      34554  1826          0.12     0.01
 5 11001 Dist… B08134_… B08134005 B08134      43447  1548          0.15     0   
 6 11001 Dist… B08134_… B08134006 B08134      22820  1166          0.08     0   
 7 11001 Dist… B08134_… B08134007 B08134      60341  2153          0.2      0.01
 8 11001 Dist… B08134_… B08134008 B08134      38187  1849          0.13     0.01
 9 11001 Dist… B08134_… B08134009 B08134      34521  1746          0.12     0.01
10 11001 Dist… B08134_… B08134010 B08134      25800  1395          0.09     0   
# ℹ 230 more rows
# ℹ 15 more variables: table_title <chr>, simple_table_title <chr>,
#   subject_area <chr>, universe <chr>, denominator_column_id <chr>,
#   topics <chr>, line_number <dbl>, column_title <chr>, indent <dbl>,
#   parent_column_id <chr>, denominator_estimate <dbl>, denominator_moe <dbl>,
#   denominator_column_title <chr>, geography <chr>, state <chr>

Next steps

  • Subset data to area
  • Recode values if needed
  • Create table or visualization
commute_acs_tbl <- dc_acs |> 
  filter_acs(
    indent == 1,
    line_number >= 11,
    geography = "state"
    ) |> 
  select_acs(.name_col = NULL) |> 
  gt_acs(
    column_title_label = "Mode of transportation",
    table = dc_acs$table_id
  )
commute_acs_tbl
Mode of transportation Est. % share
Car, truck, or van 131,329 ± 3,031 44% ± 1%
Public transportation (excluding taxicab) 100,595 ± 2,297 34% ± 1%
Walked 41,960 ± 1,475 14% ± 0%
Taxicab, motorcycle, bicycle, or other means 23,563 ± 1,485 8% ± 0%
Source: 2017-2021 ACS 5-year Estimates, Table B08134.

Map-making with {getdata}, {maplayer}, and {mapboxapi}

library(getdata)
library(maplayer)

Downloading data

dc_wards <- get_location(
  "https://maps2.dcgis.dc.gov/dcgis/rest/services/DCGIS_DATA/Demographic_WebMercator/MapServer/39"
)

dc_wards
Simple feature collection with 8 features and 97 fields
Geometry type: MULTIPOLYGON
Dimension:     XY
Bounding box:  xmin: -8584933 ymin: 4691871 xmax: -8561515 ymax: 4721078
Projected CRS: WGS 84 / Pseudo-Mercator
  OBJECTID GEOID NAMELSAD DP05_0001E DP05_0002E DP05_0003E DP05_0004E
1        1 11001   Ward 1      82404      39512      42892       92.1
2        2 11002   Ward 2      83925      40917      43008       95.1
3        3 11003   Ward 3      82450      37996      44454       85.5
4        4 11004   Ward 4      86085      41989      44096       95.2
5        5 11005   Ward 5      89082      42795      46287       92.5
6        6 11006   Ward 6      79161      38599      40562       95.2
7        7 11007   Ward 7      91997      44239      47758       92.6
8        8 11008   Ward 8      88050      39443      48607       81.1
  DP05_0005E DP05_0006E DP05_0007E DP05_0008E DP05_0009E DP05_0010E DP05_0011E
1       4653       3202       2731       3877       6330      26719      14746
2       2845        999        950       7065      11558      24359      11797
3       4085       4293       3281       5203       5328      14681      11903
4       5654       6329       5129       3368       3474      13220      14759
5       5783       4841       4250       4289       6126      18785      14205
6       4216       2487       2163       2300       4955      25640      14534
7       7591       6038       5528       5199       5616      16482      12459
8       8131       6880       7417       5340       5924      15828      12395
  DP05_0012E DP05_0013E DP05_0014E DP05_0015E DP05_0016E DP05_0017E DP05_0018E
1       7881       3901       2563       3869       1383        549       32.5
2       7047       3974       4032       5795       2779        725       32.3
3      10122       4192       3923       7980       5322       2137       38.4
4      11117       5448       4813       7706       3292       1776       39.0
5      10408       4311       4596       6420       3330       1738       35.2
6       8583       3599       2957       4688       2236        803       34.0
7      10006       5152       5697       7102       3576       1551       34.7
8       8437       4618       4638       5412       2050        980       31.0
  DP05_0019E DP05_0020E DP05_0021E DP05_0022E DP05_0023E DP05_0024E DP05_0025E
1      11854      71298      70550      66675       7139       5801      70550
2       5303      79049      78622      68722      11239       9299      78622
3      13305      70274      69145      64312      17904      15439      69145
4      19523      68236      66562      64886      15700      12774      66562
5      16637      73628      72445      68795      14527      11488      72445
6      10113      70025      69048      67373       9432       7727      69048
7      22400      71788      69597      67008      15478      12229      69597
8      25887      64107      62163      59442      11252       8442      62163
  DP05_0026E DP05_0027E DP05_0028E DP05_0029E DP05_0030E DP05_0031E DP05_0032E
1      33692      36858       91.4       5801       2401       3400       70.6
2      38682      39940       96.9       9299       4354       4945       88.0
3      31240      37905       82.4      15439       6818       8621       79.1
4      31677      34885       90.8      12774       5267       7507       70.2
5      34620      37825       91.5      11488       4272       7216       59.2
6      33185      35863       92.5       7727       3295       4432       74.3
7      32312      37285       86.7      12229       4834       7395       65.4
8      26809      35354       75.8       8442       3277       5165       63.4
  DP05_0033E DP05_0034E DP05_0035E DP05_0036E DP05_0037E DP05_0038E DP05_0039E
1      82404      76164       6240      76164      41431      21213        400
2      83925      78218       5707      78218      58021       9417        432
3      82450      77017       5433      77017      62358       7184         92
4      86085      80071       6014      80071      25169      41998        243
5      89082      84742       4340      84742      22839      54807        255
6      79161      74657       4504      74657      51212      17663         95
7      91997      88437       3560      88437       7192      78810        316
8      88050      84991       3059      84991       8151      74017        151
  DP05_0040E DP05_0041E DP05_0042E DP05_0043E DP05_0044E DP05_0045E DP05_0046E
1          0          0          0          0       4670       1405        617
2          0          0          0          0       7764       2087       2840
3          0          0          0          0       5935       2148       1140
4          0          0          0          0       1536        516        223
5          0          0          0          0       2614        592        521
6          0          0          0          0       4075        534       1700
7          0          0          0          0        648         92        104
8          0          0          0          0        746        127        128
  DP05_0047E DP05_0048E DP05_0049E DP05_0050E DP05_0051E DP05_0052E DP05_0053E
1        422        139        610        825        652         52         34
2        318        337        989        350        843         91          8
3        710        205        229        598        905         60         37
4        147         47        146        202        255          0          0
5        527         29        343        305        297         99         95
6        381        174        684        272        330         26          0
7         34         11        272         74         61         19          0
8         68          0        275         23        125         12          1
  DP05_0054E DP05_0055E DP05_0056E DP05_0057E DP05_0058E DP05_0059E DP05_0060E
1          0          0         18       8398       6240        809        219
2         29          0         54       2493       5707        690        246
3         13          0         10       1388       5433        980        131
4          0          0          0      11125       6014       1165        179
5          4          0          0       4128       4340       1459        162
6          0         12         14       1586       4504        987        214
7          0          0         19       1452       3560       1006        104
8          8          0          3       1914       3059       1143        149
  DP05_0061E DP05_0062E DP05_0063E DP05_0064E DP05_0065E DP05_0066E DP05_0067E
1       1267        417      82404      46690      23121       1418       6168
2       1382         13      83925      63164      10758        969       9478
3       1517         68      82450      67494       8692        538       7741
4        447        340      86085      29685      45158       1609       2780
5        624        123      89082      26324      57527        942       3810
6       1192        112      79161      55272      19351        572       5773
7        279        832      91997       9470      81375       1596       1104
8        166        530      88050      10526      76253       1068       1132
  DP05_0068E DP05_0069E DP05_0070E DP05_0071E DP05_0072E DP05_0073E DP05_0074E
1         98      11575      82404      17017       2838       1082        462
2        222       5619      83925      11251       2220        958        665
3         83       3937      82450       8216       1288        788        693
4        237      14080      86085      18628       1860        651        441
5        318       5326      89082       8265       1876        666        183
6         93       3013      79161       6332       1281        730        369
7         55       2347      91997       3802        728        524        361
8         99       2663      88050       3471       1011        804          8
  DP05_0075E DP05_0076E DP05_0077E DP05_0078E DP05_0079E DP05_0080E DP05_0081E
1      12635      65387      36338      20384        184       4598         52
2       7408      72674      52548       9193         85       7598         91
3       5447      74234      58064       6853         26       5841         36
4      15676      67457      21216      41098         94       1536          0
5       5540      80817      20645      54053        190       2579         95
6       3952      72829      47824      17539         43       3999         14
7       2189      88195       6543      77487        268        639          0
8       1648      84579       7218      73109        113        732          9
  DP05_0082E DP05_0083E DP05_0084E DP05_0085E DP05_0086E DP05_0087E DP05_0088E
1        580       3251        480       2771      42860      61241      28734
2        174       2985        973       2012      50324      69707      34477
3        411       3003        347       2656      42476      62064      28229
4        463       3050        441       2609      36284      57069      26590
5        339       2916        199       2717      42066      67939      32150
6        260       3150        497       2653      46550      65343      31454
7        462       2796        180       2616      41318      67919      31367
8        917       2481         38       2443      42428      61055      26163
  DP05_0089E SHAPE.AREA SHAPE.LEN                    GIS_ID
1      32507          0         0 ACS_Demographic_DC_Ward_1
2      35230          0         0 ACS_Demographic_DC_Ward_2
3      33835          0         0 ACS_Demographic_DC_Ward_3
4      30479          0         0 ACS_Demographic_DC_Ward_4
5      35789          0         0 ACS_Demographic_DC_Ward_5
6      33889          0         0 ACS_Demographic_DC_Ward_6
7      36552          0         0 ACS_Demographic_DC_Ward_7
8      34892          0         0 ACS_Demographic_DC_Ward_8
                              GEONAME                               GLOBALID
1 Ward 1 (2022), District of Columbia {A8BDB2A5-BC64-4797-86A3-59D07CB339F9}
2 Ward 2 (2022), District of Columbia {B76AA6F1-C20D-407A-A7B9-FCEF33CAE4BD}
3 Ward 3 (2022), District of Columbia {70C2C87D-3384-4710-9566-8BBF1430957E}
4 Ward 4 (2022), District of Columbia {EAEC242D-526F-4B40-9D0D-50AAFED0B154}
5 Ward 5 (2022), District of Columbia {5A3FBA65-7076-40B5-A9F6-577FDA6434F0}
6 Ward 6 (2022), District of Columbia {E785FE32-1537-4180-B638-E843085072B2}
7 Ward 7 (2022), District of Columbia {C226EFA3-A1DD-47F9-B4AB-622E06A5C87D}
8 Ward 8 (2022), District of Columbia {DED98CA0-3002-47B4-B538-D5197018ED3F}
                           geoms
1 MULTIPOLYGON (((-8577254 47...
2 MULTIPOLYGON (((-8580711 47...
3 MULTIPOLYGON (((-8584932 47...
4 MULTIPOLYGON (((-8579577 47...
5 MULTIPOLYGON (((-8573742 47...
6 MULTIPOLYGON (((-8576466 47...
7 MULTIPOLYGON (((-8569782 47...
8 MULTIPOLYGON (((-8576945 46...

Subsetting data

ward_5 <- get_location(
  dc_wards,
  name = "Ward 5",
  name_col = "NAMELSAD"
)

ward_5
Simple feature collection with 1 feature and 97 fields
Geometry type: MULTIPOLYGON
Dimension:     XY
Bounding box:  xmin: -8573742 ymin: 4706949 xmax: -8565136 ymax: 4715554
Projected CRS: WGS 84 / Pseudo-Mercator
  OBJECTID GEOID NAMELSAD DP05_0001E DP05_0002E DP05_0003E DP05_0004E
5        5 11005   Ward 5      89082      42795      46287       92.5
  DP05_0005E DP05_0006E DP05_0007E DP05_0008E DP05_0009E DP05_0010E DP05_0011E
5       5783       4841       4250       4289       6126      18785      14205
  DP05_0012E DP05_0013E DP05_0014E DP05_0015E DP05_0016E DP05_0017E DP05_0018E
5      10408       4311       4596       6420       3330       1738       35.2
  DP05_0019E DP05_0020E DP05_0021E DP05_0022E DP05_0023E DP05_0024E DP05_0025E
5      16637      73628      72445      68795      14527      11488      72445
  DP05_0026E DP05_0027E DP05_0028E DP05_0029E DP05_0030E DP05_0031E DP05_0032E
5      34620      37825       91.5      11488       4272       7216       59.2
  DP05_0033E DP05_0034E DP05_0035E DP05_0036E DP05_0037E DP05_0038E DP05_0039E
5      89082      84742       4340      84742      22839      54807        255
  DP05_0040E DP05_0041E DP05_0042E DP05_0043E DP05_0044E DP05_0045E DP05_0046E
5          0          0          0          0       2614        592        521
  DP05_0047E DP05_0048E DP05_0049E DP05_0050E DP05_0051E DP05_0052E DP05_0053E
5        527         29        343        305        297         99         95
  DP05_0054E DP05_0055E DP05_0056E DP05_0057E DP05_0058E DP05_0059E DP05_0060E
5          4          0          0       4128       4340       1459        162
  DP05_0061E DP05_0062E DP05_0063E DP05_0064E DP05_0065E DP05_0066E DP05_0067E
5        624        123      89082      26324      57527        942       3810
  DP05_0068E DP05_0069E DP05_0070E DP05_0071E DP05_0072E DP05_0073E DP05_0074E
5        318       5326      89082       8265       1876        666        183
  DP05_0075E DP05_0076E DP05_0077E DP05_0078E DP05_0079E DP05_0080E DP05_0081E
5       5540      80817      20645      54053        190       2579         95
  DP05_0082E DP05_0083E DP05_0084E DP05_0085E DP05_0086E DP05_0087E DP05_0088E
5        339       2916        199       2717      42066      67939      32150
  DP05_0089E SHAPE.AREA SHAPE.LEN                    GIS_ID
5      35789          0         0 ACS_Demographic_DC_Ward_5
                              GEONAME                               GLOBALID
5 Ward 5 (2022), District of Columbia {5A3FBA65-7076-40B5-A9F6-577FDA6434F0}
                           geoms
5 MULTIPOLYGON (((-8573742 47...

Make a basemap

make_mapbox_map(
  dc_wards,
  style_url = "mapbox://styles/mapbox/navigation-day-v1",
  asp = 1,
  expand = FALSE,
  color = NA
)

Make a basemap

ward_5_map <- make_mapbox_map(
  ward_5,
  style_url = "mapbox://styles/mapbox/navigation-day-v1",
  asp = 1,
  expand = FALSE,
  color = NA
)

ward_5_map

Add an inset map

layer_inset(
  map = ward_5_map,
  inset = layer_location_context(
    context = dc_wards,
    location = ward_5,
    basemap = TRUE
  )
)

Get more area data

ward_5_schools <- get_osm_data(
    ward_5,
    key = "building",
    value = "school",
    geometry = "polygon"
    )

Add icon markers to a map

ward_5_map +
  layer_icon(
    ward_5_schools,
    icon = "school",
    source = "openstreetmap/map-icons",
    size = 3
  )