-
Notifications
You must be signed in to change notification settings - Fork 2
/
Copy path11_Overview_map.Rmd
226 lines (186 loc) · 6.72 KB
/
11_Overview_map.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
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
---
title: "Overview map"
output:
html_document:
keep_md: true
toc: true
toc_depth: 3
toc_float: true
code_folding: hide
df_print: paged
---
Makes overview map, using the map data in NIVA's "K:/Kart"
The "second try" map is the one used
This code uses the new _sf_ package (which supersedes _sp_). It works nicely together with ggplot2 (see below), but for the time being (Dec. 2018) you need the _development version_ of ggplot2. See code below for details.
## Packages
```{r}
# Install
# install.packages("sf")
# Also needs the *development version* of ggplot2
# First, remove folders digest and ggplot2 in your Library folder (I needed to do that, at least)
# (Eh, I don't know where I store my packages? Run: .libPaths() )
# Then:
# install.packages("digest")
# devtools::install_github("tidyverse/ggplot2")
library(sf)
library(ggplot2)
library(dplyr)
library(ggrepel)
save_plot <- FALSE
```
## Load basemap
```{r}
nc_norway <- st_read("K:/Kart/N1000/norge2.shp")
```
### Check coordinate system of the base map
It is NA, so we have to set it manually
```{r}
# Check
check <- st_crs(nc_norway)
check
if (is.na(check)){
cat("\nSet coordinate system:\n")
st_crs(nc_norway) <- "+proj=utm +zone=33"
}
```
### Test map
In order to find values xlim and ylim, use the map with 'datum' set to the 'native' coordinates (ca. 20 seconds)
```{r}
make_test_map <- TRUE
if (make_test_map){
ggplot(nc_norway) +
geom_sf() +
theme_bw() +
coord_sf(datum = st_crs("+proj=utm +zone=33"))
}
```
### Check background map (ca. 20 seconds)
```{r}
ggplot(nc_norway) +
geom_sf() +
theme_bw() +
coord_sf(ylim = c(6.45, 6.7)*1E6, xlim = c(0.05, 0.3)*1E6)
```
## Add rivers from the ELVIS base
```{r}
nc_rivers <- st_read("K:\\Kart\\ELVIS\\Hovedelv\\elvis_hovedelv.shp")
```
### Various checks on river data
```{r}
# Check variables:
nc_rivers[1,]
# Check STRAHLER klassification of river size
cat("\n\nStrahler classification\n------------------------------\n")
xtabs(~STRAHLER, nc_rivers)
# Check names of rivers
cat("\n\nRiver names\n------------------------------\n")
cat("Strahler = 8\n")
nc_rivers %>% filter(STRAHLER == 8) %>% xtabs(~ELVENAVN, ., drop = TRUE) %>% names() %>% dput()
cat("Strahler = 7\n")
nc_rivers %>% filter(STRAHLER == 7) %>% xtabs(~ELVENAVN, ., drop = TRUE) %>% names() %>% dput()
```
```{r}
# Pick rivers to show
rivers <- c("GLOMMAVASSDRAGET", "DRAMMENSVASSDRAGET", "SKIENSVASSDRAGET", "NUMEDALSLÅGEN")
sel <- with(nc_rivers, ELVENAVN %in% rivers); sum(sel)
ggplot(nc_norway) +
geom_sf() +
geom_sf(data = nc_rivers[sel,], color = "blue") +
theme_bw() +
coord_sf(ylim = c(6.45, 6.7)*1E6, xlim = c(0.05, 0.3)*1E6)
```
## Add sampling points
```{r}
df_stations <- read.csv(textConnection("Station_type, Name, Lat, Long
Hard bottom,HR104, 58.2732, 08.5372
Hard bottom,HT113, 58.5132, 08.9445
Soft bottom,BR1, 58.3253, 08.6295
Soft bottom,BT44, 58.4038, 09.0312
Hydrography/plankton, Arendal 2, 58.3870, 8.8330
River,Glomma, 59.278, 11.134
River,Drammenselva, 59.75399, 10.00903
River,Numedalslågen, 59.08627, 10.06962
River,Skienselva, 59.199, 9.611
"), stringsAsFactors = FALSE)
sf_stations <- st_as_sf(df_stations, coords = c("Long", "Lat"), crs = "+proj=longlat")
sf_stations[1,]
```
### Draw map, first try
```{r}
# Even if the background map and nc_norway is in UTM coordinates and sf_stations is in long-lat,
# the points automatically adjust to the background map
# However,
# - wasn't able to use geom_text_repel as in https://www.r-spatial.org/r/2018/10/25/ggplot2-sf-2.html
# - wasn't able to make nudge_x working
# But check the last map
ggplot(nc_norway) +
geom_sf() +
geom_sf(data = nc_rivers[sel,], color = "blue") +
geom_sf(data = sf_stations, aes(color = Station_type), show.legend = "point") +
geom_sf_text(data = sf_stations, aes(label = Name), adj = -0.1) +
theme_bw() +
coord_sf(ylim = c(6.45, 6.7)*1E6, xlim = c(0.05, 0.3)*1E6) +
theme(axis.title = element_blank())
```
Hard bottom,HR104, 58.2732, 08.5372
Hard bottom,HT113, 58.5132, 08.9445
Soft bottom,BR1, 58.3253, 08.6295
Soft bottom,BT44, 58.4038, 09.0312
Hydrography/plankton, Arendal 2, 58.3870, 8.8330
River station,Glomma, 59.278, 11.134
River station,Drammenselva, 59.75399, 10.00903
River station,Numedalslågen, 59.08627, 10.06962
River station,Skienselva, 59.199, 9.611
### Draw map, second try
Manual adjustment
```{r}
# We make adjusted positions and a new sf object
# For longitude, note that they also are adjusted for being mostly left-adjusted
# (adj = 0 in geom_sf_text) but some are right-adjusted (adj = 1 in geom_sf_text)
df_stations$Lat2 <- df_stations$Lat + c(-0.07,0.03,0.03,0.03,-0.07,0,0,-0.15,0)
df_stations$Long2 <- df_stations$Long + 0.05 + c(-0.07, 0, -0.1, 0, -0.1, 0, -0.1, 0, -0.1)
sf_stations_lab <- st_as_sf(df_stations, coords = c("Long2", "Lat2"), crs = "+proj=longlat")
station_order <- c("Hydrography/plankton", "Hard bottom", "Soft bottom", "River")
sf_stations$Station_type <- factor(sf_stations$Station_type, levels = station_order)
gg <- ggplot(nc_norway) +
geom_sf(fill = RColorBrewer::brewer.pal(5, name = "Pastel1")[3]) +
geom_sf(data = nc_rivers[sel,], color = "blue") +
geom_sf(data = sf_stations, aes(color = Station_type), show.legend = "point") +
scale_color_brewer("Station type", palette = "Set1") +
geom_sf_text(data = sf_stations_lab, aes(label = Name), adj = c(0,0,1,0,0,0,1,0.3,1)) +
theme_bw() +
coord_sf(ylim = c(6.45, 6.7)*1E6, xlim = c(0.05, 0.32)*1E6) +
theme(axis.title = element_blank())
if (save_plot){
ggsave("Figures_rapp/Oversiktskart.png", gg, dpi = 500)
} else {
print(gg)
}
```
### Draw map, using geom_text_repel
geom_text_repel() needs to have x and y in the data set as 'explicit' variables on the 'native' scale.
So we must first transform the data, then add coordinates as variables.
```{r}
# Transform data set to the native scale, here: UTM
sf_stations_utm <- st_transform(sf_stations, "+proj=utm +zone=33")
# Function for extracting coordinates from one map feature
get_coor <- function(i, sf){
p <- as.numeric(sf$geometry[[i]])
tibble(x = p[1], y = p[2])
}
# test
# get_coor(1, sf_stations)
# Extract all coordinates and add to the data set
coor <- 1:nrow(sf_stations) %>% purrr::map_df(~get_coor(., sf_stations_utm))
sf_stations_utm$x <- coor$x
sf_stations_utm$y <- coor$y
ggplot(nc_norway) +
geom_sf() +
geom_sf(data = nc_rivers[sel,], color = "blue") +
geom_sf(data = sf_stations, aes(color = Station_type), show.legend = "point") +
scale_color_brewer("Station type", palette = "Set1") +
geom_text_repel(data = sf_stations_utm, aes(x = x, y = y, label = Name)) +
theme_bw() +
coord_sf(ylim = c(6.45, 6.7)*1E6, xlim = c(0.05, 0.3)*1E6) +
theme(axis.title = element_blank())
```