@@ -18,8 +18,8 @@ update_labels <- function(p, labels) {
1818
1919# Called in `ggplot_build()` to set default labels not specified by user.
2020setup_plot_labels <- function (plot , layers , data ) {
21- # Initiate from user-defined labels
22- labels <- plot $ labels
21+ # Initiate empty labels
22+ labels <- list ()
2323
2424 # Find labels from every layer
2525 for (i in seq_along(layers )) {
@@ -65,7 +65,26 @@ setup_plot_labels <- function(plot, layers, data) {
6565 labels <- defaults(labels , current )
6666 }
6767 }
68- labels
68+
69+ # Warn for spurious labels that don't have a mapping.
70+ # Note: sometimes, 'x' and 'y' might not have a mapping, like in
71+ # `geom_function()`. We can display these labels anyway, so we include them.
72+ plot_labels <- plot $ labels
73+ known_labels <- c(names(labels ), fn_fmls_names(labs ), " x" , " y" )
74+ extra_labels <- setdiff(names(plot_labels ), known_labels )
75+
76+ if (length(extra_labels ) > 0 ) {
77+ extra_labels <- paste0(
78+ " {.code " , extra_labels , " = \" " , plot_labels [extra_labels ], " \" }"
79+ )
80+ names(extra_labels ) <- rep(" *" , length(extra_labels ))
81+ cli :: cli_warn(c(
82+ " Ignoring unknown labels:" ,
83+ extra_labels
84+ ))
85+ }
86+
87+ defaults(plot_labels , labels )
6988}
7089
7190# ' Modify axis, legend, and plot labels
@@ -168,7 +187,7 @@ ggtitle <- function(label, subtitle = waiver()) {
168187# ' text from the information stored in the plot.
169188# '
170189# ' @param p a ggplot object
171- # ' @param ... Currently ignored
190+ # ' @inheritParams rlang::args_dots_used
172191# '
173192# ' @return A text string
174193# '
@@ -191,6 +210,7 @@ ggtitle <- function(label, subtitle = waiver()) {
191210# ' get_alt_text(p)
192211# '
193212get_alt_text <- function (p , ... ) {
213+ warn_dots_used()
194214 UseMethod(" get_alt_text" )
195215}
196216# ' @export
0 commit comments