I have a function that returns a ggplot object that already has a line type scale. Now I want to change the scale. If I simply add a new scale, I get a message: "Scale for '...' is already present."
The question how to suppress that message has already been asked on stackoverflow (e.g. Supressing Warnings in scale_x_datetime). The only simple solution seems to be to suppress all messages - which I don't want, in order not to overlook something. So the natural alternative would be: Remove the present scale first, and then add another scale.
Can that be done easily?
Example (under the assumption that I'm not allowed to change the definition of f
):
> f <- function() ggplot(mtcars, aes(x = mpg, y = hp)) + geom_point() + xlim(c(11, 34))
> g <- f()
> g + xlim(c(9, 36))
Scale for 'x' is already present. Adding another scale for 'x', which will replace the existing scale.