In Hadley Wickham\'s ggplot2 book in chapter 10.3, he alludes to making plot functions. I want to make many similar plots that use faceting, but I cannot r
Here are some alternatives using new features from ggplot2 V3.0.0
Using strings :
flowerPlot <- function(dat, sl, sw, pl, sp){
ggplot(data=dat, aes(x=!!ensym(sl), y=!!ensym(sw), color=!!ensym(pl))) +
geom_point() +
facet_wrap(eval(expr(~!!ensym(sp))))
}
flowerPlot(iris, sl='Sepal.Length', sw='Sepal.Width', pl='Petal.Length', sp = 'Species')
Using names :
flowerPlot2 <- function(dat, sl, sw, pl, sp){
ggplot(data=dat, aes(x=!!enquo(sl), y=!!enquo(sw), color=!!enquo(pl))) +
geom_point() +
facet_wrap(eval(expr(~!!enquo(sp))))
}
flowerPlot2(iris, sl= Sepal.Length, sw=Sepal.Width, pl=Petal.Length, sp = Species)