Skip to content

Commit 801ab54

Browse files
committed
distilling
1 parent aeb4207 commit 801ab54

File tree

211 files changed

+20473
-101889
lines changed

Some content is hidden

Large Commits have some content hidden by default. Use the searchbox below for content that may be hidden.

211 files changed

+20473
-101889
lines changed

01_georeference.Rmd

Lines changed: 3 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -1,20 +1,14 @@
11
---
22
title: Latitude and Longitude Coordinates
3-
output: html_document
4-
# html_document:
5-
# toc: FALSE
63
---
7-
 
8-
9-
To introduce the [`mapview` package](https://r-spatial.github.io/mapview/index.html), make an interactive map with Starbucks coffee shop locations in North Carolina (2012). (Adapted from Machlis.)^[[5 Visualizations in 5 Minutes](http://www.computerworld.com/article/2893271/business-intelligence/5-data-visualizations-in-5-minutes-each-in-5-lines-or-less-of-r.html). ComputerWorld.com by Sharon Machlis]
4+
Use the [`mapview` package](https://r-spatial.github.io/mapview/index.html) to make interactive maps. In this example, georeference Starbucks coffee shop locations in North Carolina (2012). (Adapted from Machlis.)^[[5 Visualizations in 5 Minutes](http://www.computerworld.com/article/2893271/business-intelligence/5-data-visualizations-in-5-minutes-each-in-5-lines-or-less-of-r.html). ComputerWorld.com by Sharon Machlis]
105

116

127
## Load Libraries
138

149
```{r libraries, message=FALSE, warning=FALSE}
1510
library(tidyverse)
1611
library(sf)
17-
#library(leaflet)
1812
library(mapview)
1913
```
2014

@@ -31,7 +25,8 @@ starbucks <- read_csv("https://raw.githubusercontent.com/libjohn/mapping-with-R/
3125
starbucksNC <- starbucks %>%
3226
filter(State == "NC")
3327
34-
starbucksNC
28+
starbucksNC %>%
29+
glimpse()
3530
```
3631

3732
## Make the Map
@@ -61,12 +56,7 @@ mapview(sbux_sf)
6156

6257

6358

64-
## Save the Map
65-
66-
You can save a map as an interactive HTML page or a static `.png, .pdf, or .jpeg` file with the [`mapshot` function](https://r-spatial.github.io/mapview/reference/mapshot.html). For example:
6759

68-
- `mapshot(starNCmap, file = "map.png")`
69-
- `mapshot(starNCmap, url = "map.html")`
7060

7161

7262

02_choropleth.Rmd

Lines changed: 9 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -1,37 +1,36 @@
11
---
22
title: "tidycensus"
3-
output: html_document
43
---
54

6-
The [tidycensus](https://walkerke.github.io/tidycensus/index.html) package, developed by [Kyle Walker](https://walkerke.github.io/), is very **convenient and easy to use package for making choropleth maps** from United States Department of **Census data**, specifically from the Decennial and ACS Census reports. This package makes it possible to gather census variables and conveniently join those variables with "Census Geography" (i.e. aka "shapefiles", or polygons.) Visualization, or plotting, maps can be done with separate packages.
5+
The [tidycensus](https://walkerke.github.io/tidycensus/index.html) package, developed by [Kyle Walker](https://walkerke.github.io/), is very **convenient and easy to use package for making choropleth maps** from United States Department of **Census data**. Tidycensus uses the Decennial or ACS Census reports. This package makes it possible to gather census variables and conveniently join those variables with "Census Geography" (i.e. aka "shapefiles", or polygons.) Visualization can be done with separate packages such as `mapview`, `leaflet`, or `ggplot2::geom_sf()`.
76

87

98
```{r libraries, message=FALSE, warning=FALSE}
109
library(tidyverse)
1110
library(sf)
1211
library(tidycensus)
13-
#library(leaflet)
1412
library(mapview)
1513
```
1614

1715
## Census API Key
1816

19-
Use the tidycensus package to gather Census data and join that data with Census geography (i.e. geometry, i.e. shapefiles, i.e. polygons). First, you will need to get a [free Census API key](https://api.census.gov/data/key_signup.html). Kyle Walker's [*Basic usage of tidycensus*](https://walkerke.github.io/tidycensus/articles/basic-usage.html) documents this process.
17+
you need a [free Census API key](https://api.census.gov/data/key_signup.html). Kyle Walker's [*Basic usage of tidycensus*](https://walkerke.github.io/tidycensus/articles/basic-usage.html) documents this process.
2018

2119
```{}
2220
census_api_key("YOUR API KEY GOES HERE")
2321
```
2422

2523
### .Renviron File
2624

27-
See also Kyle's [more detailed documentation](https://walkerke.github.io/tidycensus/reference/census_api_key.html) for putting the key into your environment file. But skip that for the moment.
25+
See also Kyle's [more detailed documentation](https://walkerke.github.io/tidycensus/reference/census_api_key.html) for caching the API key in your R environment.
2826

2927

3028
## TidyCensus -- Get Data
3129

32-
Create a Simple Features dataframe using `tidycensus::get_acs()`
30+
Create a Simple Features (i.e. `sf`) dataframe using `tidycensus::get_acs()`
3331

3432
The Census population variable we'll use is "B01003_001". More information about identifying Census variables is available at the [bottom of this page](#variables).
33+
3534
```{r load-data, message=FALSE, warning=FALSE, include=FALSE}
3635
nc_pop <-
3736
get_acs(geography = "county",
@@ -41,6 +40,7 @@ nc_pop <-
4140
4241
#nc_pop
4342
```
43+
4444
```{}
4545
nc_pop <-
4646
get_acs(geography = "county",
@@ -73,7 +73,8 @@ starbucks <- read_csv("data/All_Starbucks_Locations_in_the_US_-_Map.csv")
7373
```
7474

7575

76-
Subset Starbucks Data to North Carolina
76+
Subset Starbucks Data to North Carolina
77+
7778
```{r filter-dataset}
7879
starbucksNC <- starbucks %>%
7980
filter(State == "NC")
@@ -151,7 +152,7 @@ The Census is a very large collection of data. Many casual users of Census data
151152

152153
## Shapefiles
153154

154-
In the [*tmap* section](031_thematic_mapping.html) of the [Simple Features module](030_thematic_sf.html) we will use the `tigris` package to gather State level, Census geography shapefiles. Shapefiles are an important GIS data standard. There are many other standards, although shapefiles have a very broad user base. If you need shapefiles for other geographies, please consult our [*GIS Data: Starting Points* guide](https://guides.library.duke.edu/gisdata) -- a very handy collection of GIS data sources, much of which will be in the shapefiles format.
155+
Shapefiles are an important GIS data standard used frequently in thematic mapping. There are many other standards, although shapefiles have a very broad user base. If you need shapefiles for other geographies, please consult the [*guide to geospatial applications using the R programming language*](https://guides.library.duke.edu/r-geospatial)
155156

156157
## End Notes
157158

031_thematic_mapping.Rmd

Lines changed: 5 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,5 @@
11
---
22
title: "Thematic Mapping with tmap"
3-
output: html_document
43
---
54

65

@@ -19,11 +18,11 @@ library(sf)
1918
Using the `tigris` package, get Census Tiger shapefiles for census geographies. Tigris will return the shapefile in the `sf`, or simple features, format.
2019

2120
```{}
22-
us_geo <- tigris::states(class = "sf")
21+
us_geo <- tigris::states(class = "sf")
2322
```
2423

2524
```{r get-data, message=FALSE, warning=FALSE, include=FALSE, paged.print=FALSE}
26-
us_geo <- tigris::states(class = "sf")
25+
us_geo <- tigris::states(class = "sf")
2726
```
2827

2928
## Data Structure
@@ -128,14 +127,12 @@ qtm(HelperShapeObject, fill = "wages")
128127

129128
## Contiguous 48 states
130129

131-
Filter to only the contiguous 48 states + D.C.
130+
Filter to only the contiguous 50 states + D.C. Note that `tigris::shift_geometry()` will shift and resize Alaska and Hawaii.
132131

133132
```{r subset-data}
134133
contiguous_states <- HelperShapeObject %>%
135-
filter(REGION != 9) %>%
136-
filter(STUSPS != "AK") %>%
137-
filter(STUSPS != "HI")
138-
134+
filter(REGION != 9) %>%
135+
shift_geometry()
139136
```
140137

141138

032_thematic_mapping_geom_sf.Rmd

Lines changed: 13 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,5 @@
11
---
22
title: "Thematic Mapping with geom_sf"
3-
output: html_document
43
---
54

65

@@ -16,20 +15,19 @@ library(ggplot2)
1615

1716
## Shapefiles as sf
1817

19-
Repeating steps from the [previous section](031_thematic_mapping.html), using the `tigris` package, get Census Tiger shapefiles for census geographies. Coastal boundaries can be gathered with the tigris argument: `cb = TRUE`.
18+
Use the `tigris` package to get Census Tiger shapefiles for census geographies. Coastal boundaries can be gathered with the tigris argument: `cb = TRUE`.
2019

2120
```{r getuscb, message=FALSE, warning=FALSE, include=FALSE}
22-
us_geo <- tigris::states(class = "sf", cb = TRUE)
21+
us_geo <- tigris::states(class = "sf", cb = TRUE) %>%
22+
shift_geometry()
2323
```
2424
``` r
2525
us_geo <- tigris::states(class = "sf", cb = TRUE)
2626
```
2727

2828
## Get BLS data
2929

30-
As mentioned before, the data are from the Bureau of Labor Statistics. These data are stored in an excel file in the `data` directory of the [repository](https://github.com/libjohn/mapping-with-R): `data/OES_Report.xlsx`.
31-
32-
Again, from the previous section you will see how these data were gathered, loaded, transformed, and joined.
30+
In additon to shapefiles, the wage data used in this example comes from from the Bureau of Labor Statistics. These data are stored in an excel file in the `data` directory of the [repository](https://github.com/libjohn/mapping-with-R): `data/OES_Report.xlsx`.
3331

3432
```{r getblsdata, message=FALSE, warning=FALSE}
3533
Salary4Helpers <-
@@ -42,7 +40,7 @@ Salary4Helpers
4240

4341
## Wrangle the data
4442

45-
As before...
43+
Using the [`stringr` package](https://stringr.tidyverse.org) we can extract the text of the state names from the state code by leveraging regular expressions (i.e. regex) pattern matching techniques with stringr::str_extract().
4644

4745
```{r wrangleblsdata, message=FALSE, warning=FALSE}
4846
BlsWage_ToJoin <- Salary4Helpers %>%
@@ -64,26 +62,26 @@ HelperShapeObject <- us_geo %>%
6462
```
6563

6664

67-
## Contiguous 48 states
65+
## 50 states
66+
67+
Filter to only the 50 US states + D.C.
6868

69-
Filter to only the contiguous 48 states + D.C.
69+
Alaska and Hawaii were shifted and rescaled using `tigris::shift_geometry()`, above.
7070

7171
```{r filterlower48, message=FALSE, warning=FALSE}
7272
contiguous_states <- HelperShapeObject %>%
73-
filter(GEOID < 60) %>%
74-
filter(STUSPS != "AK") %>%
75-
filter(STUSPS != "HI")
73+
filter(GEOID < 60)
7674
```
7775

7876

7977

8078
## ggplot2 with geom_sf and viridis
8179

82-
In this section we introduce making shapefiles with ggplot2. ggplot2 is one of the more popular and broadly distributed graphics packages used in the R community. ([Learn more](https://rfun.library.duke.edu/#portfolio) about ggplot2.
80+
ggplot2 is one of the more popular and broadly distributed graphics packages used in the R community. ([Learn more](https://rfun.library.duke.edu/#portfolio) about ggplot2.
8381

84-
In this plot I reversed the direction of the color scale. After consulting with my visualization colleagues it seems this may have been a non-standard action on my part. But, I leave the `direction` argument here for the curious.
82+
In this plot I **reversed** the direction of the color scale. My visualization colleagues alerted me that this is non-standard approach to visualization, but I kept the argument in this example to demonstrate the `direction` argument.
8583

86-
Use a pleasing projection, in this case assigned the crs projection to '5070' (`coord_sf(crs = 5070)`), and removed the gridlines (i.e [graticules](https://en.wikipedia.org/wiki/Graticule)) `coords_sf(datum = NA)`
84+
Use a pleasing [projection](https://guides.library.duke.edu/r-geospatial/CRS), in this case assigned the crs projection to '5070' (`coord_sf(crs = 5070)`), and removed the gridlines (i.e [graticules](https://en.wikipedia.org/wiki/Graticule)) `coords_sf(datum = NA)`
8785

8886
```{r with-ggplot-geom_sf, message=FALSE, warning=FALSE}
8987
contiguous_states %>%

038_facets_wrap_thematic_mapping.Rmd

Lines changed: 6 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,5 @@
11
---
22
title: "Faceted Thematic Mapping"
3-
output: html_document
43
---
54

65
```{r load-libraries, message=FALSE, warning=FALSE}
@@ -56,9 +55,10 @@ my_df <- my_xl_files %>%
5655
state_names <- us_geo %>%
5756
filter(REGION != "9") %>%
5857
select(NAME) %>%
59-
st_drop_geometry()
58+
st_drop_geometry()
6059
```
6160

61+
6262
### Wrangle the data
6363

6464
Before we join the BLS data to the shapefile `us_geo` we need to transform BLS data
@@ -69,7 +69,8 @@ my_df <- my_df %>%
6969
wages = "Annual mean wage(2)",
7070
type = sheet) %>%
7171
mutate(State = str_extract(area, '.*(?=\\()')) %>%
72-
mutate(type = str_extract(type, "(?<=data/OES_)\\w+"))
72+
mutate(type = str_extract(type, "(?<=data/OES_)\\w+")) %>%
73+
filter(State != "Guam")
7374
```
7475

7576
### Missing data
@@ -114,7 +115,7 @@ my_df <- my_df %>%
114115
### identify and pick census variables
115116

116117
B01003_001E = Total Population
117-
B06011_001E = Median income in the past 12 months --!!Total:
118+
B06011_001E = Median income in the past 12 months
118119

119120
```{r identify_ACS_variables}
120121
variables_census <- load_variables(2015, "acs5", cache = TRUE)
@@ -165,7 +166,7 @@ my_df <- my_df %>%
165166
### Display Map
166167

167168
```{r}
168-
my_df %>%
169+
my_df %>%
169170
ggplot(aes(fill = wages, color = wages)) +
170171
geom_sf() +
171172
coord_sf(crs = 5070, datum = NA) +

039_facet_example.Rmd

Lines changed: 0 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,8 +1,5 @@
11
---
22
title: "Example Faceted Thematic Mapping"
3-
output:
4-
html_document:
5-
toc: FALSE
63
---
74

85
![](facet_map.png)
Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,5 @@
11
---
22
title: "Thematic Mapping with Simple Features"
3-
output: html_document
43
---
54
&nbsp;
65

_CopyOfindex.Rmd.bak

Lines changed: 61 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,61 @@
1+
---
2+
output:
3+
html_document:
4+
toc: FALSE
5+
---
6+
&nbsp;
7+
8+
[**Slides**](/slides/)
9+
10+
[Guide to geospatial applications using the R programming language](https://guides.library.duke.edu/r-geospatial)
11+
12+
[Code for Hands-on Workshop -- Spring 2019](https://github.com/libjohn/map-spring2019)
13+
14+
## Overview
15+
16+
These learning and quick-reference resource pages support the face-to-face workshop, [Mapping with R](https://rfun.library.duke.edu/portfolio/mapping_workshop/), offered via the [Data & Visualization Services](https://library.duke.edu/data/) Department, Duke University Libraries. The exercises and workshop are designed to help you learn georeferencing, make a [choropleth](https://en.wikipedia.org/wiki/Choropleth_map) with USA census data via the [tidycensus](https://walkerke.github.io/tidycensus/) package, and briefly introducing the [sf](http://r-spatial.github.io/sf/) package, simple features.
17+
18+
<div class = "picture1">
19+
<iframe height="315" width="500" src="https://warpwire.duke.edu/w/JZ8EAA/" frameborder="0" scrolling="0" allow="autoplay; encrypted-media; fullscreen; picture-in-picture;" allowfullscreen></iframe>
20+
21+
</div>
22+
23+
During the Workshop we introduce
24+
25+
- Georeferencing latitude and longitude
26+
- Choropleths
27+
- Polygons (shapes)
28+
- Census Geography
29+
- Briefly introduce ACS and Decennial census
30+
31+
- Census API keys
32+
33+
- Thematic mapping using _Simple Features for R_
34+
35+
## Workshop Resource Page
36+
37+
1. Georeferencing, Plotting XY latitude & longitude: with [interactive `mapview` maps](01_georeference.html) and saving static maps.
38+
39+
1. [Choropleths with tidycensus](02_choropleth.html)
40+
41+
1. [Thematic Mapping](030_thematic_sf.html) with `ggplot2`, `geom_sf`, `tigris`, and `viridis`. Additionally we show faceting mapping, and saving map images and files. We also introduce `tmap` a full featured thematic mapping package.
42+
43+
## Hands-on Exercises
44+
45+
- [Exercises](exercises.html)
46+
47+
48+
## Supporting Materials
49+
50+
- [**Slides**](slides)
51+
52+
- [Software / Package Prequisites](software_prereqs.html)
53+
54+
- [Next Steps](next.html): Moving toward GIS analysis, raster analytics, etc.
55+
56+
57+
&nbsp;
58+
59+
<figure>![](https://imgs.xkcd.com/comics/2016_election_map.png)<figcaption>KXCD. 2016 Election Map: https://xkcd.com/1939/</figcaption></figure><br>
60+
61+

0 commit comments

Comments
 (0)