Skip to content

Commit b19fbf4

Browse files
committed
allow labeling by graticules and panel sides at the same time
1 parent 6e9e144 commit b19fbf4

File tree

2 files changed

+126
-57
lines changed

2 files changed

+126
-57
lines changed

R/sf.R

Lines changed: 104 additions & 50 deletions
Original file line numberDiff line numberDiff line change
@@ -354,7 +354,8 @@ CoordSf <- ggproto("CoordSf", CoordCartesian,
354354
y_range = y_range,
355355
graticule = graticule,
356356
crs = params$crs,
357-
graticule_labeling = self$graticule_labeling
357+
label_axes = self$label_axes,
358+
label_graticules = self$label_graticules
358359
)
359360
},
360361

@@ -393,12 +394,12 @@ CoordSf <- ggproto("CoordSf", CoordCartesian,
393394
graticule <- panel_params$graticule
394395

395396
# top axis
396-
if (identical(panel_params$graticule_labeling$top, "E") ||
397-
identical(panel_params$graticule_labeling$top, "N")) {
397+
if (identical(panel_params$label_axes$top, "E") ||
398+
identical(panel_params$label_axes$top, "N")) {
398399
# we don't generally know which direction graticules run, so need to consider both
399-
ticks1 <- graticule[graticule$type == panel_params$graticule_labeling$top &
400+
ticks1 <- graticule[graticule$type == panel_params$label_axes$top &
400401
graticule$y_start > 0.999, ]
401-
ticks2 <- graticule[graticule$type == panel_params$graticule_labeling$top &
402+
ticks2 <- graticule[graticule$type == panel_params$label_axes$top &
402403
graticule$y_end > 0.999, ]
403404
tick_positions <- c(ticks1$x_start, ticks2$x_end)
404405
tick_labels <- c(ticks1$degree_label, ticks2$degree_label)
@@ -414,12 +415,12 @@ CoordSf <- ggproto("CoordSf", CoordCartesian,
414415
}
415416

416417
# bottom axis
417-
if (identical(panel_params$graticule_labeling$bottom, "E") ||
418-
identical(panel_params$graticule_labeling$bottom, "N")) {
418+
if (identical(panel_params$label_axes$bottom, "E") ||
419+
identical(panel_params$label_axes$bottom, "N")) {
419420
# we don't generally know which direction graticules run, so need to consider both
420-
ticks1 <- graticule[graticule$type == panel_params$graticule_labeling$bottom &
421+
ticks1 <- graticule[graticule$type == panel_params$label_axes$bottom &
421422
graticule$y_start < 0.001, ]
422-
ticks2 <- graticule[graticule$type == panel_params$graticule_labeling$bottom &
423+
ticks2 <- graticule[graticule$type == panel_params$label_axes$bottom &
423424
graticule$y_end < 0.001, ]
424425
tick_positions <- c(ticks1$x_start, ticks2$x_end)
425426
tick_labels <- c(ticks1$degree_label, ticks2$degree_label)
@@ -442,12 +443,12 @@ CoordSf <- ggproto("CoordSf", CoordCartesian,
442443
graticule <- panel_params$graticule
443444

444445
# left axis
445-
if (identical(panel_params$graticule_labeling$left, "E") ||
446-
identical(panel_params$graticule_labeling$left, "N")) {
446+
if (identical(panel_params$label_axes$left, "E") ||
447+
identical(panel_params$label_axes$left, "N")) {
447448
# we don't generally know which direction graticules run, so need to consider both
448-
ticks1 <- graticule[graticule$type == panel_params$graticule_labeling$left &
449+
ticks1 <- graticule[graticule$type == panel_params$label_axes$left &
449450
graticule$x_start < 0.001, ]
450-
ticks2 <- graticule[graticule$type == panel_params$graticule_labeling$left &
451+
ticks2 <- graticule[graticule$type == panel_params$label_axes$left &
451452
graticule$x_end < 0.001, ]
452453
tick_positions <- c(ticks1$y_start, ticks2$y_end)
453454
tick_labels <- c(ticks1$degree_label, ticks2$degree_label)
@@ -463,12 +464,12 @@ CoordSf <- ggproto("CoordSf", CoordCartesian,
463464
}
464465

465466
# right axis
466-
if (identical(panel_params$graticule_labeling$right, "E") ||
467-
identical(panel_params$graticule_labeling$right, "N")) {
467+
if (identical(panel_params$label_axes$right, "E") ||
468+
identical(panel_params$label_axes$right, "N")) {
468469
# we don't generally know which direction graticules run, so need to consider both
469-
ticks1 <- graticule[graticule$type == panel_params$graticule_labeling$right &
470+
ticks1 <- graticule[graticule$type == panel_params$label_axes$right &
470471
graticule$x_start > 0.999, ]
471-
ticks2 <- graticule[graticule$type == panel_params$graticule_labeling$right &
472+
ticks2 <- graticule[graticule$type == panel_params$label_axes$right &
472473
graticule$x_end > 0.999, ]
473474
tick_positions <- c(ticks1$y_start, ticks2$y_end)
474475
tick_labels <- c(ticks1$degree_label, ticks2$degree_label)
@@ -491,16 +492,21 @@ CoordSf <- ggproto("CoordSf", CoordCartesian,
491492

492493
# top axis
493494
id1 <- id2 <- integer(0)
494-
if ("S" %in% panel_params$graticule_labeling) {
495+
# labels based on panel side
496+
id1 <- c(id1, which(graticule$type == panel_params$label_axes$top & graticule$y_start > 0.999))
497+
id2 <- c(id2, which(graticule$type == panel_params$label_axes$top & graticule$y_end > 0.999))
498+
499+
# labels based on graticule direction
500+
if ("S" %in% panel_params$label_graticules) {
495501
id1 <- c(id1, which(graticule$type == "E" & graticule$y_start > 0.999))
496502
}
497-
if ("N" %in% panel_params$graticule_labeling) {
503+
if ("N" %in% panel_params$label_graticules) {
498504
id2 <- c(id2, which(graticule$type == "E" & graticule$y_end > 0.999))
499505
}
500-
if ("W" %in% panel_params$graticule_labeling) {
506+
if ("W" %in% panel_params$label_graticules) {
501507
id1 <- c(id1, which(graticule$type == "N" & graticule$y_start > 0.999))
502508
}
503-
if ("E" %in% panel_params$graticule_labeling) {
509+
if ("E" %in% panel_params$label_graticules) {
504510
id2 <- c(id2, which(graticule$type == "N" & graticule$y_end > 0.999))
505511
}
506512

@@ -522,16 +528,21 @@ CoordSf <- ggproto("CoordSf", CoordCartesian,
522528

523529
# bottom axis
524530
id1 <- id2 <- integer(0)
525-
if ("S" %in% panel_params$graticule_labeling) {
531+
# labels based on panel side
532+
id1 <- c(id1, which(graticule$type == panel_params$label_axes$bottom & graticule$y_start < 0.001))
533+
id2 <- c(id2, which(graticule$type == panel_params$label_axes$bottom & graticule$y_end < 0.001))
534+
535+
# labels based on graticule direction
536+
if ("S" %in% panel_params$label_graticules) {
526537
id1 <- c(id1, which(graticule$type == "E" & graticule$y_start < 0.001))
527538
}
528-
if ("N" %in% panel_params$graticule_labeling) {
539+
if ("N" %in% panel_params$label_graticules) {
529540
id2 <- c(id2, which(graticule$type == "E" & graticule$y_end < 0.001))
530541
}
531-
if ("W" %in% panel_params$graticule_labeling) {
542+
if ("W" %in% panel_params$label_graticules) {
532543
id1 <- c(id1, which(graticule$type == "N" & graticule$y_start < 0.001))
533544
}
534-
if ("E" %in% panel_params$graticule_labeling) {
545+
if ("E" %in% panel_params$label_graticules) {
535546
id2 <- c(id2, which(graticule$type == "N" & graticule$y_end < 0.001))
536547
}
537548

@@ -559,16 +570,21 @@ CoordSf <- ggproto("CoordSf", CoordCartesian,
559570

560571
# right axis
561572
id1 <- id2 <- integer(0)
562-
if ("N" %in% panel_params$graticule_labeling) {
573+
# labels based on panel side
574+
id1 <- c(id1, which(graticule$type == panel_params$label_axes$right & graticule$x_end > 0.999))
575+
id2 <- c(id2, which(graticule$type == panel_params$label_axes$right & graticule$x_start > 0.999))
576+
577+
# labels based on graticule direction
578+
if ("N" %in% panel_params$label_graticules) {
563579
id1 <- c(id1, which(graticule$type == "E" & graticule$x_end > 0.999))
564580
}
565-
if ("S" %in% panel_params$graticule_labeling) {
581+
if ("S" %in% panel_params$label_graticules) {
566582
id2 <- c(id2, which(graticule$type == "E" & graticule$x_start > 0.999))
567583
}
568-
if ("E" %in% panel_params$graticule_labeling) {
584+
if ("E" %in% panel_params$label_graticules) {
569585
id1 <- c(id1, which(graticule$type == "N" & graticule$x_end > 0.999))
570586
}
571-
if ("W" %in% panel_params$graticule_labeling) {
587+
if ("W" %in% panel_params$label_graticules) {
572588
id2 <- c(id2, which(graticule$type == "N" & graticule$x_start > 0.999))
573589
}
574590

@@ -590,16 +606,21 @@ CoordSf <- ggproto("CoordSf", CoordCartesian,
590606

591607
# left axis
592608
id1 <- id2 <- integer(0)
593-
if ("N" %in% panel_params$graticule_labeling) {
609+
# labels based on panel side
610+
id1 <- c(id1, which(graticule$type == panel_params$label_axes$left & graticule$x_end < 0.001))
611+
id2 <- c(id2, which(graticule$type == panel_params$label_axes$left & graticule$x_start < 0.001))
612+
613+
# labels based on graticule direction
614+
if ("N" %in% panel_params$label_graticules) {
594615
id1 <- c(id1, which(graticule$type == "E" & graticule$x_end < 0.001))
595616
}
596-
if ("S" %in% panel_params$graticule_labeling) {
617+
if ("S" %in% panel_params$label_graticules) {
597618
id2 <- c(id2, which(graticule$type == "E" & graticule$x_start < 0.001))
598619
}
599-
if ("E" %in% panel_params$graticule_labeling) {
620+
if ("E" %in% panel_params$label_graticules) {
600621
id1 <- c(id1, which(graticule$type == "N" & graticule$x_end < 0.001))
601622
}
602-
if ("W" %in% panel_params$graticule_labeling) {
623+
if ("W" %in% panel_params$label_graticules) {
603624
id2 <- c(id2, which(graticule$type == "N" & graticule$x_start < 0.001))
604625
}
605626

@@ -642,49 +663,82 @@ sf_rescale01_x <- function(x, range) {
642663
#' @param crs Use this to select a specific coordinate reference system (CRS).
643664
#' If not specified, will use the CRS defined in the first layer.
644665
#' @param datum CRS that provides datum to use when generating graticules
645-
#' @param graticule_labeling Character vector or named list of character values
666+
#' @param label_axes Character vector or named list of character values
646667
#' specifying which graticules (meridians or parallels) should be labeled on
647668
#' which side of the plot. Meridians are indicated by `"E"` (for East) and
648-
#' parallels by `"N"` (for North). Default is `"NE--"`, which specifies
649-
#' (counter-clockwise from the left) paralleles on the left, meridians on the bottom,
650-
#' and nothing on the right or top. Alternatively, this setting could have been
651-
#' specified with `list(left = "N", bottom = "E", right = NA, top = NA)`.
669+
#' parallels by `"N"` (for North). Default is `"--EN"`, which specifies
670+
#' (clockwise from the top) no labels on the top, none on the right, meridians
671+
#' on the bottom, and paralleles on the left. Alternatively, this setting could have been
672+
#' specified with `list(left = "N", bottom = "E")`.
673+
#'
674+
#' This parameter can be used alone or in combination with `label_graticules`.
675+
#' @param label_axes Character vector indicating which graticules should be labeled
676+
#' where. Meridians run north-south, and the letters `"N"` and `"S"` indicate that
677+
#' they should be labeled on their north or south end points, respectively.
678+
#' Parallels run east-west, and the letters `"E"` and `"W"` indicate that they
679+
#' should be labeled on their east or west end points, respectively. Thus,
680+
#' `label_axes = "SW"` would label meridians at their south end and parallels at
681+
#' their west end, whereas `label_axes = "EW"` would label parallels at both
682+
#' ends and meridians not at all. Because meridians and parallels can in general
683+
#' intersect with any side of the plot panel, for any choice of `label_axes` labels
684+
#' are not guaranteed to reside on only one particular side of the plot panel.
685+
#'
686+
#' This parameter can be used alone or in combination with `label_axes`.
652687
#' @param ndiscr number of segments to use for discretising graticule lines;
653688
#' try increasing this when graticules look unexpected
654689
#' @inheritParams coord_cartesian
655690
#' @export
656691
#' @rdname ggsf
657692
coord_sf <- function(xlim = NULL, ylim = NULL, expand = TRUE,
658693
crs = NULL, datum = sf::st_crs(4326),
659-
graticule_labeling = "NW",
694+
label_graticules = waiver(),
695+
label_axes = waiver(),
660696
ndiscr = 100, default = FALSE) {
661697

662-
if (FALSE) { # commented out for now
663-
# graticule labeling can be specified via string or named list
664-
if (is.character(graticule_labeling)) {
665-
graticule_labeling <- parse_graticule_labeling(graticule_labeling)
666-
} else if (!is.list(graticule_labeling)) {
698+
if (is.waive(label_graticules) && is.waive(label_axes)) {
699+
# if both `label_graticules` and `label_axes` are set to waive then we
700+
# use the default of labels on the left and at the bottom
701+
label_graticules <- ""
702+
label_axes <- "--EN"
703+
} else {
704+
# if at least one is set we ignore the other
705+
label_graticules <- label_graticules %|W|% ""
706+
label_axes <- label_axes %|W|% ""
707+
}
708+
709+
if (is.character(label_axes)) {
710+
label_axes <- parse_axes_labeling(label_axes)
711+
} else if (!is.list(label_axes)) {
667712
warning(
668-
"Graticule labeling format not recognized. Proceeding with default settings.",
713+
"Panel labeling format not recognized. Proceeding with default settings.",
669714
call. = FALSE
670715
)
671-
graticule_labeling <- list(left = "N", bottom = "E")
716+
label_axes <- list(left = "N", bottom = "E")
672717
}
718+
719+
if (is.character(label_graticules)) {
720+
label_graticules <- unlist(strsplit(label_graticules, ""))
721+
} else {
722+
warning(
723+
"Graticule labeling format not recognized. Proceeding with default settings.",
724+
call. = FALSE
725+
)
726+
label_graticules <- ""
673727
}
674-
graticule_labeling <- unlist(strsplit(graticule_labeling, ""))
675728

676729
ggproto(NULL, CoordSf,
677730
limits = list(x = xlim, y = ylim),
678731
datum = datum,
679732
crs = crs,
680-
graticule_labeling = graticule_labeling,
733+
label_axes = label_axes,
734+
label_graticules = label_graticules,
681735
ndiscr = ndiscr,
682736
expand = expand,
683737
default = default
684738
)
685739
}
686740

687-
parse_graticule_labeling <- function(x) {
741+
parse_axes_labeling <- function(x) {
688742
labs = unlist(strsplit(x, ""))
689-
list(left = labs[1], bottom = labs[2], right = labs[3], top = labs[4])
743+
list(top = labs[1], right = labs[2], bottom = labs[3], left = labs[4])
690744
}

man/ggsf.Rd

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

0 commit comments

Comments
 (0)