Follow

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use
Contact

Why does ggplot change the position of the parameter values of data= and mapping= when using "pipe" operator?

hearted people who clicked in to see this question.I just started learning to use ggplot.

ggplot(mpg)
aes(x = displ) |> 
  ggplot(mpg)

image src

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 =?

MEDevel.com: Open-source for Healthcare and Education

Collecting and validating open-source software for healthcare, education, enterprise, development, medical imaging, medical records, and digital pathology.

Visit Medevel

The error goes like this:

img

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?

>Solution :

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 = _)
Add a comment

Leave a Reply

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use

Discover more from Dev solutions

Subscribe now to keep reading and get access to the full archive.

Continue reading