Skip to content

Enable manual setting of axis tick labels with coord_sf. Closes #2857 #2858

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

Merged
merged 2 commits into from
Aug 27, 2018
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
3 changes: 3 additions & 0 deletions NEWS.md
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,9 @@
* The error message in `compute_aesthetics()` now provides the names of only
aesthetics with mismatched lengths, rather than all aesthetics (@karawoo,
#2853).

* `coord_sf()` now respects manual setting of axis tick labels (@clauswilke,
#2857).

* `geom_sf()` now respects `lineend`, `linejoin`, and `linemitre` parameters
for lines and polygons (@alistaire47, #2826)
Expand Down
53 changes: 48 additions & 5 deletions R/sf.R
Original file line number Diff line number Diff line change
Expand Up @@ -437,6 +437,52 @@ CoordSf <- ggproto("CoordSf", CoordCartesian,
data
},


# internal function used by setup_panel_params,
# overrides the graticule labels based on scale settings if necessary
fixup_graticule_labels = function(self, graticule, scale_x, scale_y, params = list()) {
x_breaks <- graticule$degree[graticule$type == "E"]
if (is.null(scale_x$labels)) {
x_labels <- rep(NA, length(x_breaks))
} else if (is.character(scale_x$labels)) {
x_labels <- scale_x$labels
} else if (is.function(scale_x$labels)){
x_labels <- scale_x$labels(x_breaks)
} else {
x_labels <- graticule$degree_label[graticule$type == "E"]
}
if (length(x_labels) != length(x_breaks)) {
stop("Breaks and labels along x direction are different lengths", call. = FALSE)
}
graticule$degree_label[graticule$type == "E"] <- x_labels


y_breaks <- graticule$degree[graticule$type == "N"]
if (is.null(scale_y$labels)) {
y_labels <- rep(NA, length(y_breaks))
} else if (is.character(scale_y$labels)) {
y_labels <- scale_y$labels
} else if (is.function(scale_y$labels)){
y_labels <- scale_y$labels(y_breaks)
} else {
y_labels <- graticule$degree_label[graticule$type == "N"]
}
if (length(y_labels) != length(y_breaks)) {
stop("Breaks and labels along y direction are different lengths", call. = FALSE)
}
graticule$degree_label[graticule$type == "N"] <- y_labels

# remove tick labels not on axes 1 (bottom) and 2 (left)
if (!is.null(graticule$plot12))
graticule$degree_label[!graticule$plot12] <- NA

# parse labels into expressions if required
if (any(grepl("degree", graticule$degree_label)))
graticule$degree_label <- lapply(graticule$degree_label, function(x) parse(text = x)[[1]])

graticule
},

setup_panel_params = function(self, scale_x, scale_y, params = list()) {
# Bounding box of the data
x_range <- scale_range(scale_x, self$limits$x, self$expand)
Expand All @@ -456,17 +502,14 @@ CoordSf <- ggproto("CoordSf", CoordCartesian,
ndiscr = self$ndiscr
)

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

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

list(
x_range = x_range,
Expand Down