Skip to content

Commit 5bbca87

Browse files
committed
enable manual formatting of tick labels for coord_sf(). closes tidyverse#2857
1 parent 6500a30 commit 5bbca87

File tree

1 file changed

+69
-0
lines changed

1 file changed

+69
-0
lines changed

R/sf.R

Lines changed: 69 additions & 0 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+
get_graticule_labels = function(self, graticule, scale_x, scale_y, params = list()) {
444+
# if sf coordinates are not available in degrees latitude and longitude, label
445+
# as regular numbers
446+
if (is.null(params$crs) || is.na(params$crs) || !isTRUE(sf::st_is_longlat(self$datum))) {
447+
x_labeller <- base::format
448+
y_labeller <- base::format
449+
}
450+
else {
451+
x_labeller <- degree_labels_EW
452+
y_labeller <- degree_labels_NS
453+
}
454+
455+
# if scales provide labeling functions override previous function choices
456+
if (is.function(scale_x$labels)) {
457+
x_labeller <- scale_x$labels
458+
}
459+
if (is.function(scale_y$labels)) {
460+
y_labeller <- scale_y$labels
461+
}
462+
463+
x_breaks <- graticule[graticule$type == "E", ]$degree
464+
if (is.null(scale_x$labels)) {
465+
x_labels <- rep(NA, length(x_breaks))
466+
} else if (is.character(scale_x$labels)) {
467+
x_labels <- scale_x$labels
468+
} else {
469+
x_labels <- x_labeller(x_breaks)
470+
}
471+
472+
y_breaks <- graticule[graticule$type == "N", ]$degree
473+
if (is.null(scale_y$labels)) {
474+
y_labels <- rep(NA, length(y_breaks))
475+
} else if (is.character(scale_y$labels)) {
476+
y_labels <- scale_y$labels
477+
} else {
478+
y_labels <- y_labeller(y_breaks)
479+
}
480+
481+
# still to do: 1. check lengths of lables vs. breaks; 2. make sure order is correct
482+
483+
c(x_labels, y_labels)
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,6 +502,9 @@ CoordSf <- ggproto("CoordSf", CoordCartesian,
456502
ndiscr = self$ndiscr
457503
)
458504

505+
# override graticule labels provided by sf::st_graticule()
506+
graticule$degree_label <- self$get_graticule_labels(graticule, scale_x, scale_y, params)
507+
459508
# remove tick labels not on axes 1 (bottom) and 2 (left)
460509
if (!is.null(graticule$plot12))
461510
graticule$degree_label[!graticule$plot12] <- NA
@@ -573,3 +622,23 @@ coord_sf <- function(xlim = NULL, ylim = NULL, expand = TRUE,
573622
default = default
574623
)
575624
}
625+
626+
# copied from sp
627+
# move to scales package at some point?
628+
629+
degree_labels_NS <- function(x) {
630+
pos = sign(x) + 2
631+
dir = c("*S", "", "*N")
632+
paste0(abs(x), "*degree", dir[pos])
633+
}
634+
635+
degree_labels_EW <- function(x) {
636+
x <- ifelse(x > 180, x - 360, x)
637+
pos = sign(x) + 2
638+
if (any(x == -180))
639+
pos[x == -180] = 2
640+
if (any(x == 180))
641+
pos[x == 180] = 2
642+
dir = c("*W", "", "*E")
643+
paste0(abs(x), "*degree", dir[pos])
644+
}

0 commit comments

Comments
 (0)