I am trying to write a function that uses ggplot but allows user specification of several of the plotting variables. However I'm having trouble getting it to work as a function (receiving an error message: see below).
A small example dataset and working implementation are provided below, together with my attempt at the function and the associated error. I'm sure it is to do with non-standard evaluation (NSE), but I'm unsure how to get around it given my use of filter within the function, and my various attempts have been in vain.
library(dplyr)
library(ggplot2)
df<-data.frame(Date=c(seq(1:50),seq(1:50)), SRI=runif(100,-2,2), SITE=(c(rep("A",50), rep("B", 50))))
ggplot() +
geom_linerange(aes(x = Date, ymin = 0, ymax = SRI), colour = I('blue'), data = filter(df, SRI>0)) +
geom_linerange(aes(x = Date, ymin = SRI, ymax = 0), colour = I('red'), data = filter(df, SRI<=0)) +
facet_wrap(~SITE) +
labs(x = 'Date', y = "yvar", title = "Plot title")
The above works, but when implemented as a function:
plot_fun <- function(df, x, y, ylab="y-lab", plot_title="Title", facets) {
ggplot() +
geom_linerange(aes(x = x, ymin = 0, ymax = y), colour = I('blue'), data = filter(df, y > 0)) +
geom_linerange(aes(x = x, ymin = y, ymax = 0), colour = I('red'), data = filter(df, y <= 0)) +
facet_wrap(~ facets) +
labs(x = 'Date', y = ylab, title = plot_title)
return(p)
}
plot_fun(df, x="Date", y="SRI", ylab="y-lab", plot_title="Title", facets="SITE")
I get the following "Error: Aesthetics must be either length 1 or the same as the data (1): x, ymin, max".
I've tried various approaches using as_string and filter_, but all have been unsuccessful.
Any help much appreciated.
Regards
Nick

xargument in the function you posted, which is probably what is causing your reported error message. Your current function gives a different error message and won't run due tofacet_wrap/yourfacetsargument, which you can solve with one of the answers to this questionfilter_(but looks like might get less verbose in the near future ).