使用 do.call() 将参数传递给 scale_y_continuous() 中的标签函数会产生错误
Pass arguments to labels function in scale_y_continuous() with do.call() yields error
我想制作一个 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
我不明白为什么会出现此错误,因为我认为这两个公式是相同的。
非常感谢任何人的见解!
谢谢。
我们需要一个扁平化的 list
- 在 OP 的代码中,append
第一个参数是 x
,因此 x=s
,假设它是为'x' 参数而不是命名向量。我们可能需要 scale_y_continuous(labels=function(s) do.call(number, append(list(c(x=s)), fn_args)))
library(ggplot2)
ggplot(mtcars, aes(mpg, cyl)) +
geom_line() +
scale_y_continuous(labels=function(s)
do.call(number, c(list(x=s), fn_args)))
我想制作一个 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
我不明白为什么会出现此错误,因为我认为这两个公式是相同的。
非常感谢任何人的见解!
谢谢。
我们需要一个扁平化的 list
- 在 OP 的代码中,append
第一个参数是 x
,因此 x=s
,假设它是为'x' 参数而不是命名向量。我们可能需要 scale_y_continuous(labels=function(s) do.call(number, append(list(c(x=s)), fn_args)))
library(ggplot2)
ggplot(mtcars, aes(mpg, cyl)) +
geom_line() +
scale_y_continuous(labels=function(s)
do.call(number, c(list(x=s), fn_args)))