Skip to content

Commit 2cc038d

Browse files
committed
update plot xy ; tidycen
1 parent 4fe89b9 commit 2cc038d

24 files changed

+1016
-399
lines changed

01_georeference.Rmd

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

9-
To introduce the `library(mapview)` package, 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]
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]
1010

1111

1212
## Load Libraries
@@ -42,7 +42,9 @@ Transform our standard tibble (i.e. the starbucksNC data frame) into a spacial d
4242
sbuxsfc <- st_as_sf(starbucksNC, coords = c("Longitude", "Latitude"), crs=4326)
4343
```
4444

45-
Below, you can set the `map.types` argument to `openStreetMap`. Base maps are set with `map.types`. For example a high contrast, black and white basemap can be set with the argument `map.types = "Stamen.Toner"`. See avaiable [map types](http://leaflet-extras.github.io/leaflet-providers/preview/), or leave out the map.types argument for a set of default base maps to choose via the layering button.
45+
## Visualize the Map
46+
47+
Below, you can plot the latitude and longitude coordinate, and set the `map.types` argument to `openStreetMap`. Base maps are set with `map.types`. For example a high contrast, black and white basemap can be set with the argument `map.types = "Stamen.Toner"`. See available [map types](http://leaflet-extras.github.io/leaflet-providers/preview/), or leave out the map.types argument for a set of default base maps to choose via the layering button.
4648

4749
```{r plotmap}
4850
#mapview(sbuxsfc, map.types = "Stamen.Toner")
@@ -53,7 +55,7 @@ mapview(sbuxsfc)
5355

5456
## Save the Map
5557

56-
You can save a map as an interalctive 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:
58+
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:
5759

5860
- `mapshot(starNCmap, file = "map.png")`
5961
- `mapshot(starNCmap, url = "map.html")`

02_choropleth.Rmd

Lines changed: 34 additions & 56 deletions
Original file line numberDiff line numberDiff line change
@@ -8,10 +8,11 @@ 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)
1112
library(tidycensus)
12-
library(leaflet)
1313
library(sf)
14-
library(stringr)
14+
library(leaflet)
15+
library(mapview)
1516
```
1617

1718
## Census API Key
@@ -52,36 +53,16 @@ nc_pop <-
5253

5354

5455

55-
## Make Choropleth in Leaflet
56-
57-
58-
Generate a color palette for the colors and legend
59-
60-
```{r colorpalette}
61-
MapPalette <- colorQuantile(palette = "viridis", domain = nc_pop$estimate, n = 10)
62-
```
56+
## Make Choropleth via mapview
6357

64-
Make choropleth by filling county polygons (census geography) with correlated value (population variable *B01003_001*).
58+
Identify which variable will be used to create the color ramp shading. Assign this variable with the `zcol` argument. The `estimate` variable was extracted via the `tidycensus::get_acs()` function.
6559

66-
`st_transform()` - converts coordinate system of simple features
6760

6861
```{r make_choropleth}
69-
nc_pop %>%
70-
st_transform(crs = "+init=epsg:4326") %>%
71-
leaflet(width = "100%") %>%
72-
addProviderTiles(provider = "Stamen.TonerLines") %>%
73-
addPolygons(popup = ~ str_extract(NAME, "^([^,]*)"),
74-
stroke = FALSE,
75-
smoothFactor = 0,
76-
fillOpacity = 0.7,
77-
color = ~ MapPalette(estimate)) %>%
78-
addLegend("bottomright",
79-
pal = MapPalette,
80-
values = ~ estimate,
81-
title = "Population percentiles",
82-
opacity = 1)
62+
mapview(nc_pop, zcol = "estimate")
8363
```
8464

65+
8566
## Add another layer
8667

8768
Now we'll geolocate the Starbucks stores and add those locations as a layer over our choropleth. The Starbucks locations were generated and plotted in the previous exercise. Here we regenerate the StarbuckNC object.
@@ -99,33 +80,32 @@ starbucksNC <- starbucks %>%
9980
filter(State == "NC")
10081
```
10182

102-
Generate the map with multiple layers.
103-
104-
```{r add_layer}
105-
nc_pop %>%
106-
st_transform(crs = "+init=epsg:4326") %>%
107-
leaflet(width = "100%") %>%
108-
addProviderTiles(provider = "Stamen.TonerLines") %>%
109-
addPolygons(popup = ~ str_extract(NAME, "^([^,]*)"),
110-
stroke = FALSE,
111-
smoothFactor = 0,
112-
fillOpacity = 0.7,
113-
color = ~ MapPalette(estimate)) %>%
114-
addLegend("bottomright",
115-
pal = MapPalette,
116-
values = ~ estimate,
117-
title = "Population percentiles",
118-
opacity = 1) %>%
119-
addTiles() %>%
120-
# The next paragraph adds a layer from exercise 01
121-
addCircleMarkers(data = starbucksNC,
122-
lat = ~ Latitude,
123-
lng = ~ Longitude,
124-
popup = starbucksNC$Name,
125-
weight = 1,
126-
radius = 0.6,
127-
opacity = 0.5,
128-
color = "black")
83+
Convert the `starbuckNC` dataframe to a spatial (sf) object and assign the same projection as the `nc_pop` spatial object.
84+
85+
```{r}
86+
starbucksNC <- st_as_sf(starbucksNC, coords = c("Longitude", "Latitude"), crs = st_crs(nc_pop))
87+
```
88+
89+
90+
91+
Generate the map with multiple layers. You can read more about additional arguments such as `homebutton, legend, alpha, cex` in the [`mapview()` documentation](https://r-spatial.github.io/mapview/reference/mapView.html). Read about the many more mapview functions in the [full documentation](https://r-spatial.github.io/mapview/reference/index.html).
92+
93+
```{r}
94+
mymap <- mapview(nc_pop,
95+
zcol = "estimate",
96+
homebutton = FALSE) +
97+
mapview(starbucksNC,
98+
zcol = "Name",
99+
legend = FALSE,
100+
alpha = 0.5, cex = 3,
101+
homebutton = FALSE)
102+
103+
addLogo(mymap, "images/Rfun3.png",
104+
position = "bottomright",
105+
offset.x = 8,
106+
offset.y = 38,
107+
width = 100,
108+
height = 100)
129109
```
130110

131111
## Census
@@ -152,6 +132,4 @@ In the [*tmap* section](031_thematic_mapping.html) of the [Simple Features modul
152132

153133
This session based on
154134

155-
- Kyle Walker's [TidyCensus](https://walkerke.github.io/tidycensus/) package
156-
157-
- Julia Silge's [leaflet implementation of tidycensus](https://juliasilge.com/blog/using-tidycensus/)
135+
- Kyle Walker's [TidyCensus](https://walkerke.github.io/tidycensus/) package

docs/01_georeference.html

Lines changed: 11 additions & 20 deletions
Large diffs are not rendered by default.

docs/01_ggmap_georef.html

Lines changed: 3 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -438,7 +438,7 @@
438438
<ul class="nav navbar-nav">
439439
<li class="dropdown">
440440
<a href="#" class="dropdown-toggle" data-toggle="dropdown" role="button" aria-expanded="false">
441-
Home
441+
Preface
442442

443443
<span class="caret"></span>
444444
</a>
@@ -454,20 +454,8 @@
454454
</li>
455455
</ul>
456456
</li>
457-
<li class="dropdown">
458-
<a href="#" class="dropdown-toggle" data-toggle="dropdown" role="button" aria-expanded="false">
459-
Plot Coordinates
460-
461-
<span class="caret"></span>
462-
</a>
463-
<ul class="dropdown-menu" role="menu">
464-
<li>
465-
<a href="01_georeference.html">Interactive map (leaflet)</a>
466-
</li>
467-
<li>
468-
<a href="01_ggmap_georef.html">Static Map (ggmap)</a>
469-
</li>
470-
</ul>
457+
<li>
458+
<a href="01_georeference.html">Plot Coordinates</a>
471459
</li>
472460
<li>
473461
<a href="02_choropleth.html">tidycensus</a>

docs/02_choropleth.html

Lines changed: 37 additions & 68 deletions
Large diffs are not rendered by default.

docs/030_thematic_sf.html

Lines changed: 3 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -373,7 +373,7 @@
373373
<ul class="nav navbar-nav">
374374
<li class="dropdown">
375375
<a href="#" class="dropdown-toggle" data-toggle="dropdown" role="button" aria-expanded="false">
376-
Home
376+
Preface
377377

378378
<span class="caret"></span>
379379
</a>
@@ -389,20 +389,8 @@
389389
</li>
390390
</ul>
391391
</li>
392-
<li class="dropdown">
393-
<a href="#" class="dropdown-toggle" data-toggle="dropdown" role="button" aria-expanded="false">
394-
Plot Coordinates
395-
396-
<span class="caret"></span>
397-
</a>
398-
<ul class="dropdown-menu" role="menu">
399-
<li>
400-
<a href="01_georeference.html">Interactive map (leaflet)</a>
401-
</li>
402-
<li>
403-
<a href="01_ggmap_georef.html">Static Map (ggmap)</a>
404-
</li>
405-
</ul>
392+
<li>
393+
<a href="01_georeference.html">Plot Coordinates</a>
406394
</li>
407395
<li>
408396
<a href="02_choropleth.html">tidycensus</a>

docs/031_thematic_mapping.html

Lines changed: 3 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -438,7 +438,7 @@
438438
<ul class="nav navbar-nav">
439439
<li class="dropdown">
440440
<a href="#" class="dropdown-toggle" data-toggle="dropdown" role="button" aria-expanded="false">
441-
Home
441+
Preface
442442

443443
<span class="caret"></span>
444444
</a>
@@ -454,20 +454,8 @@
454454
</li>
455455
</ul>
456456
</li>
457-
<li class="dropdown">
458-
<a href="#" class="dropdown-toggle" data-toggle="dropdown" role="button" aria-expanded="false">
459-
Plot Coordinates
460-
461-
<span class="caret"></span>
462-
</a>
463-
<ul class="dropdown-menu" role="menu">
464-
<li>
465-
<a href="01_georeference.html">Interactive map (leaflet)</a>
466-
</li>
467-
<li>
468-
<a href="01_ggmap_georef.html">Static Map (ggmap)</a>
469-
</li>
470-
</ul>
457+
<li>
458+
<a href="01_georeference.html">Plot Coordinates</a>
471459
</li>
472460
<li>
473461
<a href="02_choropleth.html">tidycensus</a>

docs/032_thematic_mapping_geom_sf.html

Lines changed: 3 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -438,7 +438,7 @@
438438
<ul class="nav navbar-nav">
439439
<li class="dropdown">
440440
<a href="#" class="dropdown-toggle" data-toggle="dropdown" role="button" aria-expanded="false">
441-
Home
441+
Preface
442442

443443
<span class="caret"></span>
444444
</a>
@@ -454,20 +454,8 @@
454454
</li>
455455
</ul>
456456
</li>
457-
<li class="dropdown">
458-
<a href="#" class="dropdown-toggle" data-toggle="dropdown" role="button" aria-expanded="false">
459-
Plot Coordinates
460-
461-
<span class="caret"></span>
462-
</a>
463-
<ul class="dropdown-menu" role="menu">
464-
<li>
465-
<a href="01_georeference.html">Interactive map (leaflet)</a>
466-
</li>
467-
<li>
468-
<a href="01_ggmap_georef.html">Static Map (ggmap)</a>
469-
</li>
470-
</ul>
457+
<li>
458+
<a href="01_georeference.html">Plot Coordinates</a>
471459
</li>
472460
<li>
473461
<a href="02_choropleth.html">tidycensus</a>

docs/033_thematic_leaflet_example.html

Lines changed: 5 additions & 17 deletions
Large diffs are not rendered by default.

docs/036_thematic_mapping-outputs-fullwebpage.html

Lines changed: 3 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -438,7 +438,7 @@
438438
<ul class="nav navbar-nav">
439439
<li class="dropdown">
440440
<a href="#" class="dropdown-toggle" data-toggle="dropdown" role="button" aria-expanded="false">
441-
Home
441+
Preface
442442

443443
<span class="caret"></span>
444444
</a>
@@ -454,20 +454,8 @@
454454
</li>
455455
</ul>
456456
</li>
457-
<li class="dropdown">
458-
<a href="#" class="dropdown-toggle" data-toggle="dropdown" role="button" aria-expanded="false">
459-
Plot Coordinates
460-
461-
<span class="caret"></span>
462-
</a>
463-
<ul class="dropdown-menu" role="menu">
464-
<li>
465-
<a href="01_georeference.html">Interactive map (leaflet)</a>
466-
</li>
467-
<li>
468-
<a href="01_ggmap_georef.html">Static Map (ggmap)</a>
469-
</li>
470-
</ul>
457+
<li>
458+
<a href="01_georeference.html">Plot Coordinates</a>
471459
</li>
472460
<li>
473461
<a href="02_choropleth.html">tidycensus</a>

0 commit comments

Comments
 (0)