Why can’t ggplot2 use %>% instead of “+”?

Have you ever mixed up dplyr’s pipe operator or magittr %>% with ggplot’s “+” while piping dplyr commands with ggplot2, you are not alone. Depending on where you misused “%>%” for “+”, you might get confusing errors like

  • Error: Mapping must be created by `aes()` or `aes_()`
  • Error in as.vector(x, “character”) : cannot coerce type ‘environment’ to vector of type ‘character’

Well. that is about to change, Hadley Wickham has been working on meaningful errors for tidyverse, like “Did you use %>% instead of +?

Still, if you wonder, why can’t I use %>% in ggplot2 instead of “+”?. Hadley Wickham has commented on RStudio community.

Here is the short answer from Hadley Wickham,

Should ggplot2 use the pipe? IMO, yes.
Could ggplot2 support both the pipe and plus? No
Would it be worth it to create a ggplot3 that uses the pipe? No.

Check out the complete answer at
https://community.rstudio.com/t/why-cant-ggplot2-use/4372

If you still want to use %>%, try this package https://github.com/zeehio/ggpipe