@@ -76,21 +76,20 @@ geom_abline <- function(mapping = NULL, data = NULL,
7676 show.legend = NA ) {
7777
7878 # If nothing set, default to y = x
79- if (missing (mapping ) && missing(slope ) && missing(intercept )) {
79+ if (is.null (mapping ) && missing(slope ) && missing(intercept )) {
8080 slope <- 1
8181 intercept <- 0
8282 }
8383
8484 # Act like an annotation
8585 if (! missing(slope ) || ! missing(intercept )) {
8686
87- # Warn if supplied mapping is going to be overwritten
88- if (! missing(mapping )) {
89- warning(paste0(" Using `intercept` and/or `slope` with `mapping` may" ,
90- " not have the desired result as mapping is overwritten" ,
91- " if either of these is specified\n "
92- )
93- )
87+ # Warn if supplied mapping and/or data is going to be overwritten
88+ if (! is.null(mapping )) {
89+ warn_overwritten_args(" geom_abline()" , " mapping" , c(" slope" , " intercept" ))
90+ }
91+ if (! is.null(data )) {
92+ warn_overwritten_args(" geom_abline()" , " data" , c(" slope" , " intercept" ))
9493 }
9594
9695 if (missing(slope )) slope <- 1
@@ -141,3 +140,34 @@ GeomAbline <- ggproto("GeomAbline", Geom,
141140
142141 draw_key = draw_key_abline
143142)
143+
144+ warn_overwritten_args <- function (fun_name , overwritten_arg , provided_args , plural_join = " and/or " ) {
145+ overwritten_arg_text <- paste0(" `" , overwritten_arg , " `" )
146+
147+ n_provided_args <- length(provided_args )
148+ if (n_provided_args == 1 ) {
149+ provided_arg_text <- paste0(" `" , provided_args , " `" )
150+ verb <- " was"
151+ } else if (n_provided_args == 2 ) {
152+ provided_arg_text <- paste0(" `" , provided_args , " `" , collapse = plural_join )
153+ verb <- " were"
154+ } else {
155+ provided_arg_text <- paste0(
156+ paste0(" `" , provided_args [- n_provided_args ], " `" , collapse = " , " ),
157+ " ," , plural_join ,
158+ " `" , provided_args [n_provided_args ], " `"
159+ )
160+ verb <- " were"
161+ }
162+
163+ warning(
164+ sprintf(
165+ " %s: Ignoring %s because %s %s provided." ,
166+ fun_name ,
167+ overwritten_arg_text ,
168+ provided_arg_text ,
169+ verb
170+ ),
171+ call. = FALSE
172+ )
173+ }
0 commit comments