Open clauswilke opened 4 years ago
To add my two cents, the very message can be misleading as well as unnecessary:
ggplot(mtcars, aes(disp, mpg)) +
geom_point() +
xlim(100, 300) +
scale_x_continuous(name = "Displacement")
#> Scale for 'x' is already present. Adding another scale for 'x', which will
#> replace the existing scale.
Even if it is not a warning, the message suggests that something has been replaced, thus overwritten. However, the two scales have only been merged since the name is intact.
A user could think that something is wrong although everything is fine.
A problem that pops up repeatedly in different contexts is that we may want to adjust certain scale parameters without explicitly calling a scale function. We already have some support for this, e.g. with
xlim()
to set limits on the x position scale orxlab()
to set its title, but there are numerous other scenarios where the same problem arises. Here are some examples:drop = FALSE
on discrete scales: https://github.com/tidyverse/ggplot2/issues/4268#issuecomment-729224543It would be good if we could come up with a generic approach to these issues. I note that
xlim()
andxlab()
use entirely different implementation approaches currently, so that's not promising:xlim()
andylim()
use theggplot2:::limits()
function to automatically generate a scale function and give it the appropriate parameter settings: https://github.com/tidyverse/ggplot2/blob/3be0acce79dc295d5470d949c60de1b9c84a6fc9/R/limits.r#L112-L160xlab()
andylab()
ultimately feed their info to theupdate_labels()
function, which writes to a specialplot$labels
field that the layout reads from: https://github.com/tidyverse/ggplot2/blob/3aa29375167946b970692e42241e9aabaee3ae03/R/labels.r#L13-L17 https://github.com/tidyverse/ggplot2/blob/660aad2db2b3495ae0d8040915a40d247133ffc0/R/plot-build.r#L170 https://github.com/tidyverse/ggplot2/blob/b76fa9639215785f8e94874d3bdf02225bae898a/R/layout.R#L107-L113Neither approach generalizes directly to the other use cases. I also have concerns about the way
xlim()
andylim()
are implemented, because the fact that they add explicit scales functions is not obvious and can lead to strange behavior. See e.g. the following reprex, where in the first plot the call toscale_x_continuous()
overwrites thexlim()
call but in the second plot thexlab()
call is not overwritten:Created on 2020-11-17 by the reprex package (v0.3.0)