Skip to content

Commit 519a55d

Browse files
authored
Enable manual setting of axis tick labels with coord_sf. Closes #2857 (#2858)
* enable manual formatting of tick labels for coord_sf(). closes #2857
1 parent 6500a30 commit 519a55d

File tree

2 files changed

+51
-5
lines changed

2 files changed

+51
-5
lines changed

NEWS.md

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,9 @@
33
* The error message in `compute_aesthetics()` now provides the names of only
44
aesthetics with mismatched lengths, rather than all aesthetics (@karawoo,
55
#2853).
6+
7+
* `coord_sf()` now respects manual setting of axis tick labels (@clauswilke,
8+
#2857).
69

710
* `geom_sf()` now respects `lineend`, `linejoin`, and `linemitre` parameters
811
for lines and polygons (@alistaire47, #2826)

R/sf.R

Lines changed: 48 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -437,6 +437,52 @@ CoordSf <- ggproto("CoordSf", CoordCartesian,
437437
data
438438
},
439439

440+
441+
# internal function used by setup_panel_params,
442+
# overrides the graticule labels based on scale settings if necessary
443+
fixup_graticule_labels = function(self, graticule, scale_x, scale_y, params = list()) {
444+
x_breaks <- graticule$degree[graticule$type == "E"]
445+
if (is.null(scale_x$labels)) {
446+
x_labels <- rep(NA, length(x_breaks))
447+
} else if (is.character(scale_x$labels)) {
448+
x_labels <- scale_x$labels
449+
} else if (is.function(scale_x$labels)){
450+
x_labels <- scale_x$labels(x_breaks)
451+
} else {
452+
x_labels <- graticule$degree_label[graticule$type == "E"]
453+
}
454+
if (length(x_labels) != length(x_breaks)) {
455+
stop("Breaks and labels along x direction are different lengths", call. = FALSE)
456+
}
457+
graticule$degree_label[graticule$type == "E"] <- x_labels
458+
459+
460+
y_breaks <- graticule$degree[graticule$type == "N"]
461+
if (is.null(scale_y$labels)) {
462+
y_labels <- rep(NA, length(y_breaks))
463+
} else if (is.character(scale_y$labels)) {
464+
y_labels <- scale_y$labels
465+
} else if (is.function(scale_y$labels)){
466+
y_labels <- scale_y$labels(y_breaks)
467+
} else {
468+
y_labels <- graticule$degree_label[graticule$type == "N"]
469+
}
470+
if (length(y_labels) != length(y_breaks)) {
471+
stop("Breaks and labels along y direction are different lengths", call. = FALSE)
472+
}
473+
graticule$degree_label[graticule$type == "N"] <- y_labels
474+
475+
# remove tick labels not on axes 1 (bottom) and 2 (left)
476+
if (!is.null(graticule$plot12))
477+
graticule$degree_label[!graticule$plot12] <- NA
478+
479+
# parse labels into expressions if required
480+
if (any(grepl("degree", graticule$degree_label)))
481+
graticule$degree_label <- lapply(graticule$degree_label, function(x) parse(text = x)[[1]])
482+
483+
graticule
484+
},
485+
440486
setup_panel_params = function(self, scale_x, scale_y, params = list()) {
441487
# Bounding box of the data
442488
x_range <- scale_range(scale_x, self$limits$x, self$expand)
@@ -456,17 +502,14 @@ CoordSf <- ggproto("CoordSf", CoordCartesian,
456502
ndiscr = self$ndiscr
457503
)
458504

459-
# remove tick labels not on axes 1 (bottom) and 2 (left)
460-
if (!is.null(graticule$plot12))
461-
graticule$degree_label[!graticule$plot12] <- NA
505+
# override graticule labels provided by sf::st_graticule() if necessary
506+
graticule <- self$fixup_graticule_labels(graticule, scale_x, scale_y, params)
462507

463508
sf::st_geometry(graticule) <- sf_rescale01(sf::st_geometry(graticule), x_range, y_range)
464509
graticule$x_start <- sf_rescale01_x(graticule$x_start, x_range)
465510
graticule$x_end <- sf_rescale01_x(graticule$x_end, x_range)
466511
graticule$y_start <- sf_rescale01_x(graticule$y_start, y_range)
467512
graticule$y_end <- sf_rescale01_x(graticule$y_end, y_range)
468-
if (any(grepl("degree", graticule$degree_label)))
469-
graticule$degree_label <- lapply(graticule$degree_label, function(x) parse(text = x)[[1]])
470513

471514
list(
472515
x_range = x_range,

0 commit comments

Comments
 (0)