I am frustrated from ggplot2
overwriting arguments that have been set before. In fact I encouter the described problem with different ggplot2
functions, but in order to illustrate what I mean I write only about scale_y_continuous()
. So let’s say I provide a different breaks
argument for scale_y_continuous()
as here:
df <- data.frame(sex= factor(sample(c("male", "female"), 100, replace= TRUE)))
library(ggplot2)
my_plot <- ggplot(df, aes(sex)) +
geom_bar() +
scale_y_continuous(breaks= 1:nrow(df))
my_plot
This returns the expected plot:
Let’s assume we want to change the y axis to start at zero. But if I add another argument in scale_y_continuous()
, then the former breaks
argument is changed back to default. See here:
my_plot2 <- my_plot +
scale_y_continuous(expand= expansion(mult= c(0, 0.1)))
my_plot2
How can I hinder ggplot2
from overwriting the breaks
argument that has been set before?
Why I ask: We have a function that produces different plots for all our data situations. This function does a bunch of things, one can provide different data or plots and it returns plots in the layout as we need them. The function itself is too big to share here. Anyway, it is frustrating that ggplot2
sets defined arguments back to default.