Skip to content

Commit a637fbf

Browse files
authored
Don't modify legend key settings in geom_sf() during plot build. (#3942)
* fix #3941. * fix typo
1 parent a7b1520 commit a637fbf

File tree

1 file changed

+14
-5
lines changed

1 file changed

+14
-5
lines changed

R/layer-sf.R

Lines changed: 14 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -13,11 +13,21 @@ layer_sf <- function(geom = NULL, stat = NULL,
1313
position = NULL, params = list(),
1414
inherit.aes = TRUE, check.aes = TRUE, check.param = TRUE,
1515
show.legend = NA) {
16+
if (is.character(show.legend)) {
17+
legend_key_type <- show.legend
18+
show.legend <- TRUE
19+
} else {
20+
legend_key_type <- NULL
21+
}
22+
23+
# inherit from LayerSf class to add `legend_key_type` slot
24+
layer_class <- ggproto(NULL, LayerSf, legend_key_type = legend_key_type)
25+
1626
layer(
1727
geom = geom, stat = stat, data = data, mapping = mapping,
1828
position = position, params = params, inherit.aes = inherit.aes,
1929
check.aes = check.aes, check.param = check.param,
20-
show.legend = show.legend, layer_class = LayerSf
30+
show.legend = show.legend, layer_class = layer_class
2131
)
2232
}
2333

@@ -37,7 +47,7 @@ LayerSf <- ggproto("LayerSf", Layer,
3747
}
3848

3949
# automatically determine the legend type
40-
if (is.na(self$show.legend) || isTRUE(self$show.legend)) {
50+
if (is.null(self$legend_key_type)) {
4151
if (is_sf(data)) {
4252
sf_type <- detect_sf_type(data)
4353
if (sf_type == "point") {
@@ -48,9 +58,8 @@ LayerSf <- ggproto("LayerSf", Layer,
4858
self$geom_params$legend <- "polygon"
4959
}
5060
}
51-
} else if (is.character(self$show.legend)) {
52-
self$geom_params$legend <- self$show.legend
53-
self$show.legend <- TRUE
61+
} else {
62+
self$geom_params$legend <- self$legend_key_type
5463
}
5564
data
5665
}

0 commit comments

Comments
 (0)