Skip to content

Commit 6e9e144

Browse files
committed
graticule labeling via cardinal coordinates
1 parent e7d05ea commit 6e9e144

File tree

1 file changed

+144
-3
lines changed

1 file changed

+144
-3
lines changed

R/sf.R

Lines changed: 144 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -388,7 +388,8 @@ CoordSf <- ggproto("CoordSf", CoordCartesian,
388388
ggname("grill", do.call("grobTree", grobs))
389389
},
390390

391-
render_axis_h = function(self, panel_params, theme) {
391+
# not used, kept around for backup
392+
render_axis_h_alt = function(self, panel_params, theme) {
392393
graticule <- panel_params$graticule
393394

394395
# top axis
@@ -436,7 +437,8 @@ CoordSf <- ggproto("CoordSf", CoordCartesian,
436437
list(top = top, bottom = bottom)
437438
},
438439

439-
render_axis_v = function(self, panel_params, theme) {
440+
# not used, kept around for backup
441+
render_axis_v_alt = function(self, panel_params, theme) {
440442
graticule <- panel_params$graticule
441443

442444
# left axis
@@ -481,6 +483,142 @@ CoordSf <- ggproto("CoordSf", CoordCartesian,
481483
right <- zeroGrob()
482484
}
483485

486+
list(left = left, right = right)
487+
},
488+
489+
render_axis_h = function(self, panel_params, theme) {
490+
graticule <- panel_params$graticule
491+
492+
# top axis
493+
id1 <- id2 <- integer(0)
494+
if ("S" %in% panel_params$graticule_labeling) {
495+
id1 <- c(id1, which(graticule$type == "E" & graticule$y_start > 0.999))
496+
}
497+
if ("N" %in% panel_params$graticule_labeling) {
498+
id2 <- c(id2, which(graticule$type == "E" & graticule$y_end > 0.999))
499+
}
500+
if ("W" %in% panel_params$graticule_labeling) {
501+
id1 <- c(id1, which(graticule$type == "N" & graticule$y_start > 0.999))
502+
}
503+
if ("E" %in% panel_params$graticule_labeling) {
504+
id2 <- c(id2, which(graticule$type == "N" & graticule$y_end > 0.999))
505+
}
506+
507+
ticks1 <- graticule[unique(id1), ]
508+
ticks2 <- graticule[unique(id2), ]
509+
tick_positions <- c(ticks1$x_start, ticks2$x_end)
510+
tick_labels <- c(ticks1$degree_label, ticks2$degree_label)
511+
512+
if (length(tick_positions) > 0) {
513+
top <- guide_axis(
514+
tick_positions,
515+
tick_labels,
516+
position = "top",
517+
theme = theme
518+
)
519+
} else {
520+
top <- zeroGrob()
521+
}
522+
523+
# bottom axis
524+
id1 <- id2 <- integer(0)
525+
if ("S" %in% panel_params$graticule_labeling) {
526+
id1 <- c(id1, which(graticule$type == "E" & graticule$y_start < 0.001))
527+
}
528+
if ("N" %in% panel_params$graticule_labeling) {
529+
id2 <- c(id2, which(graticule$type == "E" & graticule$y_end < 0.001))
530+
}
531+
if ("W" %in% panel_params$graticule_labeling) {
532+
id1 <- c(id1, which(graticule$type == "N" & graticule$y_start < 0.001))
533+
}
534+
if ("E" %in% panel_params$graticule_labeling) {
535+
id2 <- c(id2, which(graticule$type == "N" & graticule$y_end < 0.001))
536+
}
537+
538+
ticks1 <- graticule[unique(id1), ]
539+
ticks2 <- graticule[unique(id2), ]
540+
tick_positions <- c(ticks1$x_start, ticks2$x_end)
541+
tick_labels <- c(ticks1$degree_label, ticks2$degree_label)
542+
543+
if (length(tick_positions) > 0) {
544+
bottom <- guide_axis(
545+
tick_positions,
546+
tick_labels,
547+
position = "bottom",
548+
theme = theme
549+
)
550+
} else {
551+
bottom <- zeroGrob()
552+
}
553+
554+
list(top = top, bottom = bottom)
555+
},
556+
557+
render_axis_v = function(self, panel_params, theme) {
558+
graticule <- panel_params$graticule
559+
560+
# right axis
561+
id1 <- id2 <- integer(0)
562+
if ("N" %in% panel_params$graticule_labeling) {
563+
id1 <- c(id1, which(graticule$type == "E" & graticule$x_end > 0.999))
564+
}
565+
if ("S" %in% panel_params$graticule_labeling) {
566+
id2 <- c(id2, which(graticule$type == "E" & graticule$x_start > 0.999))
567+
}
568+
if ("E" %in% panel_params$graticule_labeling) {
569+
id1 <- c(id1, which(graticule$type == "N" & graticule$x_end > 0.999))
570+
}
571+
if ("W" %in% panel_params$graticule_labeling) {
572+
id2 <- c(id2, which(graticule$type == "N" & graticule$x_start > 0.999))
573+
}
574+
575+
ticks1 <- graticule[unique(id1), ]
576+
ticks2 <- graticule[unique(id2), ]
577+
tick_positions <- c(ticks1$y_end, ticks2$y_start)
578+
tick_labels <- c(ticks1$degree_label, ticks2$degree_label)
579+
580+
if (length(tick_positions) > 0) {
581+
right <- guide_axis(
582+
tick_positions,
583+
tick_labels,
584+
position = "right",
585+
theme = theme
586+
)
587+
} else {
588+
right <- zeroGrob()
589+
}
590+
591+
# left axis
592+
id1 <- id2 <- integer(0)
593+
if ("N" %in% panel_params$graticule_labeling) {
594+
id1 <- c(id1, which(graticule$type == "E" & graticule$x_end < 0.001))
595+
}
596+
if ("S" %in% panel_params$graticule_labeling) {
597+
id2 <- c(id2, which(graticule$type == "E" & graticule$x_start < 0.001))
598+
}
599+
if ("E" %in% panel_params$graticule_labeling) {
600+
id1 <- c(id1, which(graticule$type == "N" & graticule$x_end < 0.001))
601+
}
602+
if ("W" %in% panel_params$graticule_labeling) {
603+
id2 <- c(id2, which(graticule$type == "N" & graticule$x_start < 0.001))
604+
}
605+
606+
ticks1 <- graticule[unique(id1), ]
607+
ticks2 <- graticule[unique(id2), ]
608+
tick_positions <- c(ticks1$y_end, ticks2$y_start)
609+
tick_labels <- c(ticks1$degree_label, ticks2$degree_label)
610+
611+
if (length(tick_positions) > 0) {
612+
left <- guide_axis(
613+
tick_positions,
614+
tick_labels,
615+
position = "left",
616+
theme = theme
617+
)
618+
} else {
619+
left <- zeroGrob()
620+
}
621+
484622
list(left = left, right = right)
485623
}
486624

@@ -518,9 +656,10 @@ sf_rescale01_x <- function(x, range) {
518656
#' @rdname ggsf
519657
coord_sf <- function(xlim = NULL, ylim = NULL, expand = TRUE,
520658
crs = NULL, datum = sf::st_crs(4326),
521-
graticule_labeling = "NE--",
659+
graticule_labeling = "NW",
522660
ndiscr = 100, default = FALSE) {
523661

662+
if (FALSE) { # commented out for now
524663
# graticule labeling can be specified via string or named list
525664
if (is.character(graticule_labeling)) {
526665
graticule_labeling <- parse_graticule_labeling(graticule_labeling)
@@ -531,6 +670,8 @@ coord_sf <- function(xlim = NULL, ylim = NULL, expand = TRUE,
531670
)
532671
graticule_labeling <- list(left = "N", bottom = "E")
533672
}
673+
}
674+
graticule_labeling <- unlist(strsplit(graticule_labeling, ""))
534675

535676
ggproto(NULL, CoordSf,
536677
limits = list(x = xlim, y = ylim),

0 commit comments

Comments
 (0)