@@ -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
+ fixup_graticule_labels = function (self , graticule , scale_x , scale_y , params = list ()) {
444
+ x_breaks <- graticule $ degree [graticule $ type == " E" ]
445
+ if (is.null(scale_x $ labels )) {
446
+ x_labels <- rep(NA , length(x_breaks ))
447
+ } else if (is.character(scale_x $ labels )) {
448
+ x_labels <- scale_x $ labels
449
+ } else if (is.function(scale_x $ labels )){
450
+ x_labels <- scale_x $ labels(x_breaks )
451
+ } else {
452
+ x_labels <- graticule $ degree_label [graticule $ type == " E" ]
453
+ }
454
+ if (length(x_labels ) != length(x_breaks )) {
455
+ stop(" Breaks and labels along x direction are different lengths" , call. = FALSE )
456
+ }
457
+ graticule $ degree_label [graticule $ type == " E" ] <- x_labels
458
+
459
+
460
+ y_breaks <- graticule $ degree [graticule $ type == " N" ]
461
+ if (is.null(scale_y $ labels )) {
462
+ y_labels <- rep(NA , length(y_breaks ))
463
+ } else if (is.character(scale_y $ labels )) {
464
+ y_labels <- scale_y $ labels
465
+ } else if (is.function(scale_y $ labels )){
466
+ y_labels <- scale_y $ labels(y_breaks )
467
+ } else {
468
+ y_labels <- graticule $ degree_label [graticule $ type == " N" ]
469
+ }
470
+ if (length(y_labels ) != length(y_breaks )) {
471
+ stop(" Breaks and labels along y direction are different lengths" , call. = FALSE )
472
+ }
473
+ graticule $ degree_label [graticule $ type == " N" ] <- y_labels
474
+
475
+ # remove tick labels not on axes 1 (bottom) and 2 (left)
476
+ if (! is.null(graticule $ plot12 ))
477
+ graticule $ degree_label [! graticule $ plot12 ] <- NA
478
+
479
+ # parse labels into expressions if required
480
+ if (any(grepl(" degree" , graticule $ degree_label )))
481
+ graticule $ degree_label <- lapply(graticule $ degree_label , function (x ) parse(text = x )[[1 ]])
482
+
483
+ graticule
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,17 +502,14 @@ CoordSf <- ggproto("CoordSf", CoordCartesian,
456
502
ndiscr = self $ ndiscr
457
503
)
458
504
459
- # remove tick labels not on axes 1 (bottom) and 2 (left)
460
- if (! is.null(graticule $ plot12 ))
461
- graticule $ degree_label [! graticule $ plot12 ] <- NA
505
+ # override graticule labels provided by sf::st_graticule() if necessary
506
+ graticule <- self $ fixup_graticule_labels(graticule , scale_x , scale_y , params )
462
507
463
508
sf :: st_geometry(graticule ) <- sf_rescale01(sf :: st_geometry(graticule ), x_range , y_range )
464
509
graticule $ x_start <- sf_rescale01_x(graticule $ x_start , x_range )
465
510
graticule $ x_end <- sf_rescale01_x(graticule $ x_end , x_range )
466
511
graticule $ y_start <- sf_rescale01_x(graticule $ y_start , y_range )
467
512
graticule $ y_end <- sf_rescale01_x(graticule $ y_end , y_range )
468
- if (any(grepl(" degree" , graticule $ degree_label )))
469
- graticule $ degree_label <- lapply(graticule $ degree_label , function (x ) parse(text = x )[[1 ]])
470
513
471
514
list (
472
515
x_range = x_range ,
0 commit comments