Skip to content

Commit 465f623

Browse files
committed
I think it works! Need to clean code comments.
1 parent 95e29fa commit 465f623

File tree

4 files changed

+19
-11
lines changed

4 files changed

+19
-11
lines changed

R/geom-sf.R

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -197,11 +197,12 @@ geom_sf <- function(mapping = aes(), data = NULL, stat = "sf",
197197
mapping = mapping,
198198
stat = stat,
199199
position = position,
200-
show.legend = if (is.character(show.legend)) TRUE else show.legend,
200+
#show.legend = if (is.character(show.legend)) TRUE else show.legend,
201+
show.legend = show.legend,
201202
inherit.aes = inherit.aes,
202203
params = list(
203204
na.rm = na.rm,
204-
legend = if (is.character(show.legend)) show.legend else "polygon",
205+
#legend = if (is.character(show.legend)) show.legend else "polygon",
205206
...
206207
)
207208
),

R/layer-sf.R

Lines changed: 7 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -40,13 +40,15 @@ LayerSf <- ggproto("LayerSf", Layer,
4040
if (is.na(self$show.legend) || isTRUE(self$show.legend)) {
4141
if (is_sf(data)) {
4242
geometry_type <- sf_geometry_type(data)
43-
if (geometry_type %in% c("POINT", "MULTIPOINT"))
43+
if (sf_types[geometry_type] == "point")
4444
self$geom_params$legend <- "point"
45-
else if (geometry_type %in% c("LINESTRING", "MULTILINESTRING",
46-
"CIRCULARSTRING", "COMPOUNDCURVE",
47-
"CURVE", "MULTICURVE"))
45+
else if (sf_types[geometry_type] == "line")
4846
self$geom_params$legend <- "line"
47+
else self$geom_params$legend <- "polygon"
4948
}
49+
} else if (is.character(self$show.legend)) {
50+
self$geom_params$legend = self$show.legend
51+
self$show.legend = TRUE
5052
}
5153
data
5254
}
@@ -78,6 +80,6 @@ scale_type.sfc <- function(x) "identity"
7880
# helper function to determine the geometry type of sf object
7981
sf_geometry_type <- function(sf) {
8082
geometry_type <- unique(as.character(sf::st_geometry_type(sf)))
81-
if (length(geometry_type) != 1) geometry_type <- "other"
83+
if (length(geometry_type) != 1) geometry_type <- "GEOMETRY"
8284
geometry_type
8385
}

R/layer.r

Lines changed: 6 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -81,8 +81,12 @@ layer <- function(geom = NULL, stat = NULL,
8181
params$show_guide <- NULL
8282
}
8383
if (!is.logical(show.legend)) {
84-
warning("`show.legend` must be a logical vector.", call. = FALSE)
85-
show.legend <- FALSE
84+
# a charater vector have to be allowed for LayerSf$show.legend
85+
# in order to set its legend type.
86+
if (!inherits(layer_class, "LayerSf")) {
87+
warning("`show.legend` must be a logical vector.", call. = FALSE)
88+
show.legend <- FALSE
89+
}
8690
}
8791

8892
# we validate mapping before data because in geoms and stats

R/stat-sf.R

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -28,11 +28,12 @@ stat_sf <- function(mapping = NULL, data = NULL, geom = "rect",
2828
mapping = mapping,
2929
geom = geom,
3030
position = position,
31-
show.legend = if (is.character(show.legend)) TRUE else show.legend,
31+
#show.legend = if (is.character(show.legend)) TRUE else show.legend,
32+
show.legend = show.legend,
3233
inherit.aes = inherit.aes,
3334
params = list(
3435
na.rm = na.rm,
35-
legend = if (is.character(show.legend)) show.legend else "polygon",
36+
#legend = if (is.character(show.legend)) show.legend else "polygon",
3637
...
3738
)
3839
)

0 commit comments

Comments
 (0)