-
Notifications
You must be signed in to change notification settings - Fork 1
/
layer_neatline.R
306 lines (287 loc) · 10.5 KB
/
layer_neatline.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
#' Set map limits to a bounding box with a buffer and set aspect ratio
#'
#' Set limits for a map to the bounding box of a feature using
#' [ggplot2::coord_sf()]. Optionally, adjust the x size by applying a buffer
#' and/or adjust the aspect ratio of the limiting bounding box to match a set
#' aspect ratio.
#'
#' @param data A `sf`, `sfc`, or `bbox` class object.
#' @param linewidth Line width of panel border, Default: 0.5
#' @param color Color of panel border, Default: 'black'
#' @param bgcolor Fill color of panel background; defaults to "white". If
#' "none", panel background is set to [ggplot2::element_blank()]
#' @param linetype Line type of panel border, Default: 'solid'
#' @param hide_grid If `TRUE`, hide grid lines. Default: `TRUE`
#' @param label_axes A description of which axes to label passed to
#' [ggplot2::coord_sf()]; defaults to '----' which hides all axes labels.
#' @param axis.title,axis.text,axis.ticks,axis.ticks.length,axis.line Theme
#' elements passed as is if label_axes is anything other than "----".
#' @param panel.grid,panel.grid.major,panel.grid.minor Passed as is if hide_grid
#' is FALSE.
#' @param panel.border,panel.background,plot.background,plot.margin panel.border
#' is used as is if not NULL or `ggplot2::element_blank()` if it is NULL
#' unless color is NA or "none". panel.background and plot.background are used
#' as is or `ggplot2::element_blank()` if bg color is NA or "none".
#' plot.margin is set to `ggplot2::margin(1, 1, 1, 1)` if `NULL` or
#' `ggplot2::margin(0, 0, 0, 0)` if expand is `FALSE`.
#' @param ... Additional parameters passed to [ggplot2::coord_sf()].
#' @inheritParams ggplot2::coord_sf
#' @inheritParams sfext::st_bbox_ext
#' @param default_plot_margin Defaults to `ggplot2::margin(1, 1, 1, 1)`. Ignored
#' if `expand = FALSE`
#' @return List of [ggplot2::coord_sf] and [ggplot2::theme] calls.
#' @example examples/layer_neatline.R
#' @seealso
#' [ggplot2::CoordSf()]
#' @family layer
#' @aliases set_map_limits
#' @export
#' @importFrom ggplot2 coord_sf scale_y_continuous scale_x_continuous theme
#' element_rect element_blank
layer_neatline <- function(data = NULL,
dist = getOption("maplayer.dist"),
diag_ratio = getOption("maplayer.diag_ratio"),
unit = getOption("maplayer.unit", default = "meter"),
asp = getOption("maplayer.asp"),
crs = getOption("maplayer.crs"),
nudge = getOption("maplayer.nudge"),
color = "black",
linewidth = 0.5,
linetype = "solid",
bgcolor = "white",
expand = TRUE,
hide_grid = TRUE,
label_axes = "----",
axis.title = NULL,
axis.text = NULL,
axis.ticks = NULL,
axis.ticks.length = ggplot2::unit(x = 0, units = "mm"),
axis.line = NULL,
panel.grid = NULL,
panel.grid.major = NULL,
panel.grid.minor = NULL,
panel.border = NULL,
panel.background = NULL,
plot.background = NULL,
plot.margin = NULL,
default_plot_margin = ggplot2::margin(1, 1, 1, 1),
xlim = NULL,
ylim = NULL,
...) {
xy_lims <- set_xy_lims(data, dist, diag_ratio, unit, asp, crs, nudge, xlim, ylim)
list(
# Set limits with adjustments using coord_sf
ggplot2::coord_sf(
xlim = xy_lims$xlim,
ylim = xy_lims$ylim,
expand = expand,
crs = crs,
label_axes = label_axes,
# Suppress warning about coordinate system being replaced
default = TRUE,
...
),
theme_grid(
hide_grid,
panel.grid = panel.grid,
panel.grid.major = panel.grid.major,
panel.grid.minor = panel.grid.minor
),
theme_background(
color,
linewidth,
linetype,
bgcolor,
expand,
panel.border = panel.border,
panel.background = panel.background,
plot.background = plot.background,
plot.margin = plot.margin
),
theme_sf_axis(
label_axes,
axis.title = axis.title,
axis.text = axis.text,
axis.ticks = axis.ticks,
axis.ticks.length = axis.ticks.length,
axis.line = axis.line
)
)
}
#' Set xlim and ylim based on data
#'
#' @noRd
#' @importFrom sfext st_bbox_ext
set_xy_lims <- function(data = NULL,
dist = getOption("maplayer.dist"),
diag_ratio = getOption("maplayer.diag_ratio"),
unit = getOption("maplayer.unit", default = "meter"),
asp = getOption("maplayer.asp"),
crs = getOption("maplayer.crs"),
nudge = getOption("maplayer.nudge"),
xlim = NULL,
ylim = NULL) {
if (!is.null(data)) {
bbox <- sfext::st_bbox_ext(
x = data,
dist = dist,
diag_ratio = diag_ratio,
unit = unit,
asp = asp,
crs = crs,
nudge = nudge
)
xlim <- xlim %||% c(bbox[["xmin"]], bbox[["xmax"]])
ylim <- ylim %||% c(bbox[["ymin"]], bbox[["ymax"]])
}
list(
"xlim" = xlim,
"ylim" = ylim
)
}
#' [theme_grid()] can hide (or set) panel grid theme elements based on the
#' `hide_grid` or `grid` parameters
#'
#' @rdname layer_neatline
#' @name theme_grid
#' @export
#' @importFrom ggplot2 theme element_blank
theme_grid <- function(hide_grid = TRUE,
grid = FALSE,
panel.grid = NULL,
panel.grid.major = NULL,
panel.grid.minor = NULL) {
if (hide_grid || !grid) {
return(
ggplot2::theme(
panel.grid = ggplot2::element_blank(),
panel.grid.major = ggplot2::element_blank(),
panel.grid.minor = ggplot2::element_blank()
)
)
}
ggplot2::theme(
panel.grid = panel.grid,
panel.grid.major = panel.grid.major,
panel.grid.minor = panel.grid.minor
)
}
#' [theme_sf_axis()] can hide (or set) axis theme elements based on the
#' `label_axes` parameter
#'
#' @name theme_sf_axis
#' @rdname layer_neatline
#' @export
#' @importFrom ggplot2 theme element_blank unit
theme_sf_axis <- function(label_axes = "----",
axis.title = NULL,
axis.text = NULL,
axis.text.x = NULL,
axis.text.y = NULL,
axis.ticks = NULL,
axis.ticks.length = ggplot2::unit(x = 0, units = "mm"),
axis.line = NULL,
...) {
if (label_axes == "----") {
return(
ggplot2::theme(
axis.title = ggplot2::element_blank(),
axis.text = ggplot2::element_blank(),
axis.text.x = ggplot2::element_blank(),
axis.text.y = ggplot2::element_blank(),
axis.ticks = ggplot2::element_blank(),
axis.ticks.length = ggplot2::unit(x = 0, units = "mm"),
axis.line = ggplot2::element_blank()
)
)
}
ggplot2::theme(
axis.title = axis.title,
axis.text = axis.text,
axis.text.x = axis.text.x,
axis.text.y = axis.text.y,
axis.ticks = axis.ticks,
axis.ticks.length = axis.ticks.length,
axis.line = axis.line,
...
)
}
#' Set ggplot2 panel border, panel background, and plot background theme
#' elements
#'
#' @noRd
#' @importFrom ggplot2 theme element_blank element_rect
theme_background <- function(color = "black",
linewidth = 0.5,
linetype = "solid",
bgcolor = "white",
expand = TRUE,
plot.background = NULL,
plot.margin = NULL,
panel.border = NULL,
panel.background = NULL,
default_plot_margin = ggplot2::margin(1, 1, 1, 1)) {
panel.border <- panel.border %||% ggplot2::element_blank()
panel.background <- panel.background %||% ggplot2::element_blank()
plot.background <- plot.background %||% ggplot2::element_blank()
plot.margin <- plot.margin %||% default_plot_margin
if (!is.na(color) && (color != "none")) {
panel.border <- ggplot2::element_rect(
color = color, linewidth = linewidth,
linetype = linetype, fill = NA
)
}
if (!is.na(bgcolor) && bgcolor != "none") {
panel.background <- ggplot2::element_rect(fill = bgcolor, color = bgcolor)
plot.background <- ggplot2::element_rect(fill = bgcolor, color = bgcolor)
}
if (!expand) {
plot.margin <- ggplot2::margin(0, 0, 0, 0)
}
ggplot2::theme(
panel.border = panel.border,
panel.background = panel.background,
plot.background = plot.background,
plot.margin = plot.margin
)
}
#' @name set_neatline
#' @rdname layer_neatline
#' @param x For [set_neatline()], a `ggplot` class object, `ggproto` class
#' object or list of `ggproto` objects to combine with neatline layer.
#' @param neatline A logical object, `CoordSf` object, or a list containing a
#' `CoordSf` object (typically from [layer_neatline()]) added to layer by
#' [set_neatline()].
#'
#' - If logical and `TRUE`, add a neatline layer using data, crs and any
#' additional parameters passed to ... If logical and `FALSE`, return x as is.
#' - If object from [layer_neatline()], add it as is.
#' @export
#' @importFrom dplyr case_when
#' @importFrom rlang is_logical
#' @importFrom ggplot2 is.ggplot
set_neatline <- function(x = NULL,
neatline = TRUE,
data = NULL,
crs = NULL,
...) {
if (is_null(x) || is_false(neatline) || is_null(neatline)) {
return(x)
}
if (!is_logical(neatline) && !is_neatline(neatline)) {
cli::cli_abort(
c("{.arg neatline} must be {.cls logical}, a {.cls {c('Coord', 'ggproto')}} object,
or list of {.cls {c('Coord', 'ggproto')}} objects.",
"i" = "The class of the provided {.arg neatline} is {class(neatline)}."
)
)
}
if (is_true(neatline)) {
neatline <- layer_neatline(
data = data,
crs = crs,
...
)
}
combine_gg_list(x, neatline)
}