@@ -467,8 +467,29 @@ theme <- function(...,
467
467
strip.switch.pad.wrap ,
468
468
complete = FALSE ,
469
469
validate = TRUE ) {
470
+
470
471
elements <- find_args(... , complete = NULL , validate = NULL )
472
+ elements <- fix_theme_deprecations(elements )
473
+ elements <- validate_theme_palettes(elements )
471
474
475
+ # If complete theme set all non-blank elements to inherit from blanks
476
+ if (complete ) {
477
+ elements <- lapply(elements , function (el ) {
478
+ if (is.theme_element(el ) && ! inherits(el , " element_blank" )) {
479
+ el $ inherit.blank <- TRUE
480
+ }
481
+ el
482
+ })
483
+ }
484
+ structure(
485
+ elements ,
486
+ class = c(" theme" , " gg" ),
487
+ complete = complete ,
488
+ validate = validate
489
+ )
490
+ }
491
+
492
+ fix_theme_deprecations <- function (elements ) {
472
493
if (is.unit(elements $ legend.margin ) && ! is.margin(elements $ legend.margin )) {
473
494
cli :: cli_warn(c(
474
495
" {.var legend.margin} must be specified using {.fn margin}" ,
@@ -511,22 +532,46 @@ theme <- function(...,
511
532
elements $ legend.position.inside <- elements $ legend.position
512
533
elements $ legend.position <- " inside"
513
534
}
535
+ elements
536
+ }
514
537
515
- # If complete theme set all non-blank elements to inherit from blanks
516
- if (complete ) {
517
- elements <- lapply(elements , function (el ) {
518
- if (is.theme_element(el ) && ! inherits(el , " element_blank" )) {
519
- el $ inherit.blank <- TRUE
520
- }
521
- el
522
- })
538
+ validate_theme_palettes <- function (elements ) {
539
+
540
+ pals <- c(" palette.colour.discrete" , " palette.colour.continuous" ,
541
+ " palette.fill.discrete" , " palette.fill.continuous" ,
542
+ " palette.color.discrete" , " palette.color.continuous" )
543
+ if (! any(pals %in% names(elements ))) {
544
+ return (elements )
523
545
}
524
- structure(
546
+
547
+ # Standardise spelling
548
+ elements <- replace_null(
525
549
elements ,
526
- class = c(" theme" , " gg" ),
527
- complete = complete ,
528
- validate = validate
550
+ palette.colour.discrete = elements $ palette.color.discrete ,
551
+ palette.colour.continuous = elements $ palette.color.continuous
529
552
)
553
+ elements $ palette.color.discrete <- NULL
554
+ elements $ palette.color.continuous <- NULL
555
+
556
+ # Check for incompatible options
557
+ pals <- c(" palette.colour.discrete" , " palette.colour.continuous" ,
558
+ " palette.fill.discrete" , " palette.fill.continuous" )
559
+ opts <- c(" ggplot2.discrete.colour" , " ggplot2.continuous.colour" ,
560
+ " ggplot2.discrete.fill" , " ggplot2.continuous.fill" )
561
+ index <- which(pals %in% names(elements ))
562
+
563
+ for (i in index ) {
564
+ if (is.null(getOption(opts [i ]))) {
565
+ next
566
+ }
567
+ cli :: cli_warn(c(
568
+ " The {.code options('{opts[i]}')} setting is incompatible with the \\
569
+ {.arg {pals[i]}} theme setting." ,
570
+ i = " You can set {.code options({opts[i]} = NULL)}."
571
+ ))
572
+ }
573
+
574
+ elements
530
575
}
531
576
532
577
# ' @export
0 commit comments