-
Notifications
You must be signed in to change notification settings - Fork 2.1k
Enable geom_sf to automatically determine the legend type #3646
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Changes from 7 commits
2fd47b8
40bdc73
346a8f0
f8a85ef
68a3599
5927cc9
c6f6565
95e29fa
465f623
e1d3de5
60ed8db
34dee47
4313b5e
a8e1e3a
78e5fde
6dfb21f
c8e633a
841c3fb
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
Original file line number | Diff line number | Diff line change | ||||||||||||||
---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|
|
@@ -35,6 +35,18 @@ LayerSf <- ggproto("LayerSf", Layer, | |||||||||||||||
self$mapping$geometry <- as.name(geometry_col) | ||||||||||||||||
} | ||||||||||||||||
} | ||||||||||||||||
|
||||||||||||||||
# automatically determine the legend type | ||||||||||||||||
if (is.na(self$show.legend) || isTRUE(self$show.legend)) { | ||||||||||||||||
if (is_sf(data)) { | ||||||||||||||||
if (sf_geometry_type(data) %in% c("POINT", "MULTIPOINT")) | ||||||||||||||||
self$geom_params$legend <- "point" | ||||||||||||||||
else if (sf_geometry_type(data) %in% c("LINESTRING", "MULTILINESTRING", | ||||||||||||||||
"CIRCULARSTRING", "COMPOUNDCURVE", | ||||||||||||||||
"CURVE", "MULTICURVE")) | ||||||||||||||||
self$geom_params$legend <- "line" | ||||||||||||||||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Probably you can use Lines 300 to 306 in 6424808
|
||||||||||||||||
} | ||||||||||||||||
} | ||||||||||||||||
data | ||||||||||||||||
} | ||||||||||||||||
) | ||||||||||||||||
|
@@ -62,3 +74,9 @@ is_sf <- function(data) { | |||||||||||||||
#' @export | ||||||||||||||||
scale_type.sfc <- function(x) "identity" | ||||||||||||||||
|
||||||||||||||||
# helper function to determine the geometry type of sf object | ||||||||||||||||
sf_geometry_type <- function(sf) { | ||||||||||||||||
geometry_type <- unique(as.vector(sf::st_geometry_type(sf))) | ||||||||||||||||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Why |
||||||||||||||||
if (length(geometry_type) != 1) geometry_type <- "blend" | ||||||||||||||||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. I still don't like |
||||||||||||||||
geometry_type | ||||||||||||||||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. I think this function should return |
||||||||||||||||
} |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
You're calling
sf_geometry_type()
twice. Better to call it once, assign the result to a temp variable, and use that in theif
statements.There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I am sorry that I can not express all my thanks in English! I feel that I am a young pupil and you are my teacher who teaches me hand by hand. Thanks!