-
Notifications
You must be signed in to change notification settings - Fork 1
/
mapCAN.R
138 lines (84 loc) · 3.2 KB
/
mapCAN.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
# mapcan choropleth for Canada
library(mapcan)
#--- boundaries = province for geographic data at the province level
# boundaries = census
# boundaries = ridings
#--- geographic data for a standard choropleth map
# type = standard
# maps that alter the geography based on the
# population size at the province or census
# type = cartogram.
# mapcan::riding_binplot()
# mapcan() will provide geographic data for the entire country.
# province: only one province (or territory)
# (options are NL, PE, NS, NB, QC, ON, MB, SK, AB, BC, YT, NT, and NU)
# --- basic data
mapcan(boundaries = province,
type = standard) %>%
head()
library(ggplot2)
pr_map <- mapcan(boundaries = province,
type = standard) %>%
ggplot(aes(x = long, y = lat, group = group))
pr_map <- pr_map +
geom_polygon() +
coord_fixed()
pr_map
# axis ticks and background grid using theme_mapcan function
pr_map +
theme_mapcan() +
## Add a title
ggtitle("Map of Canada with Provincial/Territorial Boundaries")
# province_pop_annual data frame that is included in the mapcan package.
# dataset provides annual provincial/territorial population estimates dating back to 1971.
library(dplyr)
canada = mapcan::province_pop_annual
can2017 = canada %>% filter(canada$year == 2017)
pop_2017 <- mapcan::province_pop_annual %>%
filter(year == 2017)
head(pop_2017)
# attach these numbers to every point on the polygons of the provinces
# use inner_join()
pr_geographic <- mapcan(boundaries = province,
type = standard)
pr_geographic <- inner_join(pr_geographic,
pop_2017,
by = c("pr_english" = "province"))
# colour the provinces according to their population size,
# set the population variable as a fill aesthetic.
pr_geographic %>%
ggplot(aes(x = long, y = lat, group = group, fill = population)) +
geom_polygon( col="white") +
coord_fixed() +
theme_mapcan() +
scale_fill_viridis_c(name ="Population") +
ggtitle("Canadian Population by Province (2017)") + theme(legend.position = "left")
# --------------
bc_ridings <- mapcan(boundaries = ridings,
type = standard,
province = BC)
head(bc_ridings)
# Plot geographic data with riding boundaries
ggplot(bc_ridings, aes(x = long, y = lat, group = group)) +
geom_polygon() +
coord_fixed() +
theme_mapcan() +
ggtitle("British Columbia \nFederal Electoral Ridings")
# Incorporate riding-level statistics
# federal_election_results (up to 2015) data frame that is included in the mapcan package.
bc_results <- mapcan::federal_election_results %>%
# Restrict data to include just 2015 election results from BC
filter(election_year == 2015 & pr_alpha == "BC")
head(bc_results)
bc_ridings <- inner_join(bc_results, bc_ridings, by = "riding_code")
bc_riding_map <- bc_ridings %>%
ggplot(aes(x = long, y = lat, group = group, fill = party)) +
geom_polygon() +
coord_fixed() +
theme_mapcan() +
ggtitle("British Columbia \n2015 Federal Electoral Results")
bc_riding_map
# change colors
bc_riding_map +
scale_fill_manual(name = "Winning party",
values = c("blue", "springgreen3", "red", "Orange"))