@@ -349,7 +349,8 @@ CoordSf <- ggproto("CoordSf", CoordCartesian,
349
349
x_range = x_range ,
350
350
y_range = y_range ,
351
351
graticule = graticule ,
352
- crs = params $ crs
352
+ crs = params $ crs ,
353
+ graticule_labeling = self $ graticule_labeling
353
354
)
354
355
},
355
356
@@ -382,53 +383,97 @@ CoordSf <- ggproto("CoordSf", CoordCartesian,
382
383
render_axis_h = function (self , panel_params , theme ) {
383
384
graticule <- panel_params $ graticule
384
385
385
- # horizontal axes label degrees east (meridians)
386
- # for the bottom side, graticule$plot12 tells us whether
387
- # a tick is needed or not
388
- ticks_bottom <- graticule [graticule $ type == " E" & graticule $ plot12 , ]
389
- # for the top, we need to guess
390
- ticks_top <- graticule [graticule $ type == " E" & graticule $ y_end > 0.99 , ]
391
-
392
- list (
393
- top = guide_axis(
394
- ticks_top $ x_end ,
395
- ticks_top $ degree_label ,
386
+ # top axis
387
+ if (identical(panel_params $ graticule_labeling $ top , " E" ) ||
388
+ identical(panel_params $ graticule_labeling $ top , " N" )) {
389
+ # we don't generally know which direction graticules run, so need to consider both
390
+ ticks1 <- graticule [graticule $ type == panel_params $ graticule_labeling $ top &
391
+ graticule $ y_start > 0.999 , ]
392
+ ticks2 <- graticule [graticule $ type == panel_params $ graticule_labeling $ top &
393
+ graticule $ y_end > 0.999 , ]
394
+ tick_positions <- c(ticks1 $ x_start , ticks2 $ x_end )
395
+ tick_labels <- c(ticks1 $ degree_label , ticks2 $ degree_label )
396
+
397
+ top <- guide_axis(
398
+ tick_positions ,
399
+ tick_labels ,
396
400
position = " top" ,
397
401
theme = theme
398
- ),
399
- bottom = guide_axis(
400
- ticks_bottom $ x_start ,
401
- ticks_bottom $ degree_label ,
402
+ )
403
+ } else {
404
+ top <- zeroGrob()
405
+ }
406
+
407
+ # bottom axis
408
+ if (identical(panel_params $ graticule_labeling $ bottom , " E" ) ||
409
+ identical(panel_params $ graticule_labeling $ bottom , " N" )) {
410
+ # we don't generally know which direction graticules run, so need to consider both
411
+ ticks1 <- graticule [graticule $ type == panel_params $ graticule_labeling $ bottom &
412
+ graticule $ y_start < 0.001 , ]
413
+ ticks2 <- graticule [graticule $ type == panel_params $ graticule_labeling $ bottom &
414
+ graticule $ y_end < 0.001 , ]
415
+ tick_positions <- c(ticks1 $ x_start , ticks2 $ x_end )
416
+ tick_labels <- c(ticks1 $ degree_label , ticks2 $ degree_label )
417
+
418
+ bottom <- guide_axis(
419
+ tick_positions ,
420
+ tick_labels ,
402
421
position = " bottom" ,
403
422
theme = theme
404
423
)
405
- )
424
+ } else {
425
+ bottom <- zeroGrob()
426
+ }
427
+
428
+ list (top = top , bottom = bottom )
406
429
},
407
430
408
431
render_axis_v = function (self , panel_params , theme ) {
409
432
graticule <- panel_params $ graticule
410
433
411
- # vertical axes label degrees north (parallels)
412
- # for the left side, graticule$plot12 tells us whether
413
- # a tick is needed or not
414
- ticks_left <- graticule [graticule $ type == " N" & graticule $ plot12 , ]
415
- # for the right side, we need to guess
416
- ticks_right <- graticule [graticule $ type == " N" & graticule $ x_end > 0.99 , ]
417
-
418
- list (
419
- left = guide_axis(
420
- ticks_left $ y_start ,
421
- ticks_left $ degree_label ,
434
+ # left axis
435
+ if (identical(panel_params $ graticule_labeling $ left , " E" ) ||
436
+ identical(panel_params $ graticule_labeling $ left , " N" )) {
437
+ # we don't generally know which direction graticules run, so need to consider both
438
+ ticks1 <- graticule [graticule $ type == panel_params $ graticule_labeling $ left &
439
+ graticule $ x_start < 0.001 , ]
440
+ ticks2 <- graticule [graticule $ type == panel_params $ graticule_labeling $ left &
441
+ graticule $ x_end < 0.001 , ]
442
+ tick_positions <- c(ticks1 $ y_start , ticks2 $ y_end )
443
+ tick_labels <- c(ticks1 $ degree_label , ticks2 $ degree_label )
444
+
445
+ left <- guide_axis(
446
+ tick_positions ,
447
+ tick_labels ,
422
448
position = " left" ,
423
449
theme = theme
424
- ),
425
- right = guide_axis(
426
- ticks_right $ y_end ,
427
- ticks_right $ degree_label ,
450
+ )
451
+ } else {
452
+ left <- zeroGrob()
453
+ }
454
+
455
+ # right axis
456
+ if (identical(panel_params $ graticule_labeling $ right , " E" ) ||
457
+ identical(panel_params $ graticule_labeling $ right , " N" )) {
458
+ # we don't generally know which direction graticules run, so need to consider both
459
+ ticks1 <- graticule [graticule $ type == panel_params $ graticule_labeling $ right &
460
+ graticule $ x_start > 0.999 , ]
461
+ ticks2 <- graticule [graticule $ type == panel_params $ graticule_labeling $ right &
462
+ graticule $ x_end > 0.999 , ]
463
+ tick_positions <- c(ticks1 $ y_start , ticks2 $ y_end )
464
+ tick_labels <- c(ticks1 $ degree_label , ticks2 $ degree_label )
465
+
466
+ right <- guide_axis(
467
+ tick_positions ,
468
+ tick_labels ,
428
469
position = " right" ,
429
470
theme = theme
430
471
)
431
- )
472
+ } else {
473
+ right <- zeroGrob()
474
+ }
475
+
476
+ list (left = left , right = right )
432
477
}
433
478
434
479
)
@@ -448,21 +493,28 @@ sf_rescale01_x <- function(x, range) {
448
493
}
449
494
450
495
451
- # ' @param crs Use this to select a specific CRS. If not specified, will
452
- # ' use the CRS defined in the first layer.
496
+ # ' @param crs Use this to select a specific coordinate reference system (CRS).
497
+ # ' If not specified, will use the CRS defined in the first layer.
453
498
# ' @param datum CRS that provides datum to use when generating graticules
499
+ # ' @param graticule_labeling Named list of character values specifying which
500
+ # ' graticules (meridians or parallels) should be labeled on which side of the
501
+ # ' plot. Meridians are indicated by `"E"` (for East) and parallels by `"N"`
502
+ # ' (for North). Default is `list(top = NA, right = NA, bottom = "E",
503
+ # ' left = "N")` to label parallels on the left and meridians at the bottom.
454
504
# ' @param ndiscr number of segments to use for discretising graticule lines;
455
505
# ' try increasing this when graticules look unexpected
456
506
# ' @inheritParams coord_cartesian
457
507
# ' @export
458
508
# ' @rdname ggsf
459
509
coord_sf <- function (xlim = NULL , ylim = NULL , expand = TRUE ,
460
- crs = NULL , datum = sf :: st_crs(4326 ), ndiscr = 100 ,
461
- default = FALSE ) {
510
+ crs = NULL , datum = sf :: st_crs(4326 ),
511
+ graticule_labeling = list (top = NA , right = NA , bottom = " E" , left = " N" ),
512
+ ndiscr = 100 , default = FALSE ) {
462
513
ggproto(NULL , CoordSf ,
463
514
limits = list (x = xlim , y = ylim ),
464
515
datum = datum ,
465
516
crs = crs ,
517
+ graticule_labeling = graticule_labeling ,
466
518
ndiscr = ndiscr ,
467
519
expand = expand ,
468
520
default = default
0 commit comments