使用 do.call() 将参数传递给 scale_y_continuous() 中的标签函数会产生错误
我想制作一个 ggplot,其 y 轴标签由预制列表格式化。我发现,如果我直接将参数传递给 ggplot 中的 scale_y_continuous()
函数中的 labels
选项,它可以正常工作,但如果我通过 do.call 传递它们
它会抛出一个错误,即使(我认为)这些是等效的。
这是一个例子:
library(scales)
# make the list of arguments
fn_args = list(prefix = "$", big.mark = ",", decimal.mark = ".")
# print an example of a number formatted with these arguments using do.call()
do.call(number, as.list(append(x=10001, fn_args)))
##> [1] "P10,001S"
# print the same number but put the arguments directly in the function
number(10001, prefix = "P", suffix = "S", big.mark = ",", decimal.mark = ".")
##> [1] "P10,001S"
# Great -- they're identical, as expected.
# Now, let's make plots:
library(ggplot2)
# this works and produces a plot with the y-axis formatted nicely
ggplot(mtcars, aes(mpg, cyl)) +
geom_line() +
scale_y_continuous(labels=function(s) number(x = s, prefix = "P", suffix = "S", big.mark = ",", decimal.mark = "."))
# But this one doesn't work:
ggplot(mtcars, aes(mpg, cyl)) +
geom_line() +
scale_y_continuous(labels=function(s) do.call(number, as.list(append(x=s, fn_args))))
##> Error in `f()`:
##> ! Breaks and labels are different lengths
我不明白为什么会出现此错误,因为我认为这两个公式是相同的。
我很感激任何人的见解!
谢谢。
I want to make a ggplot for which the y-axis labels are formatted by a pre-made list. I have found that if I pass arguments to the labels
option in scale_y_continuous()
function in ggplot directly it works fine, but if I pass them via do.call
it throws an error, even though (I think) these are equivalent.
Here is an example:
library(scales)
# make the list of arguments
fn_args = list(prefix = "quot;, big.mark = ",", decimal.mark = ".")
# print an example of a number formatted with these arguments using do.call()
do.call(number, as.list(append(x=10001, fn_args)))
##> [1] "P10,001S"
# print the same number but put the arguments directly in the function
number(10001, prefix = "P", suffix = "S", big.mark = ",", decimal.mark = ".")
##> [1] "P10,001S"
# Great -- they're identical, as expected.
# Now, let's make plots:
library(ggplot2)
# this works and produces a plot with the y-axis formatted nicely
ggplot(mtcars, aes(mpg, cyl)) +
geom_line() +
scale_y_continuous(labels=function(s) number(x = s, prefix = "P", suffix = "S", big.mark = ",", decimal.mark = "."))
# But this one doesn't work:
ggplot(mtcars, aes(mpg, cyl)) +
geom_line() +
scale_y_continuous(labels=function(s) do.call(number, as.list(append(x=s, fn_args))))
##> Error in `f()`:
##> ! Breaks and labels are different lengths
I don't understand why I'm getting this error, since I thought those two formulations were identical.
I'd appreciate anyone's insights!
Thank you.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
我们需要一个扁平的
list
- 在OP的代码中,append
第一个参数是x
,因此x=s
,假设它是为 'x' 参数传递的值,而不是命名向量。我们可能需要scale_y_continuous(labels=function(s) do.call(number,append(list(c(x=s)), fn_args)))We need a flattened
list
- In the OP's code, theappend
first argument isx
, thusx=s
, assumes it is the value passed for the 'x' argument and not a named vector. We may needscale_y_continuous(labels=function(s) do.call(number, append(list(c(x=s)), fn_args)))