@@ -354,7 +354,8 @@ CoordSf <- ggproto("CoordSf", CoordCartesian,
354
354
y_range = y_range ,
355
355
graticule = graticule ,
356
356
crs = params $ crs ,
357
- graticule_labeling = self $ graticule_labeling
357
+ label_axes = self $ label_axes ,
358
+ label_graticules = self $ label_graticules
358
359
)
359
360
},
360
361
@@ -393,12 +394,12 @@ CoordSf <- ggproto("CoordSf", CoordCartesian,
393
394
graticule <- panel_params $ graticule
394
395
395
396
# top axis
396
- if (identical(panel_params $ graticule_labeling $ top , " E" ) ||
397
- identical(panel_params $ graticule_labeling $ top , " N" )) {
397
+ if (identical(panel_params $ label_axes $ top , " E" ) ||
398
+ identical(panel_params $ label_axes $ top , " N" )) {
398
399
# we don't generally know which direction graticules run, so need to consider both
399
- ticks1 <- graticule [graticule $ type == panel_params $ graticule_labeling $ top &
400
+ ticks1 <- graticule [graticule $ type == panel_params $ label_axes $ top &
400
401
graticule $ y_start > 0.999 , ]
401
- ticks2 <- graticule [graticule $ type == panel_params $ graticule_labeling $ top &
402
+ ticks2 <- graticule [graticule $ type == panel_params $ label_axes $ top &
402
403
graticule $ y_end > 0.999 , ]
403
404
tick_positions <- c(ticks1 $ x_start , ticks2 $ x_end )
404
405
tick_labels <- c(ticks1 $ degree_label , ticks2 $ degree_label )
@@ -414,12 +415,12 @@ CoordSf <- ggproto("CoordSf", CoordCartesian,
414
415
}
415
416
416
417
# bottom axis
417
- if (identical(panel_params $ graticule_labeling $ bottom , " E" ) ||
418
- identical(panel_params $ graticule_labeling $ bottom , " N" )) {
418
+ if (identical(panel_params $ label_axes $ bottom , " E" ) ||
419
+ identical(panel_params $ label_axes $ bottom , " N" )) {
419
420
# we don't generally know which direction graticules run, so need to consider both
420
- ticks1 <- graticule [graticule $ type == panel_params $ graticule_labeling $ bottom &
421
+ ticks1 <- graticule [graticule $ type == panel_params $ label_axes $ bottom &
421
422
graticule $ y_start < 0.001 , ]
422
- ticks2 <- graticule [graticule $ type == panel_params $ graticule_labeling $ bottom &
423
+ ticks2 <- graticule [graticule $ type == panel_params $ label_axes $ bottom &
423
424
graticule $ y_end < 0.001 , ]
424
425
tick_positions <- c(ticks1 $ x_start , ticks2 $ x_end )
425
426
tick_labels <- c(ticks1 $ degree_label , ticks2 $ degree_label )
@@ -442,12 +443,12 @@ CoordSf <- ggproto("CoordSf", CoordCartesian,
442
443
graticule <- panel_params $ graticule
443
444
444
445
# left axis
445
- if (identical(panel_params $ graticule_labeling $ left , " E" ) ||
446
- identical(panel_params $ graticule_labeling $ left , " N" )) {
446
+ if (identical(panel_params $ label_axes $ left , " E" ) ||
447
+ identical(panel_params $ label_axes $ left , " N" )) {
447
448
# we don't generally know which direction graticules run, so need to consider both
448
- ticks1 <- graticule [graticule $ type == panel_params $ graticule_labeling $ left &
449
+ ticks1 <- graticule [graticule $ type == panel_params $ label_axes $ left &
449
450
graticule $ x_start < 0.001 , ]
450
- ticks2 <- graticule [graticule $ type == panel_params $ graticule_labeling $ left &
451
+ ticks2 <- graticule [graticule $ type == panel_params $ label_axes $ left &
451
452
graticule $ x_end < 0.001 , ]
452
453
tick_positions <- c(ticks1 $ y_start , ticks2 $ y_end )
453
454
tick_labels <- c(ticks1 $ degree_label , ticks2 $ degree_label )
@@ -463,12 +464,12 @@ CoordSf <- ggproto("CoordSf", CoordCartesian,
463
464
}
464
465
465
466
# right axis
466
- if (identical(panel_params $ graticule_labeling $ right , " E" ) ||
467
- identical(panel_params $ graticule_labeling $ right , " N" )) {
467
+ if (identical(panel_params $ label_axes $ right , " E" ) ||
468
+ identical(panel_params $ label_axes $ right , " N" )) {
468
469
# we don't generally know which direction graticules run, so need to consider both
469
- ticks1 <- graticule [graticule $ type == panel_params $ graticule_labeling $ right &
470
+ ticks1 <- graticule [graticule $ type == panel_params $ label_axes $ right &
470
471
graticule $ x_start > 0.999 , ]
471
- ticks2 <- graticule [graticule $ type == panel_params $ graticule_labeling $ right &
472
+ ticks2 <- graticule [graticule $ type == panel_params $ label_axes $ right &
472
473
graticule $ x_end > 0.999 , ]
473
474
tick_positions <- c(ticks1 $ y_start , ticks2 $ y_end )
474
475
tick_labels <- c(ticks1 $ degree_label , ticks2 $ degree_label )
@@ -491,16 +492,21 @@ CoordSf <- ggproto("CoordSf", CoordCartesian,
491
492
492
493
# top axis
493
494
id1 <- id2 <- integer(0 )
494
- if (" S" %in% panel_params $ graticule_labeling ) {
495
+ # labels based on panel side
496
+ id1 <- c(id1 , which(graticule $ type == panel_params $ label_axes $ top & graticule $ y_start > 0.999 ))
497
+ id2 <- c(id2 , which(graticule $ type == panel_params $ label_axes $ top & graticule $ y_end > 0.999 ))
498
+
499
+ # labels based on graticule direction
500
+ if (" S" %in% panel_params $ label_graticules ) {
495
501
id1 <- c(id1 , which(graticule $ type == " E" & graticule $ y_start > 0.999 ))
496
502
}
497
- if (" N" %in% panel_params $ graticule_labeling ) {
503
+ if (" N" %in% panel_params $ label_graticules ) {
498
504
id2 <- c(id2 , which(graticule $ type == " E" & graticule $ y_end > 0.999 ))
499
505
}
500
- if (" W" %in% panel_params $ graticule_labeling ) {
506
+ if (" W" %in% panel_params $ label_graticules ) {
501
507
id1 <- c(id1 , which(graticule $ type == " N" & graticule $ y_start > 0.999 ))
502
508
}
503
- if (" E" %in% panel_params $ graticule_labeling ) {
509
+ if (" E" %in% panel_params $ label_graticules ) {
504
510
id2 <- c(id2 , which(graticule $ type == " N" & graticule $ y_end > 0.999 ))
505
511
}
506
512
@@ -522,16 +528,21 @@ CoordSf <- ggproto("CoordSf", CoordCartesian,
522
528
523
529
# bottom axis
524
530
id1 <- id2 <- integer(0 )
525
- if (" S" %in% panel_params $ graticule_labeling ) {
531
+ # labels based on panel side
532
+ id1 <- c(id1 , which(graticule $ type == panel_params $ label_axes $ bottom & graticule $ y_start < 0.001 ))
533
+ id2 <- c(id2 , which(graticule $ type == panel_params $ label_axes $ bottom & graticule $ y_end < 0.001 ))
534
+
535
+ # labels based on graticule direction
536
+ if (" S" %in% panel_params $ label_graticules ) {
526
537
id1 <- c(id1 , which(graticule $ type == " E" & graticule $ y_start < 0.001 ))
527
538
}
528
- if (" N" %in% panel_params $ graticule_labeling ) {
539
+ if (" N" %in% panel_params $ label_graticules ) {
529
540
id2 <- c(id2 , which(graticule $ type == " E" & graticule $ y_end < 0.001 ))
530
541
}
531
- if (" W" %in% panel_params $ graticule_labeling ) {
542
+ if (" W" %in% panel_params $ label_graticules ) {
532
543
id1 <- c(id1 , which(graticule $ type == " N" & graticule $ y_start < 0.001 ))
533
544
}
534
- if (" E" %in% panel_params $ graticule_labeling ) {
545
+ if (" E" %in% panel_params $ label_graticules ) {
535
546
id2 <- c(id2 , which(graticule $ type == " N" & graticule $ y_end < 0.001 ))
536
547
}
537
548
@@ -559,16 +570,21 @@ CoordSf <- ggproto("CoordSf", CoordCartesian,
559
570
560
571
# right axis
561
572
id1 <- id2 <- integer(0 )
562
- if (" N" %in% panel_params $ graticule_labeling ) {
573
+ # labels based on panel side
574
+ id1 <- c(id1 , which(graticule $ type == panel_params $ label_axes $ right & graticule $ x_end > 0.999 ))
575
+ id2 <- c(id2 , which(graticule $ type == panel_params $ label_axes $ right & graticule $ x_start > 0.999 ))
576
+
577
+ # labels based on graticule direction
578
+ if (" N" %in% panel_params $ label_graticules ) {
563
579
id1 <- c(id1 , which(graticule $ type == " E" & graticule $ x_end > 0.999 ))
564
580
}
565
- if (" S" %in% panel_params $ graticule_labeling ) {
581
+ if (" S" %in% panel_params $ label_graticules ) {
566
582
id2 <- c(id2 , which(graticule $ type == " E" & graticule $ x_start > 0.999 ))
567
583
}
568
- if (" E" %in% panel_params $ graticule_labeling ) {
584
+ if (" E" %in% panel_params $ label_graticules ) {
569
585
id1 <- c(id1 , which(graticule $ type == " N" & graticule $ x_end > 0.999 ))
570
586
}
571
- if (" W" %in% panel_params $ graticule_labeling ) {
587
+ if (" W" %in% panel_params $ label_graticules ) {
572
588
id2 <- c(id2 , which(graticule $ type == " N" & graticule $ x_start > 0.999 ))
573
589
}
574
590
@@ -590,16 +606,21 @@ CoordSf <- ggproto("CoordSf", CoordCartesian,
590
606
591
607
# left axis
592
608
id1 <- id2 <- integer(0 )
593
- if (" N" %in% panel_params $ graticule_labeling ) {
609
+ # labels based on panel side
610
+ id1 <- c(id1 , which(graticule $ type == panel_params $ label_axes $ left & graticule $ x_end < 0.001 ))
611
+ id2 <- c(id2 , which(graticule $ type == panel_params $ label_axes $ left & graticule $ x_start < 0.001 ))
612
+
613
+ # labels based on graticule direction
614
+ if (" N" %in% panel_params $ label_graticules ) {
594
615
id1 <- c(id1 , which(graticule $ type == " E" & graticule $ x_end < 0.001 ))
595
616
}
596
- if (" S" %in% panel_params $ graticule_labeling ) {
617
+ if (" S" %in% panel_params $ label_graticules ) {
597
618
id2 <- c(id2 , which(graticule $ type == " E" & graticule $ x_start < 0.001 ))
598
619
}
599
- if (" E" %in% panel_params $ graticule_labeling ) {
620
+ if (" E" %in% panel_params $ label_graticules ) {
600
621
id1 <- c(id1 , which(graticule $ type == " N" & graticule $ x_end < 0.001 ))
601
622
}
602
- if (" W" %in% panel_params $ graticule_labeling ) {
623
+ if (" W" %in% panel_params $ label_graticules ) {
603
624
id2 <- c(id2 , which(graticule $ type == " N" & graticule $ x_start < 0.001 ))
604
625
}
605
626
@@ -642,49 +663,82 @@ sf_rescale01_x <- function(x, range) {
642
663
# ' @param crs Use this to select a specific coordinate reference system (CRS).
643
664
# ' If not specified, will use the CRS defined in the first layer.
644
665
# ' @param datum CRS that provides datum to use when generating graticules
645
- # ' @param graticule_labeling Character vector or named list of character values
666
+ # ' @param label_axes Character vector or named list of character values
646
667
# ' specifying which graticules (meridians or parallels) should be labeled on
647
668
# ' which side of the plot. Meridians are indicated by `"E"` (for East) and
648
- # ' parallels by `"N"` (for North). Default is `"NE--"`, which specifies
649
- # ' (counter-clockwise from the left) paralleles on the left, meridians on the bottom,
650
- # ' and nothing on the right or top. Alternatively, this setting could have been
651
- # ' specified with `list(left = "N", bottom = "E", right = NA, top = NA)`.
669
+ # ' parallels by `"N"` (for North). Default is `"--EN"`, which specifies
670
+ # ' (clockwise from the top) no labels on the top, none on the right, meridians
671
+ # ' on the bottom, and paralleles on the left. Alternatively, this setting could have been
672
+ # ' specified with `list(left = "N", bottom = "E")`.
673
+ # '
674
+ # ' This parameter can be used alone or in combination with `label_graticules`.
675
+ # ' @param label_axes Character vector indicating which graticules should be labeled
676
+ # ' where. Meridians run north-south, and the letters `"N"` and `"S"` indicate that
677
+ # ' they should be labeled on their north or south end points, respectively.
678
+ # ' Parallels run east-west, and the letters `"E"` and `"W"` indicate that they
679
+ # ' should be labeled on their east or west end points, respectively. Thus,
680
+ # ' `label_axes = "SW"` would label meridians at their south end and parallels at
681
+ # ' their west end, whereas `label_axes = "EW"` would label parallels at both
682
+ # ' ends and meridians not at all. Because meridians and parallels can in general
683
+ # ' intersect with any side of the plot panel, for any choice of `label_axes` labels
684
+ # ' are not guaranteed to reside on only one particular side of the plot panel.
685
+ # '
686
+ # ' This parameter can be used alone or in combination with `label_axes`.
652
687
# ' @param ndiscr number of segments to use for discretising graticule lines;
653
688
# ' try increasing this when graticules look unexpected
654
689
# ' @inheritParams coord_cartesian
655
690
# ' @export
656
691
# ' @rdname ggsf
657
692
coord_sf <- function (xlim = NULL , ylim = NULL , expand = TRUE ,
658
693
crs = NULL , datum = sf :: st_crs(4326 ),
659
- graticule_labeling = " NW" ,
694
+ label_graticules = waiver(),
695
+ label_axes = waiver(),
660
696
ndiscr = 100 , default = FALSE ) {
661
697
662
- if (FALSE ) { # commented out for now
663
- # graticule labeling can be specified via string or named list
664
- if (is.character(graticule_labeling )) {
665
- graticule_labeling <- parse_graticule_labeling(graticule_labeling )
666
- } else if (! is.list(graticule_labeling )) {
698
+ if (is.waive(label_graticules ) && is.waive(label_axes )) {
699
+ # if both `label_graticules` and `label_axes` are set to waive then we
700
+ # use the default of labels on the left and at the bottom
701
+ label_graticules <- " "
702
+ label_axes <- " --EN"
703
+ } else {
704
+ # if at least one is set we ignore the other
705
+ label_graticules <- label_graticules %| W | % " "
706
+ label_axes <- label_axes %| W | % " "
707
+ }
708
+
709
+ if (is.character(label_axes )) {
710
+ label_axes <- parse_axes_labeling(label_axes )
711
+ } else if (! is.list(label_axes )) {
667
712
warning(
668
- " Graticule labeling format not recognized. Proceeding with default settings." ,
713
+ " Panel labeling format not recognized. Proceeding with default settings." ,
669
714
call. = FALSE
670
715
)
671
- graticule_labeling <- list (left = " N" , bottom = " E" )
716
+ label_axes <- list (left = " N" , bottom = " E" )
672
717
}
718
+
719
+ if (is.character(label_graticules )) {
720
+ label_graticules <- unlist(strsplit(label_graticules , " " ))
721
+ } else {
722
+ warning(
723
+ " Graticule labeling format not recognized. Proceeding with default settings." ,
724
+ call. = FALSE
725
+ )
726
+ label_graticules <- " "
673
727
}
674
- graticule_labeling <- unlist(strsplit(graticule_labeling , " " ))
675
728
676
729
ggproto(NULL , CoordSf ,
677
730
limits = list (x = xlim , y = ylim ),
678
731
datum = datum ,
679
732
crs = crs ,
680
- graticule_labeling = graticule_labeling ,
733
+ label_axes = label_axes ,
734
+ label_graticules = label_graticules ,
681
735
ndiscr = ndiscr ,
682
736
expand = expand ,
683
737
default = default
684
738
)
685
739
}
686
740
687
- parse_graticule_labeling <- function (x ) {
741
+ parse_axes_labeling <- function (x ) {
688
742
labs = unlist(strsplit(x , " " ))
689
- list (left = labs [1 ], bottom = labs [2 ], right = labs [3 ], top = labs [4 ])
743
+ list (top = labs [1 ], right = labs [2 ], bottom = labs [3 ], left = labs [4 ])
690
744
}
0 commit comments