Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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

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()
like image 868
Anthony Ebert Avatar asked Feb 11 '16 04:02

Anthony Ebert


People also ask

What does %>% mean in Ggplot?

1. the %>% is a pipe operator that is actually part of the dplyr library (along with the filter function) not from the ggplot2 library. To sample 1%, there is a sample_frac function in the dplyr library.

What does the %>% operator do?

The tee ( %T>% ) operator allows you to continue piping functions that normally cause termination. The compound assignment %<>% operator is used to update a value by first piping it into one or more expressions, and then assigning the result.

Is %>% the same as in R?

1 Answer. %>% is called the forward pipe operator in R.

What does Magrittr do in R?

The magrittr (to be pronounced with a sophisticated french accent) package has two aims: decrease development time and improve readability and maintainability of code. Or even shortr: make your code smokin' (puff puff)!


1 Answers

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()
like image 130
Gregor Thomas Avatar answered Sep 21 '22 15:09

Gregor Thomas