-
Notifications
You must be signed in to change notification settings - Fork 136
/
gis-with-r-intro.R
245 lines (204 loc) · 6.83 KB
/
gis-with-r-intro.R
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
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
### Introduction to GIS with R: Spatial data with the sp and sf packages ###
# This script goes along with the blog post of the same name,
# which can be found at https://www.jessesadler.com/post/gis-with-r-intro/
# See the Rmarkdown document for the contents of the blog post.
### Load packages and data
library(tidyverse)
library(sp)
library(sf)
library(rnaturalearth)
# Load the data
letters <- read_csv("data/correspondence-data-1585.csv")
locations <- read_csv("data/locations.csv")
########################
## Preparing the data ##
########################
# Letters per source
sources <- letters %>%
group_by(source) %>%
count() %>%
rename(place = source) %>%
add_column(type = "source") %>%
ungroup()
# Letters per destination
destinations <- letters %>%
group_by(destination) %>%
count() %>%
rename(place = destination) %>%
add_column(type = "destination") %>%
ungroup()
# Bind the rows of the two data frames
# and change type column to factor
letters_data <- rbind(sources, destinations) %>%
mutate(type = as_factor(type))
# Join letters_data to locations
geo_data <- left_join(letters_data, locations, by = "place")
##################################
## Spatial data with sp package ##
##################################
# Create data frame of only longitude and latitude values
coords <- select(geo_data, lon, lat)
# Create SpatialPoints object with coords and CRS
points_sp <- SpatialPoints(coords = coords,
proj4string = CRS("+proj=longlat +datum=WGS84"))
# Create SpatialPointsDataFrame object
points_spdf <- SpatialPointsDataFrame(coords = coords,
data = letters_data,
proj4string = CRS("+proj=longlat +datum=WGS84"))
# Example of subsetting `points_spdf` to return locations with "n" greater than 10
points_spdf[points_spdf@data$n > 10, ]
# Get coastal and country world maps as Spatial objects
coast_sp <- ne_coastline(scale = "medium")
countries_sp <- ne_countries(scale = "medium")
####################################
## Mapping with sp and base plots ##
####################################
### Set up plot ###
# Create a new color palette to distinguish source and destination
palette(alpha(c("darkorchid", "darkorange"), 0.7))
# Set margins for bottom, left, top, and right of plot
par(mar = c(1, 1, 3, 1))
### Plot points ###
plot(points_spdf,
pch = 20,
col = points_spdf$type,
cex = sqrt(points_spdf$n)/2 + 0.25)
# Add a box around the plot
box()
# Add a title
title(main = "Correspondence of Daniel van der Meulen, 1585")
### Plot map with coastlines (lines) data ###
# Pointsize vector for legend
pointsize <- c(1, 50, 100)
par(mar = c(1, 1, 3, 1))
# Plot points
plot(points_spdf,
pch = 20,
col = points_spdf$type,
cex = sqrt(points_spdf$n)/2 + 0.25)
# Plot coastlines background map
plot(coast_sp,
col = "black",
add = TRUE)
# Add a box around the plot
box()
# Legend for colors
legend("topright", legend = levels(points_spdf$type),
pt.cex = 2,
col = 1:2,
pch = 15)
# legend for size of points
legend("right", legend = pointsize,
pt.cex = (sqrt(pointsize)/2 + 0.25),
col = "black",
pch = 20,
title = "Letters")
# Title for the map
title(main = "Correspondence of Daniel van der Meulen, 1585")
# Make bounding box for countries_sp match
# bounding box of points_spdf
countries_sp@bbox <- bbox(points_spdf)
### Plot map with countries (polygons) data ###
par(mar = c(1, 1, 3, 1))
# Plot countries map and color with grays
plot(countries_sp,
col = gray(0.8),
border = gray(0.7))
# Plot points
plot(points_spdf,
pch = 20,
col = points_spdf$type,
cex = sqrt(points_spdf$n)/2 + 0.25,
add = TRUE)
# Add a box around the plot
box()
# Legend for colors
legend("topright",
legend = levels(points_spdf$type),
pt.cex = 2,
col = 1:2,
pch = 15)
# legend for size of points
legend("right",
legend = pointsize,
pt.cex = (sqrt(pointsize)/2 + 0.25),
col = "black",
pch = 20,
title = "Letters")
# Title for the map
title(main = "Correspondence of Daniel van der Meulen, 1585")
##################################
## Spatial data with sf package ##
##################################
# Create sf object with geo_data data frame and CRS
points_sf <- st_as_sf(geo_data, coords = c("lon", "lat"), crs = 4326)
# Get coastal and country world maps as sf objects
coast_sf <- ne_coastline(scale = "medium", returnclass = "sf")
countries_sf <- ne_countries(scale = "medium", returnclass = "sf")
# Subset of locations with "n" greater than 10
filter(points_sf, n > 10)
### Subset of countries object to get South American countries ###
# South American countries with new CRS
countries_sf %>%
filter(continent == "South America") %>%
select(name) %>%
st_transform(crs = "+proj=moll +datum=WGS84")
### Make map of South America ###
# Return to default palette
palette("default")
# Map of South American countries
countries_sf %>%
filter(continent == "South America") %>%
select(name) %>%
st_transform(crs = "+proj=moll +datum=WGS84") %>%
plot(key.pos = NULL, graticule = TRUE, main = "South America")
#################################
## Mapping with sf and ggplot2 ##
#################################
### Basic ggplot2 plot with geom_sf ###
ggplot() +
geom_sf(data = coast_sf) +
geom_sf(data = points_sf,
aes(color = type, size = n),
alpha = 0.7,
show.legend = "point") +
coord_sf(xlim = c(-1, 14), ylim = c(44, 55))
### Plot map with coastlines (lines) data ###
# Load ggrepel package
library(ggrepel)
ggplot() +
geom_sf(data = coast_sf) +
geom_sf(data = points_sf,
aes(color = type, size = n),
alpha = 0.7,
show.legend = "point") +
coord_sf(xlim = c(-1, 14), ylim = c(44, 55),
datum = NA) + # removes graticules
geom_text_repel(data = locations,
aes(x = lon, y = lat, label = place)) +
labs(title = "Correspondence of Daniel van der Meulen, 1585",
size = "Letters",
color = "Type",
x = NULL,
y = NULL) +
guides(color = guide_legend(override.aes = list(size = 6))) +
theme_minimal()
### Plot map with countries (polygons) data ###
ggplot() +
geom_sf(data = countries_sf,
fill = gray(0.8), color = gray(0.7)) +
geom_sf(data = points_sf,
aes(color = type, size = n),
alpha = 0.7,
show.legend = "point") +
coord_sf(xlim = c(-1, 14), ylim = c(44, 55),
datum = NA) + # removes graticules
geom_text_repel(data = locations,
aes(x = lon, y = lat, label = place)) +
labs(title = "Correspondence of Daniel van der Meulen, 1585",
size = "Letters",
color = "Type",
x = NULL,
y = NULL) +
guides(color = guide_legend(override.aes = list(size = 6))) +
theme_bw()