I've been trying to cut down on the amount of copying and pasting required to make a large number of charts with slightly differing functions / slices of the data.
Here is a simplified example of what I am trying to do:
test <- data.table(a=c("x","y"), b=seq(1,3), c=rnorm(18))
fixedSlices <- function(input, rowfacet, colfacet, metric){
calc <- substitute(metric)
bygroup<-c(rowfacet,colfacet)
aggregates <- input[,eval(calc),by=bygroup]
ggplot(aggregates) + geom_point(stat="identity") + aes(x="", y=V1) + facet_grid(a ~ b)
}
fixedSlices(test, "a", "b", mean(c)) #works
dynamicSlices <- function(input, rowfacet, colfacet, metric){
calc <- substitute(metric)
bygroup<-c(rowfacet,colfacet)
aggregates <- input[,eval(calc),by=bygroup]
ggplot(aggregates) + geom_point(stat="identity") + aes(x="", y=V1) + facet_grid(eval(rowfacet) ~ eval(colfacet))
}
dynamicSlices(test, "a", "b", mean(c))
#Error in layout_base(data, rows, drop = drop) : At least one layer must contain all variables used for facetting
I'd like to be able to have my function accept the variables to facet by as parameters. I was able to get this to work with respect to grouping by the columns in the data.table, but can't facet by them in ggplot.
You should use
... + facet_grid(as.formula(sprintf("%s ~ %s", rowfacet, colfacet))
in your code.
facet_grid()
takes a formula object, and you can create a formula object from a string with as.formula()
. So you should be able to do something like:
facet_grid(as.formula(paste(rowfacet, "~", colfacet)))
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With