I write many packages where the generic plot is a ggplot2. A ggplot call, unlike other R function calls, is layered so one could end up with several options (separated by + signs) to get a plot looking just right. However, I don't want someone to suffer through my pre-defined options and would like them to be able to customize it without re-writing my function from scratch. How do I accomplish this?
With a traditional function, I could use the three dot operator to pass optional arguments. This seems harder with a ggplot.
Reproducible example
f <- function(df) {
custom_plot <- ggplot(df, aes(mpg, disp, color = gear)) +
geom_point(size = 3) +
theme(panel.background = element_blank(), panel.grid.major = element_blank(),
panel.grid.minor = element_blank(), panel.border = element_blank(),
axis.line = element_line(colour = "black"))
return(custom_plot)
}
To generate a plot
f(mtcars)
produces this:

How do I generalize this function such that someone could pass additional or different options to this plot (especially in cases where it is the generic plot)?
If my function were defined as:
f <- function(df, ...)
How would I pass those in to my ggplot call?
+as needed outside the function.