@@ -13,11 +13,21 @@ layer_sf <- function(geom = NULL, stat = NULL,
13
13
position = NULL , params = list (),
14
14
inherit.aes = TRUE , check.aes = TRUE , check.param = TRUE ,
15
15
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
+
16
26
layer(
17
27
geom = geom , stat = stat , data = data , mapping = mapping ,
18
28
position = position , params = params , inherit.aes = inherit.aes ,
19
29
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
21
31
)
22
32
}
23
33
@@ -37,7 +47,7 @@ LayerSf <- ggproto("LayerSf", Layer,
37
47
}
38
48
39
49
# automatically determine the legend type
40
- if (is.na (self $ show.legend ) || isTRUE( self $ show.legend )) {
50
+ if (is.null (self $ legend_key_type )) {
41
51
if (is_sf(data )) {
42
52
sf_type <- detect_sf_type(data )
43
53
if (sf_type == " point" ) {
@@ -48,9 +58,8 @@ LayerSf <- ggproto("LayerSf", Layer,
48
58
self $ geom_params $ legend <- " polygon"
49
59
}
50
60
}
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
54
63
}
55
64
data
56
65
}
0 commit comments