-
Notifications
You must be signed in to change notification settings - Fork 1
/
5-mapping.Rmd
205 lines (136 loc) · 5.67 KB
/
5-mapping.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
197
198
199
200
201
202
203
204
205
---
title: "Mapping in R"
author: "Angela Zoss"
date: "8/13/2018"
output: github_document
---
## Setup your environment
```{r}
# Load required libraries
library(tidyverse)
#install.packages("maps")
library(maps)
#install.packages("mapproj")
library(mapproj)
#install.packages("sf")
library(sf)
```
## Using sf
```{r}
# example from https://www.r-spatial.org/book/
# load NC data included in sf package, then transform to appropriate projection
# (North Carolina State Plane, with EPSG code 32119)
nc <- system.file("gpkg/nc.gpkg", package="sf") %>% read_sf() %>% st_transform(32119)
```
```{r}
ggplot(nc) +
geom_sf(aes(fill=BIR74)) +
scale_fill_gradientn(colors = sf.colors())
```
## California counties, random data
```{r}
# load in the data for each county in California;
# this function comes from the "maps" package
# ?map_data for more information about how to get basic polygons
county_map <- map_data("county","california")
# add a new column providing a random value for each county
county_map <- county_map %>% group_by(subregion) %>% mutate(rand = runif(1))
# With polygon data (data where you have a series of lat/lon points defining a polygon),
# you can use geom_polygon() and coord_map(). Make sure to assign the id of each polygon to
# "group" to have each polygon appear as separate shapes.
ggplot(county_map) +
geom_polygon(aes(x = long,
y = lat,
group=group,
fill=rand)) +
coord_map()
```
## Buildings in Calaveras County, CA
```{r}
# load in building data from a csv file
point_locations <- read_csv("data/Calaveras-County-Government-Offices.csv")
# color definitions; change these to change the look of the map
default_county_color <- "white"
default_county_border <- "gray80"
cal_county_color <- "white"
cal_county_border <- "red"
dot_color <- "blue"
dot_size <- 2 #can change this if the dots are too small or large
dot_transparency <- 0.5 #can change this if the dots are too light or dark
# three different ggplot pieces; one for all the counties, one for calaveras, and one for the lat/lon points
map_counties <- geom_polygon(data = county_map, aes(x = long, y = lat, group=group), fill=default_county_color, color=default_county_border)
map_calaveras <- geom_polygon(data = county_map[county_map$subregion=="calaveras",], aes(x = long, y = lat, group=group), fill=cal_county_color, color = cal_county_border, size=1)
map_points <- geom_point(data=point_locations, aes(x=Longitude, y=Latitude), color=dot_color, size=dot_size, alpha=dot_transparency)
# add everything into a single map object, then display the map
mp <- ggplot()
mp <- mp + map_counties
mp
mp <- mp + map_calaveras
mp
mp <- mp + map_points
mp
# so far, the map is just displaying as if it's normal x/y data; we can add a
# coordinate system to project the map into a more normal ratio
# default projection is mercator(); other options are listed at ?mapproject
mp <- mp + coord_map()
mp
# optional: can zoom into map, using these long/lat as the new boundaries
zoom_long_left <- -122.0
zoom_long_right <- -119.0
zoom_lat_top <- 39.5
zoom_lat_bottom <- 37.0
# add this line (below) to zoom in
# (that is, change the minimum and maximum values shown)
mp <- mp + coord_map(xlim=c(zoom_long_left,zoom_long_right),
ylim=c(zoom_lat_bottom,zoom_lat_top))
mp
```
## El Niño measurements
```{r}
elnino <- read_csv("data/elnino.csv", na=c(".",NA),
col_types = cols(Date = col_date(format="%y%m%d"),
Humidity = col_double())) %>%
type_convert()
elnino$Longitude <- ifelse(elnino$Longitude < 0, 360+elnino$Longitude,elnino$Longitude)
# load in the data for each county in California;
# this function comes from the "maps" package, which gets loaded automatically
world_map <- map_data("world2")
# ?map_data for more information about how to get basic polygons
# color definitions; change these to change the look of the map
default_county_color <- "white"
default_county_border <- "gray80"
dot_color <- "blue"
dot_size <- 2 #can change this if the dots are too small or large
dot_transparency <- 0.5 #can change this if the dots are too light or dark
# three different ggplot pieces; one for all the counties, one for calaveras, and one for the lat/lon points
map_countries <- geom_polygon(data = world_map, aes(x = long, y = lat, group=group), fill=default_county_color, color=default_county_border)
map_buoys <- geom_point(aes(x=elnino$Longitude, y=elnino$Latitude), color=dot_color, size=dot_size, alpha=dot_transparency)
map_bin2d <- geom_bin2d(aes(x=elnino$Longitude, y=elnino$Latitude))
# add everything into a single map object, then display the map
mp <- ggplot()
mp <- mp + map_countries
mp
# add coord_map to show polygons in Mercator projection
mp <- mp + coord_map()
mp
# try alternative data overlays
mp + map_buoys
mp + map_bin2d
```
```{r}
# example from ggplot2 cheatsheet
# create a small data frame of state name and murder rate
data <- data.frame(murder = USArrests$Murder, state = tolower(rownames(USArrests)))
# grab state polygons from map_data
map <- map_data("state")
# In this example, still getting polygon data from map_data, but the numerical
# data that is used for fill is not included inside the polygon data.
# The fill is mapped from the USArrests dataset, but then the polygon
# data (from "map") is passed to the geom_map layer via the map attribute.
# The expand_limits command seems to be necessary because the plot doesn't
# have access to lat and long as the y and x variables in the normal way.
ggplot(data, aes(fill = murder)) +
geom_map(aes(map_id = state), map = map) +
expand_limits(x = map$long, y = map$lat) +
coord_map(projection = "azequalarea")
```