/
target_plot.R
320 lines (274 loc) · 13.1 KB
/
target_plot.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
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
#' Plot a map with target locations
#'
#' Function to plot the centroids of sites to be dated and shoreline isobases of
#' employed displacement curves on a basemap. Defaults to displaying a
#' light-weight version of the spatial coverage in south-eastern Norway.
#' However, spatial geometries covering other regions can also be provided or
#' temporarily downloaded with the function.
#'
#' @param targets Objects of class `sf` representing the sites to be dated. The
#' first column beyond geom is taken as site name.
#' @param isobases Spatial lines as object of class `sf` representing the
#' shoreline isobases. Defaults to isobases with a direction of 327 within the
#' spatial limit in SE Norway, but `create_isobases()` can be used to create
#' isobases with other directions that can then be passed to `target_plot()`.
#' @param basemap Object of class `sf` representing a background map. Defaults
#' to a light-weight basemap for the spatial limit in SE Norway.
#' @param crs_epsg Numeric value specifying the EPSG code of the coordinate
#' reference system (CRS) to be used. Geometries with a different CRS will be
#' re-projected. Defaults to 32632, which is WGS 84 / UTM zone 32N
#' (EPSG:32632).
#' @param naturalearth_basemap Logical value specifying if a background map
#' should be downloaded to be used as a basemap. Downloaded files are stored
#' with `base::tempdir()` and deleted when the R session is closed. If `TRUE`,
#' overrides the `basemap` argument. Defaults to `FALSE`.
#' @param naturalearth_zoom A vector of two numerical values specifying the
#' amount of cropping that is done around provided `targets` when
#' `naturalearth_basemap` is set to
#' `TRUE`. Be aware of whether a projected or geographical CRS is specified in
#' `crs_epsg`. Defaults to `c(20000, 20000)`.
#' @param target_labels Logical value specifying whether the targets should be
#' labelled in the plot. Takes the first column beyond the one holding the
#' geometries to represent names. If this is not present the targets are
#' labelled by row number. Defaults to `TRUE`.
#' @param scalebar Logical specifying whether a scale bar should be added to the
#' plot. Defaults to `TRUE`.
#' @param scalebar_width Numerical value specifying the width of the scale bar
#' by passing it to the `width_hint` argument of
#' `ggspatial::annotation:scale()`. Defaults to `0.4`.
#' @param scalebar_style Character value specifying the style of the scale bar
#' by passing it to the `style` argument of `ggspatial::annotation:scale()`.
#' Defaults to `"ticks"`.
#' @param scalebar_location Character value specifying the location of the
#' scale bar on the plot by passing it to the `location` argument of
#' `ggspatial::annotation:scale()`. Defaults to `"br"`.
#' @param base_fill Character value specifying the fill colour of the basemap.
#' Defaults to `"grey"`.
#' @param base_col Character value specifying the outline colour of the
#' basemap. Defaults to `NA`.
#' @param target_shape Numerical value specifying the point shape that represent
#' the centroids of the targets. Defaults to `21`.
#' @param target_col Character value specifying the colour parameter for the
#' points that represent the centroids of the targets. Defaults to `"black"`.
#' @param target_fill Character value specifying the fill parameter for the
#' points that represent the centroids of the targets. Defaults to `"red"`.
#' @param target_size Numerical value specifying the size of the points that
#' represent the centroids of the targets. Defaults to `2.25`.
#' @param isobase_line Vector of character values specifying the linetype that
#' is used to represent the isobases of the geologically derived displacement
#' curves. Defaults to `c("Horten" = "solid", "Porsgrunn" = "solid",
#' "Tvedestrand" = "solid", "Arendal" = "solid")`.`
#' @param isobase_col Vector of character values specifying the colours used
#' for the lines that represent the isobases of the geologically derived
#' displacement curves. Defaults to `c("Arendal" = "black",
#' "Porsgrunn" = "darkgreen", "Tvedestrand" = "blue",
#' "Horten" = "darkorange")`.`
#' @param greyscale Logical value indicating whether the plot should include
#' colours or not. Overrides other graphical parameters When set to `TRUE`.
#' Defaults to `FALSE`.
#'
#' @return A ggplot that displays a background map with the location of the
#' shoreline isobases within the spatial coverage in south-eastern Norway,
#' unless geometries for other regions are provided. If provided, the function
#' also plots the position of target locations represented as centroids.
#'
#' @export
#'
#' @examples
#' # Display the background map and default isobases
#' target_plot()
#'
target_plot <- function(targets = NA,
isobases = sf::st_read(
system.file("extdata/isobases.gpkg",
package = "shoredate",
mustWork = TRUE), quiet = TRUE),
basemap = sf::st_read(
system.file("extdata/naturalearth_basemap.gpkg",
package = "shoredate",
mustWork = TRUE), quiet = TRUE),
crs_epsg = 32632,
naturalearth_basemap = FALSE,
naturalearth_zoom = c(20000, 20000),
target_labels = TRUE,
scalebar = TRUE,
scalebar_width = 0.4,
scalebar_style = "ticks",
scalebar_location = "br",
base_fill = "grey",
base_col = NA,
target_shape = 21,
target_col = "black",
target_fill = "red",
target_size = 2.25,
isobase_line = c("Horten" = "solid",
"Porsgrunn" = "solid",
"Tvedestrand" = "solid",
"Arendal" = "solid"),
isobase_col = c("Arendal" = "black",
"Porsgrunn" = "darkgreen",
"Tvedestrand" = "blue",
"Horten" = "darkorange"),
greyscale = FALSE) {
if (greyscale) {
target_shape <- 16
target_col <- "black"
isobase_col <- c("Arendal" = "black",
"Porsgrunn" = "black",
"Tvedestrand" = "black",
"Horten" = "black")
isobase_line <- c("Horten" = "twodash",
"Porsgrunn" = "dashed",
"Tvedestrand" = "dotted",
"Arendal" = "longdash")
}
# Check that provided geometries have a CRS (sf would throw an error, but
# doesn't distinguish between mismatching and no CRS).
if (!all(is.na(basemap))) {
if (is.na(sf::st_crs(basemap))) {
stop("The provided basemap does not have a defined coordinate reference system.")
}
}
if (!all(is.na(isobases))) {
if (is.na(sf::st_crs(isobases))) {
stop("The provided isobases do not have a defined coordinate reference system.")
}
}
if (!all(is.na(targets))) {
if (is.na(sf::st_crs(targets))) {
stop("The provided targets do not have a defined coordinate reference system.")
}
}
if(naturalearth_basemap){
basemap <- rnaturalearth::ne_download(scale = "large",
type = "countries",
category = "cultural",
returnclass = "sf")
}
# Make sure CRS matches the crs_epsg argument
if (!all(is.na(basemap)) & crs_epsg != sf::st_crs(basemap)$epsg) {
warning(paste("Reprojecting basemap from CRS with EPSG code",
sf::st_crs(basemap)$epsg, "to EPSG", crs_epsg), ".")
basemap <- sf::st_transform(basemap, sf::st_crs(crs_epsg))
}
if (!all(is.na(isobases)) & crs_epsg != sf::st_crs(isobases)$epsg) {
warning(paste("Reprojecting isobases from CRS with EPSG code",
sf::st_crs(isobases)$epsg, "to EPSG", crs_epsg), ".")
isobases <- sf::st_transform(isobases, sf::st_crs(crs_epsg))
}
if (!all(is.na(targets)) & crs_epsg != sf::st_crs(targets)$epsg) {
warning(paste("Reprojecting targets from CRS with EPSG code",
sf::st_crs(targets)$epsg, "to EPSG", crs_epsg), ".")
targets <- sf::st_transform(targets, sf::st_crs(crs_epsg))
}
# Check that provided geometries intersect (except sites and isobases,
# which is not required). Give warning if not.
# If only one element is present, skip checks
if(sum(!all(is.na(basemap)),
!all(is.na(isobases)),
!all(is.na(targets))) > 1) {
# If basemap and isobases are provided
if (!all(is.na(basemap)) & !all(is.na(isobases))) {
if (!any(sf::st_intersects(basemap, isobases, sparse = FALSE))) {
warning("Basemap and isobases do not intersect.")
}
}
# If basemap and targets are provided
if (!all(is.na(basemap)) & !all(is.na(targets))) {
if (!any(sf::st_intersects(basemap, targets, sparse = FALSE))) {
warning("Basemap and targets do not intersect.")
}
}
}
# Create baseplot
plt <- ggplot2::ggplot() +
ggplot2::theme_bw() +
ggplot2::theme(
axis.title = ggplot2::element_blank(),
axis.text.y = ggplot2::element_blank(),
axis.text.x = ggplot2::element_blank(),
axis.ticks = ggplot2::element_blank(),
rect = ggplot2::element_rect(),
panel.grid.major = ggplot2::element_blank(),
legend.position = "none")
if(naturalearth_basemap & !any(is.na(naturalearth_zoom))){
targetsbbox <- sf::st_bbox(targets)
targetsbbox[1:2] <- targetsbbox[1:2] - naturalearth_zoom[1]
targetsbbox[3:4] <- targetsbbox[3:4] + naturalearth_zoom[2]
croppoly <- sf::st_as_sf(sf::st_as_sfc(targetsbbox))
# Suppress warning on attributes from sf
basemap <- suppressWarnings(sf::st_crop(basemap, croppoly))
# plt <- plt +
# ggplot2::coord_sf(xlim = c(targetsbbox[1], targetsbbox[3]),
# ylim = c(targetsbbox[2], targetsbbox[4]),
# expand = FALSE, sf::st_crs(crs_epsg))
}
# Add basemap, if not set to NA
if (!all(is.na(basemap))) {
plt <- plt + ggplot2::geom_sf(data = basemap,
fill = base_fill,
colour = base_col)
}
# Add isobases, if not set to NA
if (!all(is.na(isobases))) {
plt <- plt + ggplot2::geom_sf(data = isobases,
ggplot2::aes(colour = .data$name,
linetype = .data$name)) +
ggplot2::scale_colour_manual(values = isobase_col) +
ggplot2::scale_linetype_manual(values = isobase_line)
}
# If targets are provided
if (!all(is.na(targets))) {
# Make sure the geometries are represented as a sf data frame,
# and not for example sfc
if (!inherits(targets, c("sf", "data.frame"))) {
targets <- sf::st_as_sf(targets, crs = sf::st_crs(targets))
}
# Make sure the column representing geometries is present and named "geom"
if(!("geom" %in% names(targets))){
targets <- sf::st_set_geometry(targets, sf::st_geometry(targets))
# sf::st_geometry(targets) <- "geom"
}
# Add target centroids, suppressing the warning from sf::st_centroid()
plt <- plt +
ggplot2::geom_sf(data = suppressWarnings(sf::st_centroid(targets)),
fill = target_fill,
size = target_size,
shape = target_shape,
colour = target_col)
# Add labels
if (target_labels) {
# If only a single column is present, name the targets by row number
if (ncol(targets) == 1) {
targets$site_name <- as.character(1:nrow(targets))
# If there are more columns present, take the first one to represent names
} else {
targets$site_name <- as.character(sf::st_drop_geometry(targets)[, 1])
}
plt <- plt + ggrepel::geom_text_repel(
data = targets,
ggplot2::aes(label = .data$site_name,
geometry = sf::st_geometry(targets)),
stat = "sf_coordinates")
}
}
# Unless naturalearth background map is provided, create bounding box for
# constraining the plot
if (!all(is.na(basemap))) {
base_bbox <- sf::st_bbox(basemap)
# Adjust to bounding box
plt <- plt +
ggplot2::coord_sf(xlim = c(base_bbox[1], base_bbox[3]),
ylim = c(base_bbox[2], base_bbox[4]),
expand = FALSE,
datum = sf::st_crs(crs_epsg))
}
# Add scale bar
if(scalebar) {
plt <- plt + ggspatial::annotation_scale(
location = scalebar_location,
width_hint = scalebar_width,
style = scalebar_style)
}
plt
}