英文:
Why does ggplot change the position of the parameter values of data= and mapping= when using "pipe" operator?
问题
心地善良的人点击进来看这个问题。我刚开始学习如何使用ggplot。
ggplot(mpg)
aes(x = displ) |>
ggplot(mpg)
第一个命令mpg作为ggplot的data参数传递,而第二个命令具有相同的形式,但为什么mpg被传递为mapping参数的一个参数,而不是传递给mapping参数的aes函数作为参数呢?
错误如下所示:

看起来mpg被推到了一边。这里的逻辑是什么?
感谢您的耐心。
我尝试在谷歌上搜索并向朋友提问,但我无法得到答案。我是否犯了一些错误,还是这是R语言的某种特性?
英文:
hearted people who clicked in to see this question.I just started learning to use ggplot.
ggplot(mpg)
aes(x = displ) |>
ggplot(mpg)

The first command mpg is passed to ggplot as an argument to data =, while the second command is of the same form, but why mpg is passed as an argument to mapping = instead of the incoming aes function as an argument to mapping =?
The error goes like this:

It looks like mpg is being pushed to the side. What is the logic here?
Thank you for your patience.
I try to search on google and asked my friends. But I can't get an answer. Have I made some mistakes or is this some kind of feature of the R language?
答案1
得分: 1
The pipe passes its left hand side to the first unnamed argument of the right hand side. So aes(x = displ) |> ggplot(mpg) is equivalent to ggplot(aes(x = displ), mpg), which in turn is equivalent to ggplot(data = aes(x = displ), mapping = mpg) since that’s the order of ggplot()’s arguments.
If you want to get around this, you can explicitly pass mpg to the data arg:
aes(x = displ) |>
ggplot(data = mpg)
or explicitly pass your aes() call to mapping using the pipe placeholder (_):
aes(x = displ) |>
ggplot(mpg, mapping = _)
英文:
The pipe passes its left hand side to the first unnamed argument of the right hand side. So aes(x = displ) |> ggplot(mpg) is equivalent to ggplot(aes(x = displ), mpg), which in turn is equivalent to ggplot(data = aes(x = displ), mapping = mpg) since that’s the order of ggplot()’s arguments.
If you want to get around this, you can explicitly pass mpg to the data arg:
aes(x = displ) |>
ggplot(data = mpg)
or explicitly pass your aes() call to mapping using the pipe placeholder (_):
aes(x = displ) |>
ggplot(mpg, mapping = _)
通过集体智慧和协作来改善编程学习和解决问题的方式。致力于成为全球开发者共同参与的知识库,让每个人都能够通过互相帮助和分享经验来进步。


评论