What is the difference between the “+” operator in ggplot2 and the “%>%” operator in magrittr?

江枫思渺然 提交于 2021-02-03 06:39:01

问题


What is the difference between the "+" operator in ggplot2 and the "%>%" operator in magrittr?

I was told that they are the same, however if we consider the following script.

library(magrittr)
library(ggplot2)

# 1. This works
ggplot(data = mtcars, aes(x=wt, y = mpg)) + geom_point()

# 2. This works
ggplot(data = mtcars) + aes(x=wt, y = mpg) + geom_point()

# 3. This works
ggplot(data = mtcars) + aes(x=wt, y = mpg) %>% geom_point()

# 4. But this doesn't
ggplot(data = mtcars) %>% aes(x=wt, y = mpg) %>% geom_point()

回答1:


Piping is very different from ggplot2's addition. What the pipe operator, %>%, does is take the result of the left-hand side and put it as the first argument of the function on the right-hand side. For example:

1:10 %>% mean()
# [1] 5.5

Is exactly equivalent to mean(1:10). The pipe is more useful to replace multiply nested functions, e.g.,

x = factor(2008:2012)
x_num = as.numeric(as.character(x))
# could be rewritten to read from left-to-right as
x_num = x %>% as.character() %>% as.numeric()

but this is all explained nicely over at What does %>% mean in R?, you should read through that for a couple more examples.

Using this knowledge, we can re-write your pipe examples as nested functions and see that they still do the same things; but now it (hopefully) is obvious why #4 doesn't work:

# 3. This is acceptable ggplot2 syntax
ggplot(data = mtcars) + geom_point(aes(x=wt, y = mpg))

# 4. This is not
geom_point(aes(ggplot(data = mtcars), x=wt, y = mpg))

ggplot2 includes a special "+" method for ggplot objects, which it uses to add layers to plots. I didn't know until you asked your question that it also works with the aes() function, but apparently that's defined as well. These are all specially defined within ggplot2. The use of + in ggplot2 predates the pipe, and while the usage is similar, the functionality is quite different.

As an interesting side-note, Hadley Wickham (the creator of ggplot2) said that:

...if I'd discovered the pipe earlier, there never would've been a ggplot2, because you could write ggplot graphics as

ggplot(mtcars, aes(wt, mpg)) %>%
  geom_point() %>%
  geom_smooth()


来源:https://stackoverflow.com/questions/35332861/what-is-the-difference-between-the-operator-in-ggplot2-and-the-operato

标签
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!