Skip to content

Commit 1fea7a6

Browse files
committed
update finalizing
1 parent 82a9aa4 commit 1fea7a6

36 files changed

+263
-2873
lines changed

02_choropleth.Rmd

Lines changed: 25 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -8,9 +8,8 @@ The [tidycensus](https://walkerke.github.io/tidycensus/index.html) package, deve
88

99
```{r libraries, message=FALSE, warning=FALSE}
1010
library(tidyverse)
11-
library(stringr)
12-
library(tidycensus)
1311
library(sf)
12+
library(tidycensus)
1413
library(leaflet)
1514
library(mapview)
1615
```
@@ -108,6 +107,30 @@ addLogo(mymap, "images/Rfun3.png",
108107
```
109108

110109

110+
## Alaska & Hawaii - Shift
111+
112+
Shift and re-scale Alaska and Hawaii for better cartographic display of the entire US.
113+
114+
```{r shiftgeoakhi, message=FALSE, warning=FALSE}
115+
116+
population <- get_acs(geography = "state",
117+
variables = "B01003_001",
118+
geometry = TRUE,
119+
shift_geo = TRUE)
120+
121+
122+
```
123+
124+
125+
```{r buildshift, message=FALSE, warning=FALSE}
126+
mapviewOptions(legend.pos = "bottomright")
127+
mapviewOptions(leafletWidth = 800)
128+
#mapviewOptions()
129+
#mapviewOptions(default = TRUE)
130+
mapview(poopulation, zcol = "estimate", native.crs = TRUE, crs = 5070)
131+
```
132+
133+
111134

112135
## Census
113136

030_thematic_sf.Rmd

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -6,12 +6,12 @@ output: html_document
66

77
## Sections
88

9-
1. [Thematic Mapping with tmap](031_thematic_mapping.html): creating choropleth with the `tmap` package
10-
119
1. Thematic Mapping with [sf & ggplot::geom_sf](032_thematic_mapping_geom_sf.html): creating choropleth with `ggplot2`, `sf`, and `viridis` packages
1210

1311
1. [Faceted Thematic Maps](039_facet_example.html) with **ggplot2::geom_sf**
1412

13+
1. [Thematic Mapping with tmap](031_thematic_mapping.html): creating choropleth with the `tmap` package
14+
1515
1. [Saving ouput files](01_georeference.html#save_the_map).
1616

1717
## Simple Features
@@ -24,7 +24,7 @@ The initial modules of *Mapping in R* introduce plotting latitude and longitude
2424
2525
In these *Thematic Mapping with Simple Features* sections, we introduce the `tmap` package first. Through the the `tmap` package you will learn an easy-to-use package for building spatial distributions on geographic maps. These maps are typically `.png` image files. (.png files are easy to produce and share.) In this section we'll also use the `tigris` package -- to gather Census shapefiles -- and store those shape objects in the Simple Features data structure. (In the [*Chropleth* module](02_choropleth.html) we used `tidycensus` to gather census Geography shape objects, as well as to gather census data variables.) `tigris` simply gathers the shapefile polygons. Later we will join the polygon object to a data frame using `tmaptools`.
2626

27-
Next, we'll use the `ggplot::geom_sf` function to create our mapping visualiations using the widely adopted ggplot2 syntax.
27+
Next, we'll use the `ggplot::geom_sf` function to create our mapping visualizations using the widely adopted ggplot2 syntax.
2828

2929
Later, in the *interactive thematic mapping* section, you will make a different interactive choropleth using `leaflet`. This section is largely a repeat of the earlier choropleth module. The level of interactivity is sparse, but functional. Interactivity demonstrates how you can generate maps which move beyond traditional 2D map-on-a-page images. Just know that this zoom-in/zoom-out interactivity feature is possible because we use `leaflet` to draw the map. By extension, you can add many interactive features, e.g. pop-up windows. This section is a good set-up for the last section which covers how to save maps as files.
3030

032_thematic_mapping_geom_sf.Rmd

Lines changed: 27 additions & 30 deletions
Original file line numberDiff line numberDiff line change
@@ -16,10 +16,13 @@ library(ggplot2)
1616

1717
## Shapefiles as sf
1818

19-
Repeating steps from the [previous section](031_thematic_mapping.html), using the `tigris` package, get Census Tiger shapefiles for census geographies.
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`.
2020

21-
```{}
22-
us_geo <- tigris::states(class = "sf")
21+
```{r getuscb, message=FALSE, warning=FALSE, include=FALSE}
22+
us_geo <- tigris::states(class = "sf", cb = TRUE)
23+
```
24+
``` r
25+
us_geo <- tigris::states(class = "sf", cb = TRUE)
2326
```
2427

2528
## Get BLS data
@@ -28,7 +31,7 @@ As mentioned before, the data are from the Bureau of Labor Statistics. These da
2831

2932
Again, from the previous section you will see how these data were gathered, loaded, transformed, and joined.
3033

31-
```{}
34+
```{r getblsdata, message=FALSE, warning=FALSE}
3235
Salary4Helpers <-
3336
read_excel("data/OES_Report.xlsx",
3437
col_types = c("text", "numeric"),
@@ -41,21 +44,20 @@ Salary4Helpers
4144

4245
As before...
4346

44-
```{}
47+
```{r wrangleblsdata, message=FALSE, warning=FALSE}
4548
BlsWage_ToJoin <- Salary4Helpers %>%
46-
rename(Area = "Area Name") %>%
4749
rename(wages = "Annual mean wage(2)") %>%
48-
mutate(State = gsub("\\(\\d{7}\\)", "", Area)) %>%
49-
filter(wages != "NA_character_") %>%
50+
mutate(State = str_extract(`Area Name`, "\\w+.*(?=\\()")) %>%
51+
drop_na(wages) %>%
5052
select(State, wages)
5153
```
5254

5355

54-
## Append data
56+
## Join data
5557

56-
As before, using the `append_data()` function of the `tmaptools` package, append BLS data to the previously loaded shape object
58+
Use the `dplyr::left_join` function to append BLS variable to the `sf` tibble (data frame).
5759

58-
``` r
60+
```{r leftjoin_usgeo, warning=FALSE}
5961
HelperShapeObject <- us_geo %>%
6062
left_join(BlsWage_ToJoin,
6163
by = c("NAME" = "State"))
@@ -64,41 +66,36 @@ HelperShapeObject <- us_geo %>%
6466

6567
## Contiguous 48 states
6668

67-
As before, filter to only the contiguous 48 states + D.C.
69+
Filter to only the contiguous 48 states + D.C.
6870

69-
```{}
71+
```{r filterlower48, message=FALSE, warning=FALSE}
7072
contiguous_states <- HelperShapeObject %>%
71-
filter(REGION != 9) %>%
73+
filter(GEOID < 60) %>%
7274
filter(STUSPS != "AK") %>%
7375
filter(STUSPS != "HI")
74-
7576
```
7677

7778

7879

79-
## Projection
80-
81-
Using the USA_Contiguous_Albers_Equal_Area_Conic_USGS_version projection for the continental US. `EPSG:5070`
82-
83-
```{}
84-
contiguous_states %>%
85-
st_transform(5070) %>%
86-
tm_shape() +
87-
tm_polygons("wages", id = "Name")
88-
```
80+
## ggplot2 with geom_sf and viridis
8981

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.
9083

91-
## ggplot2 with geom_sf and viridis
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.
9285

93-
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. I also 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. Finally, please note the easiest way to remove the [graticules](https://en.wikipedia.org/wiki/Graticule) is as follows: `coords_sf(datum = NA)`
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)`
9487

95-
```{r with-ggplot-geom_sf}
88+
```{r with-ggplot-geom_sf, message=FALSE, warning=FALSE}
9689
contiguous_states %>%
9790
ggplot(aes(fill = wages, color = wages)) +
9891
geom_sf() +
9992
coord_sf(crs = 5070, datum = NA) +
100-
scale_fill_viridis(option = "viridis", direction = -1) +
101-
scale_color_viridis(option = "viridis", direction = -1)
93+
scale_fill_viridis(direction = -1, label = scales::dollar) +
94+
scale_color_viridis(direction = -1, label = scales::dollar) +
95+
labs(title = "Annual Mean Wages by State",
96+
subtitle = "Mental Health and Substance Abuse Social Workers(SOC Code211023)",
97+
caption = "Data Source: BLS.gov - Occupational Employment Statistics ; 2016")
98+
10299
```
103100

104101

_ZZ_move_HTML-docs_to_blog-dir.Rmd

Lines changed: 0 additions & 56 deletions
This file was deleted.
File renamed without changes.

_geom_sf_and_coordinates_and_layers_was_02_choropleth.Rmd renamed to _old/_geom_sf_and_coordinates_and_layers_was_02_choropleth.Rmd

File renamed without changes.

0 commit comments

Comments
 (0)