forked from jtr13/cc21fall2
-
Notifications
You must be signed in to change notification settings - Fork 0
/
intro_to_maps_with_ggmap.Rmd
198 lines (118 loc) · 8.08 KB
/
intro_to_maps_with_ggmap.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
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
# Video introduction to maps with ggmap
Ryan Rogers
```{r, include=FALSE}
knitr::opts_chunk$set(echo = TRUE)
library(tidyverse)
```
```{r}
# install.packages("ggmap")
library(ggmap)
# You will also need to activate an API key! Use a line like the following:
# register_google("<API KEY>")
```
```{r, include=FALSE}
# Note that the below API key will be deactivated after the Fall 2021 semester!
register_google(Sys.getenv("API_TOKEN"))
```
## Link to Video Tutorial
https://www.youtube.com/watch?v=AKEEAaCbtog
## Getting Started
This demonstration will show you how to make static maps within the tidyverse framework using ggmap. Before we get started, make sure you have ggmap installed and ready to go, since you may not have it installed by default.
One thing that can be somewhat pesky when working with ggmap is Google maps. As of 2018, this service requires an API key to use. To access the full features of this demonstration, you will have to register a Google API key. Details on how to do this can be found in the help file for 'register_google'.
**Note: If you wish to run the code found in this file, you will need to register a Google API key!**
## Why ggmap?
ggmap is useful for our demonstration, since it will use syntax that you may be familiar with already! ggmap is designed to be quite compatible with ggplot2 (in fact, Hadley Wickham, one of the authors of ggplot2, is a co-author!). In fact, much of ggmap is, under the hood, largely comprised of ggplot2 components. ggmap, however, features the added benefit of abstracting away from many of the more involved components of setting up a ggplot2 visual with maps, and will doubtless prove easier to use than base ggplot2 alone. ggmap also has many options for customizing its resulting visuals, though we will not explore many of these options in depth.
It should be noted that ggmap is an excellent tool for *static* maps. If you are looking more dynamic maps that provide more potential for user interaction, you may find other libraries, such as leaflet, that better suit your needs.
## Dataset for Demonstration
For our demonstration, we will utilize data on public WiFi locations in New York City. The dataset can be found here:
https://data.cityofnewyork.us/City-Government/NYC-Wi-Fi-Hotspot-Locations/yjub-udmw . We will save this dataset to 'wifi_data':
```{r}
wifi_data <- read.csv("https://data.cityofnewyork.us/api/views/yjub-udmw/rows.csv?accessType=DOWNLOAD")
head(wifi_data)
```
## Creating Maps (No plotting)
Before we dive into how to overlay datapoints and other visuals over our graphs, we'll start by going over how to create maps in the first place.
### By location name
The first, and perhaps more approachable way we can plot a map is by simply specifying the name of the location we'd like to see. This works through the geocode function. Here is an example with the city of Paris, France:
```{r}
geocode("Paris")
```
If you want to verify these coordinates, you can try for yourself! Simply plug 48.85661, 2.352222 into Google Maps, and you will see that this is indeed the location of Paris, France (or, a location roughly in the center of Paris). revgeocode and mapdist are two other useful functions to do the inverse of geocode and calculate distances, respectively.
To generate a map using a location name, we run two steps. First we use get_map(), specifying the location name. This gives a ggmap object. Second, we generate a ggplot2-compatible plot, using ggmap() and providing our ggmap object from step 1 as the input. To demonstrate our location lookup works with more abstract names, let us try finding the Parthenon, in Athens:
```{r}
parthenon <- get_map(location = "parthenon, athens")
parthenon_map <- ggmap(parthenon)
parthenon_map
```
Clearly the Parthenon cannot be viewed from space. Let us adjust some variables to give us a clearer view. First, we will adjust the 'zoom' parameter. By default, get_map will select this for us, but we can specify it manually. Here, it is initially set to 10, which is about right for a city. Let us try 16 instead:
```{r}
parthenon <- get_map(location = "parthenon, athens", zoom = 16)
parthenon_map <- ggmap(parthenon)
parthenon_map
```
That is better. The style of this map may be a little distracting, but we will show how to change this later.
### By geographic coordinates
Another way to plot a map is with geographic coordinates directly. Here, we will input the coordinates for the Grand Canyon:
```{r}
grand_canyon <- get_map(location = c(-112, 36.1), zoom = 9)
grand_canyon_map <- ggmap(grand_canyon)
grand_canyon_map
```
### Using shape files
Shape files are also an option for plotting maps with ggmap, but we will not cover them here.
## Quick Map Plot
In the event that you would like a quick visual plotted, given a set of latitude and longitude points that you would like to superimpose on a map, there is an easy option in the form of qmplot. Here is an example, using our sample dataset:
```{r}
qmplot(data=wifi_data, x = Longitude, y = Latitude, color = I('Blue'), alpha = I(0.05))
```
If you are looking for a quick visual, this may suffice in some cases.
## Plotting by Overlaying ggplot2 Visuals
In most cases, you will want more elaborate visuals, which may be easier to accomplish with ggplot2 and ordinary ggmaps. Here are some examples:
### Scatterplot
Let us start with a basic plotting. Note that 'lat' and 'lon' are the expected names of the x and y values.
```{r}
wifi_data_renamed <- mutate(wifi_data, lat = Latitude, lon = Longitude)
nyc <- get_map(location = "manhattan", zoom = 12)
ggmap(nyc) +
geom_point(data = wifi_data_renamed, aes(color = Type), alpha=.2)
```
We see two issues. First, the legend could be inset at the top left. This can be modified within our ggmap. Second, our map colors may be confusing, given that parks and water are the same colors as several of our categories. Let us correct these issues:
```{r}
nyc <- get_map(location = "manhattan", zoom = 12, source = 'stamen', maptype = "toner")
ggmap(nyc, extent = 'device', legend = "topleft") +
geom_point(data = wifi_data_renamed, aes(color = Type), alpha=.2)
```
We used arguments extent = 'device' and legend = "topleft" to move our legend, and source = 'stamen' and maptype = "toner" to adjust our map's appearance.
### Heatmap with Rectangular Bins
In the event of overplotting, a rectangular heatmap can be used. The below graph shows a rectangular heatmap for the area of manhattan north of central park.
```{r}
nyc <- get_map(location = c(-73.96, 40.8), zoom = 14, source = 'stamen', maptype = "toner")
ggmap(nyc, extent = 'device', legend = "topleft") +
geom_bin_2d(data = wifi_data_renamed, aes(color = Type, fill = Type), bins = 40, alpha=.7)
```
### Contour Plots
One final visual of interest is the contour plot. Here is an example of one:
```{r}
nyc <- get_map(location = "manhattan", zoom = 12, color = 'bw')
ggmap(nyc, extent = 'device', legend = "topleft") +
geom_density_2d_filled(data = wifi_data_renamed, alpha = 0.3)
```
### Overlaying Routes
One other neat feature of ggmap is the ability to superimpose routes. Here is a map that shows the available WiFi hotspots near a walking route from Columbia to Central Park:
```{r}
to_cp <- route(from = 'Columbia University', to = 'Frederick Douglass Circle', mode = 'walking')
nyc <- get_map(location = c(-73.96, 40.803), zoom = 16, color = 'bw')
ggmap(nyc) +
geom_leg(aes(x = start_lon, y = start_lat, xend = end_lon, yend = end_lat), size = 2, color = 'blue', data=to_cp) +
geom_point(data = wifi_data_renamed, color = 'red')
```
Looks like pickings are pretty slim!
## Recommended Reading
If you're looking for additional reading on ggmap, I would recommend https://journal.r-project.org/archive/2013-1/kahle-wickham.pdf .
## Sources
L. Ellis. Map Plots Created With R And Ggmap. Little Miss Data. April 15, 2018. URL
https://www.littlemissdata.com/blog/maps
D. Kahle and H. Wickham. ggmap: Spatial Visualization with ggplot2. The R Journal, 5(1), 144-161. URL
http://journal.r-project.org/archive/2013-1/kahle-wickham.pdf
N. Voevodin. R, Not the Best Practices. April 6, 2020. URL
https://bookdown.org/voevodin_nv/R_Not_the_Best_Practices/maps.html