Skip to content

Commit ea279b0

Browse files
authored
Add layer_sf() constructor. (#3246)
* Add layer_sf() constructor. Closes #3232. * fix typo * update news item
1 parent 9209970 commit ea279b0

File tree

7 files changed

+106
-19
lines changed

7 files changed

+106
-19
lines changed

NAMESPACE

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -379,6 +379,7 @@ export(layer)
379379
export(layer_data)
380380
export(layer_grob)
381381
export(layer_scales)
382+
export(layer_sf)
382383
export(lims)
383384
export(map_data)
384385
export(margin)

NEWS.md

Lines changed: 8 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -42,8 +42,14 @@ core developer team.
4242
very beginning of the plot building process and which has access to the
4343
original input data and the plot object being built. This function allows the
4444
creation of custom layers that autogenerate aesthetic mappings based on the
45-
input data or that filter the input data in some form. This is mainly of
46-
interest to extension developers (@clauswilke, #2872).
45+
input data or that filter the input data in some form. For the time being, this
46+
feature is not exported, but it has enabled the development of a new layer type,
47+
`layer_sf()` (see next item). Other special-purpose layer types may be added
48+
in the future (@clauswilke, #2872).
49+
50+
* A new layer type `layer_sf()` can auto-detect and auto-map sf geometry
51+
columns in the data. It should be used by extension developers who are writing
52+
new sf-based geoms or stats (@clauswilke, #3232).
4753

4854
* `x0` and `y0` are now recognized positional aesthetics so they will get scaled
4955
if used in extension geoms and stats (@thomasp85, #3168)

R/geom-sf.R

Lines changed: 6 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -170,7 +170,7 @@ geom_sf <- function(mapping = aes(), data = NULL, stat = "sf",
170170
position = "identity", na.rm = FALSE, show.legend = NA,
171171
inherit.aes = TRUE, ...) {
172172
c(
173-
layer(
173+
layer_sf(
174174
geom = GeomSf,
175175
data = data,
176176
mapping = mapping,
@@ -182,8 +182,7 @@ geom_sf <- function(mapping = aes(), data = NULL, stat = "sf",
182182
na.rm = na.rm,
183183
legend = if (is.character(show.legend)) show.legend else "polygon",
184184
...
185-
),
186-
layer_class = LayerSf
185+
)
187186
),
188187
coord_sf(default = TRUE)
189188
)
@@ -215,7 +214,7 @@ geom_sf_label <- function(mapping = aes(), data = NULL,
215214
position <- position_nudge(nudge_x, nudge_y)
216215
}
217216

218-
layer(
217+
layer_sf(
219218
data = data,
220219
mapping = mapping,
221220
stat = stat,
@@ -231,8 +230,7 @@ geom_sf_label <- function(mapping = aes(), data = NULL,
231230
na.rm = na.rm,
232231
fun.geometry = fun.geometry,
233232
...
234-
),
235-
layer_class = LayerSf
233+
)
236234
)
237235
}
238236

@@ -260,7 +258,7 @@ geom_sf_text <- function(mapping = aes(), data = NULL,
260258
position <- position_nudge(nudge_x, nudge_y)
261259
}
262260

263-
layer(
261+
layer_sf(
264262
data = data,
265263
mapping = mapping,
266264
stat = stat,
@@ -274,8 +272,7 @@ geom_sf_text <- function(mapping = aes(), data = NULL,
274272
na.rm = na.rm,
275273
fun.geometry = fun.geometry,
276274
...
277-
),
278-
layer_class = LayerSf
275+
)
279276
)
280277
}
281278

R/layer-sf.R

Lines changed: 21 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,25 @@
1+
#' Create a new sf layer that auto-maps geometry data
2+
#'
3+
#' The `layer_sf()` function is a variant of [`layer()`] meant to be used by
4+
#' extension developers who are writing new sf-based geoms or stats.
5+
#' The sf layer checks whether the data contains a geometry column, and
6+
#' if one is found it is automatically mapped to the `geometry` aesthetic.
17
#' @include layer.r
2-
3-
# A special sf layer that auto-maps geometry data to the `geometry` aesthetic
8+
#' @inheritParams layer
9+
#' @keywords internal
10+
#' @export
11+
layer_sf <- function(geom = NULL, stat = NULL,
12+
data = NULL, mapping = NULL,
13+
position = NULL, params = list(),
14+
inherit.aes = TRUE, check.aes = TRUE, check.param = TRUE,
15+
show.legend = NA) {
16+
layer(
17+
geom = geom, stat = stat, data = data, mapping = mapping,
18+
position = position, params = params, inherit.aes = inherit.aes,
19+
check.aes = check.aes, check.param = check.param,
20+
show.legend = show.legend, layer_class = LayerSf
21+
)
22+
}
423

524
LayerSf <- ggproto("LayerSf", Layer,
625
setup_layer = function(self, data, plot) {

R/stat-sf-coordinates.R

Lines changed: 2 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -62,7 +62,7 @@ stat_sf_coordinates <- function(mapping = aes(), data = NULL, geom = "point",
6262
show.legend = NA, inherit.aes = TRUE,
6363
fun.geometry = NULL,
6464
...) {
65-
layer(
65+
layer_sf(
6666
stat = StatSfCoordinates,
6767
data = data,
6868
mapping = mapping,
@@ -74,8 +74,7 @@ stat_sf_coordinates <- function(mapping = aes(), data = NULL, geom = "point",
7474
na.rm = na.rm,
7575
fun.geometry = fun.geometry,
7676
...
77-
),
78-
layer_class = LayerSf
77+
)
7978
)
8079
}
8180

R/stat-sf.R

Lines changed: 2 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -22,7 +22,7 @@ StatSf <- ggproto("StatSf", Stat,
2222
stat_sf <- function(mapping = NULL, data = NULL, geom = "rect",
2323
position = "identity", na.rm = FALSE, show.legend = NA,
2424
inherit.aes = TRUE, ...) {
25-
layer(
25+
layer_sf(
2626
stat = StatSf,
2727
data = data,
2828
mapping = mapping,
@@ -34,8 +34,7 @@ stat_sf <- function(mapping = NULL, data = NULL, geom = "rect",
3434
na.rm = na.rm,
3535
legend = if (is.character(show.legend)) show.legend else "polygon",
3636
...
37-
),
38-
layer_class = LayerSf
37+
)
3938
)
4039
}
4140

man/layer_sf.Rd

Lines changed: 66 additions & 0 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

0 commit comments

Comments
 (0)