f <- function(PLOT, TITLE) {
PLOT + ggtitle(TITLE)
}
Calling the function directly works as expected.
However, calling the function via do.call(f, ..)
throws an error when TITLE
is a language
object
## Sample Data
TIT <- bquote(atop("This is some text", atop(italic("Here is some more text"))))
P <- qplot(x=1:10, y=1:10, geom="point")
## WORKS FINE
f(P, TIT)
## FAILS
do.call(f, list(P, TIT))
## Error in labs(title = label) : could not find function "atop"
This of course only happens when TIT
is a language object
TIT.char <- "This is some text\nHere is some more text"
do.call(f, list(P, TIT.char))
## No Error
How can do.call()
be used correctly when arguments are language objects?
Inside ggtitle () function, we can directly write the title that we want to add to the plot without defining any parameter but for add Subtitle to the plot using ggtitle () function, we have to use subtitle parameter to ggtitle () function and then assign the subtitle to that parameter.
The argument label is the text to be used for the main title or for the axis labels. ToothGrowth data is used in the following examples. Make sure that the variable dose is converted as a factor using the above R script.
p + ggtitle (label = "Effect of Vitamin C on Tooth Growth" , subtitle = "Plot of length by dose") If the title is too long, you can split it into two or multiple lines using . In this case you can adjust the space between text lines by specifying the argument lineheight in the theme function element_text ():
ggtitle(label) xlab(label) ylab(label) labs(...) The argument label is the text to be used for the main title or for the axis labels. ToothGrowth data is used in the following examples.
Use
do.call(f, list(P, TIT), quote=TRUE)
instead. The problem is that your expression is being evaluated when you run do.call. By setting quote=TRUE
it will quote the arguments to leave them un-evaluated when passing them along to f
. You can also explicitly quote TIT
do.call(f, list(P, quote(TIT)))
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