使用 do.call() 将参数传递给 scale_y_continuous() 中的标签函数会产生错误

发布于 2025-01-16 14:08:35 字数 1324 浏览 4 评论 0原文

我想制作一个 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 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

左耳近心 2025-01-23 14:08:35

我们需要一个扁平的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)))

We need a flattened list - In the OP's code, the append first argument is x, thus x=s, assumes it is the value passed for the 'x' argument and not a named vector. We may need 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)))
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文