This
was a very helpful post about making a ggplot2 call into a
function. Key seems to be the use of the embrace
operator which relates to Tidy Evaluation.
This SO answer provides
some additional very similar examples of tidy_eval for
dplyr and tables.
library(ggplot2)
## Warning: package 'ggplot2' was built under R version 4.2.1
data("mtcars")
scatter_plot <- function(data, x, y, title) {
ggplot(data, aes({{x}}, {{y}})) +
geom_point()+
labs(title= {{title}})
}
scatter_plot(mtcars, drat, hp, "Drat by HP")
# Add something to the resulting plot
scatter_plot(mtcars, drat, hp, "Drat by HP with trend") +
geom_smooth()
## `geom_smooth()` using method = 'loess' and formula 'y ~ x'