Skip to content

Commit 68febed

Browse files
committed
add labels_function argument to st_graticule; #829
1 parent 631295e commit 68febed

File tree

2 files changed

+17
-6
lines changed

2 files changed

+17
-6
lines changed

R/graticule.R

Lines changed: 11 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -22,6 +22,8 @@
2222
#' @param margin numeric; small number to trim a longlat bounding box that touches or
2323
#' crosses +/-180 long or +/-90 latitude.
2424
#' @param ... ignored
25+
#' @param labels_function function that computes axis tic labels from coordinates; see Details
26+
#' @details The \code{labels_function} function takes four arguments, \code{lon}, \code{lat}, \code{crs} and \code{datum}, and returns a character vector of length \code{length(lon) + length(lat)} with labels to be plotted at these \code{lon} and \code{lat} values.
2527
#' @return an object of class \code{sf} with additional attributes describing the type
2628
#' (E: meridian, N: parallel) degree value, label, start and end coordinates and angle;
2729
#' see example.
@@ -61,7 +63,7 @@
6163
#' plot(usa, graticule = st_crs(4326), axes = TRUE, lon = seq(-60,-130,by=-10))
6264
st_graticule = function(x = c(-180,-90,180,90), crs = st_crs(x),
6365
datum = st_crs(4326), ..., lon = NULL, lat = NULL, ndiscr = 100,
64-
margin = 0.001)
66+
margin = 0.001, labels_function = degree_label)
6567
{
6668
if (missing(x)) {
6769
crs = datum
@@ -151,10 +153,7 @@ st_graticule = function(x = c(-180,-90,180,90), crs = st_crs(x),
151153

152154
df = data.frame(degree = c(lon, lat))
153155
df$type = c(rep("E", length(lon)), rep("N", length(lat)))
154-
df$degree_label = if (is.na(crs) || !isTRUE(st_is_longlat(datum)))
155-
c(format(lon), format(lat))
156-
else
157-
c(degreeLabelsEW(lon), degreeLabelsNS(lat))
156+
df$degree_label = labels_function(lon, lat, crs, datum)
158157

159158
geom = st_sfc(c(long_list, lat_list), crs = datum)
160159

@@ -212,6 +211,13 @@ trim_bb = function(bb = c(-180, -90, 180, 90), margin, wrap=c(-180,180)) {
212211
bb[4] = min(bb[4], 90. * fr)
213212
bb
214213
}
214+
215+
degree_label = function(lon, lat, crs, datum) {
216+
if (is.na(crs) || !isTRUE(st_is_longlat(datum)))
217+
c(format(lon), format(lat))
218+
else
219+
c(degreeLabelsEW(lon), degreeLabelsNS(lat))
220+
}
215221

216222
# copied from sp:
217223
degreeLabelsNS = function(x) {

man/st_graticule.Rd

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

0 commit comments

Comments
 (0)