forked from libjohn/map-spring2019
-
Notifications
You must be signed in to change notification settings - Fork 0
/
01_georeference.Rmd
76 lines (51 loc) · 2.79 KB
/
01_georeference.Rmd
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
---
title: "Latitude and Longitude Coordinates"
author: "John Little"
date: "Jan. 24, 2019"
output: html_notebook
---
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]
## Load Libraries
```{r load package libraries}
library(tidyverse)
library(sf)
library(leaflet)
library(mapview)
```
## Load Data
2012 Starbucks locations ([data source](https://github.com/libjohn/mapping-with-R/blob/master/data/All_Starbucks_Locations_in_the_US_-_Map.csv))
```{r load-data}
starbucks <- read_csv("data/All_Starbucks_Locations_in_the_US_-_Map.csv")
```
### Subset Data to North Carolina
```{r filter-dataset}
starbucksNC <- starbucks %>%
filter(State == "NC")
starbucksNC
```
## Make the Map
In this example, plot latitude (y coordinates) and longitude (x coordinates), then set the map projection to a common projection standard, WGS84, via the argument `crs = 4326`.)
```{r map1}
mapview(starbucksNC, xcol = "Longitude", ycol = "Latitude", crs = 4269, grid = FALSE)
```
## Alternative: Transform data to Spatial object
Another way to plot the x & y coordinates is by transforming the starbucksNC tibble (i.e. the starbucksNC data frame) into a spacial data frame via the simple features function, `st_as_sf()`. It's important to set the [map projection](https://en.wikipedia.org/wiki/Map_projection) to a common standard, WGS84, via the argument `crs = 4326`.)
```{r convert2spacial}
sbux_sf <- st_as_sf(starbucksNC, coords = c("Longitude", "Latitude"), crs=4326)
```
### Now Map the sf object.
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.
```{r plotmap}
# starNCmap <- mapview(sbux_sf, map.types = "Stamen.Toner")
# starNCmap
mapview(sbux_sf)
```
## Save the Map
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:
```{r savemap}
- `mapshot(starNCmap, file = "map.png")`
- `mapshot(starNCmap, url = "map.html")`
```
---
### Creative Commons
Shareable via Creative Commons: [CC By-NC](https://creativecommons.org/licenses/by-nc/4.0/)