Reputation: 339
This is an example of ggplot2 Elegant Graphics for Data Analysis
geom_mean <- function(..., bar.params = list(), errorbar.params = list()) {
params <- list(...)
bar.params <- modifyList(params, bar.params)
errorbar.params <- modifyList(params, errorbar.params)
bar <- do.call("stat_summary", modifyList(
list(fun.y = "mean", geom = "bar", fill = "grey70"),
bar.params)
)
errorbar <- do.call("stat_summary", modifyList(
list(fun.data = "mean_cl_normal", geom = "errorbar", width = 0.4),
errorbar.params)
)
list(bar, errorbar)
}
It shows
In this code, what's do.call()
roll?
I don't know why this code needs do.call()
.
What is the another function to replace do.call()
?
Upvotes: 0
Views: 88
Reputation: 190
do.call
invokes a function using arguments provided as a list.
For example,
do.call("plot", list(x = 1:10, y = 1:10))
will result to
plot(x = 1:10, y = 1:10)
Such approach allows to work with arbitrary argument number, which are caught by
params <- list(...)
Then you can first modify some of them as you wish and later call the function stat_summary
with this modified list:
do.call("stat_summary", params)
The reason is that stat_summary
itself takes an arbitrary number of arguments (see ...
in its documentation). If you are to describe all possible argument names,
which user might provide, it would result to a lot of unflexible code
(imagine, that stat_summary
will get more options in the next version and
have always to update your code accordingly).
Upvotes: 2