-
Notifications
You must be signed in to change notification settings - Fork 11
/
IntroMapping.R
112 lines (94 loc) · 3.15 KB
/
IntroMapping.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
# An Introduction to R (www.IntroRangeR.org)
#
# Devan Allen McGranahan (devan.mcgranahan@gmail.com)
#
# YouTube lectures: https://www.youtube.com/playlist?list=PLKXOvaXmjIGcSHFMe2Wpsaw4yzvWR0AgQ
# github repo: https://github.com/devanmcg/IntroRangeR
#
# Lesson 12.1.1: Introduction to mapping with ggplot2 and sf
#
# Packages
if (!require("pacman")) install.packages("pacman")
pacman::p_load(maps, mapproj, rgeos) # Dependencies
pacman::p_load(tidyverse, sf) # Main packages
# Basic mapping with maps data
world <- maps::map_data("world")
class(world)
world %>%
as_tibble
# ~The key for ggplot is the group= aesthetic~
# Outline of features with geom_path
ggplot(world) +
geom_path(aes(x=long, y=lat, group=group))
# Fill features with geom_polygon
world %>%
filter(region == "Canada") %>%
ggplot() +
geom_polygon(aes(x=long, y=lat, group=group),
fill="red")
# Alter map projection through coordinate system
world %>%
filter(region == "Canada") %>%
ggplot() +
geom_polygon(aes(x=long, y=lat, group=group),
fill="red") +
coord_map()
world %>%
filter(region == "Canada") %>%
ggplot() +
geom_polygon(aes(x=long, y=lat, group=group),
fill="red") +
coord_map("polyconic")
# sf package combines geospatial analysis tools with tidyverse
# Two important components:
# Stores spatial data in a light-weight tibble-type format
world %>%
st_as_sf(coords = c("long", "lat"))
# geom_sf is a special geom for mapping.
# It handles a lot of stuff automatically for you, i.e.
# It reads `geometry` to determine if it should plot a
# point, polygon, or line
# It automatically grabs essential aesthetics: group, x, y
# Knows projection type (no coord_map)
world %>%
filter(region == "Canada") %>%
st_as_sf(coords = c("long", "lat")) %>%
ggplot() +
geom_sf()
# Alter projection through CRS
# CRS = Coordinate Reference System
# Data are in WGS 84 (EPSG:4326)
world %>%
filter(region == "Canada") %>%
st_as_sf(coords = c("long", "lat"), crs=4326) %>%
ggplot() +
geom_sf()
# Convert to Pseudo-Mercator (EPSG:3857)
world %>%
filter(region == "Canada") %>%
st_as_sf(coords = c("long", "lat"), crs=4326) %>%
st_transform(3857) %>%
ggplot() +
geom_sf()
#
# Using other data sources
#
# Natural Earth - public domain geo data (http://www.naturalearthdata.com/)
#
# R packages connect to Natural Earth dataset
pacman::p_load(rnaturalearth, rnaturalearthdata)
# R-Natural Earth functions play well with sf
canada_sf <- ne_countries(country = "Canada",
returnclass = "sf")
names(canada_sf) # Lots of information about Canada!
canada_sf$economy
canada_sf$pop_est
# View in ggplot
ggplot(canada_sf) +
geom_sf()
canada_sf %>%
st_transform(26913) %>%
ggplot() + theme_bw() +
geom_sf(fill="red") +
theme(axis.text = element_blank(),
axis.ticks = element_blank())