Skip to content

Commit e7d05ea

Browse files
committed
specify graticule labeling via string
1 parent de4266c commit e7d05ea

File tree

2 files changed

+34
-12
lines changed

2 files changed

+34
-12
lines changed

R/sf.R

Lines changed: 25 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -504,20 +504,34 @@ sf_rescale01_x <- function(x, range) {
504504
#' @param crs Use this to select a specific coordinate reference system (CRS).
505505
#' If not specified, will use the CRS defined in the first layer.
506506
#' @param datum CRS that provides datum to use when generating graticules
507-
#' @param graticule_labeling Named list of character values specifying which
508-
#' graticules (meridians or parallels) should be labeled on which side of the
509-
#' plot. Meridians are indicated by `"E"` (for East) and parallels by `"N"`
510-
#' (for North). Default is `list(top = NA, right = NA, bottom = "E",
511-
#' left = "N")` to label parallels on the left and meridians at the bottom.
507+
#' @param graticule_labeling Character vector or named list of character values
508+
#' specifying which graticules (meridians or parallels) should be labeled on
509+
#' which side of the plot. Meridians are indicated by `"E"` (for East) and
510+
#' parallels by `"N"` (for North). Default is `"NE--"`, which specifies
511+
#' (counter-clockwise from the left) paralleles on the left, meridians on the bottom,
512+
#' and nothing on the right or top. Alternatively, this setting could have been
513+
#' specified with `list(left = "N", bottom = "E", right = NA, top = NA)`.
512514
#' @param ndiscr number of segments to use for discretising graticule lines;
513515
#' try increasing this when graticules look unexpected
514516
#' @inheritParams coord_cartesian
515517
#' @export
516518
#' @rdname ggsf
517519
coord_sf <- function(xlim = NULL, ylim = NULL, expand = TRUE,
518520
crs = NULL, datum = sf::st_crs(4326),
519-
graticule_labeling = list(top = NA, right = NA, bottom = "E", left = "N"),
521+
graticule_labeling = "NE--",
520522
ndiscr = 100, default = FALSE) {
523+
524+
# graticule labeling can be specified via string or named list
525+
if (is.character(graticule_labeling)) {
526+
graticule_labeling <- parse_graticule_labeling(graticule_labeling)
527+
} else if (!is.list(graticule_labeling)) {
528+
warning(
529+
"Graticule labeling format not recognized. Proceeding with default settings.",
530+
call. = FALSE
531+
)
532+
graticule_labeling <- list(left = "N", bottom = "E")
533+
}
534+
521535
ggproto(NULL, CoordSf,
522536
limits = list(x = xlim, y = ylim),
523537
datum = datum,
@@ -528,3 +542,8 @@ coord_sf <- function(xlim = NULL, ylim = NULL, expand = TRUE,
528542
default = default
529543
)
530544
}
545+
546+
parse_graticule_labeling <- function(x) {
547+
labs = unlist(strsplit(x, ""))
548+
list(left = labs[1], bottom = labs[2], right = labs[3], top = labs[4])
549+
}

man/ggsf.Rd

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

0 commit comments

Comments
 (0)