@@ -437,6 +437,52 @@ CoordSf <- ggproto("CoordSf", CoordCartesian,
437
437
data
438
438
},
439
439
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
+
440
486
setup_panel_params = function (self , scale_x , scale_y , params = list ()) {
441
487
# Bounding box of the data
442
488
x_range <- scale_range(scale_x , self $ limits $ x , self $ expand )
@@ -456,6 +502,9 @@ CoordSf <- ggproto("CoordSf", CoordCartesian,
456
502
ndiscr = self $ ndiscr
457
503
)
458
504
505
+ # override graticule labels provided by sf::st_graticule()
506
+ graticule $ degree_label <- self $ get_graticule_labels(graticule , scale_x , scale_y , params )
507
+
459
508
# remove tick labels not on axes 1 (bottom) and 2 (left)
460
509
if (! is.null(graticule $ plot12 ))
461
510
graticule $ degree_label [! graticule $ plot12 ] <- NA
@@ -573,3 +622,23 @@ coord_sf <- function(xlim = NULL, ylim = NULL, expand = TRUE,
573
622
default = default
574
623
)
575
624
}
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