ggplot在使用“pipe”操作符时为什么会改变data=和mapping=参数值的位置?

huangapple go评论68阅读模式
英文:

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作为ggplotdata参数传递,而第二个命令具有相同的形式,但为什么mpg被传递为mapping参数的一个参数,而不是传递给mapping参数的aes函数作为参数呢?

错误如下所示:

ggplot在使用“pipe”操作符时为什么会改变data=和mapping=参数值的位置?

看起来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)

ggplot在使用“pipe”操作符时为什么会改变data=和mapping=参数值的位置?

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:

ggplot在使用“pipe”操作符时为什么会改变data=和mapping=参数值的位置?

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 = _)

huangapple
  • 本文由 发表于 2023年2月10日 12:11:37
  • 转载请务必保留本文链接:https://go.coder-hub.com/75406869.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定