TIL what happens if you use %>% instead of + in ggplot2

[This article was first published on Citizen-Statistician » R Project, and kindly contributed to R-bloggers]. (You can report issue about the content on this page here)
Want to share your content on R-bloggers? click here if you have a blog, or here if you don't.

This post is about ggplot2 and dplyr packages, so let’s start with loading them:

 
library(ggplot2) 
library(dplyr) 

I can’t be the first person to make the following mistake:

 
ggplot(mtcars, aes(x = wt, y = mpg)) %>%
    geom_point() 

Can you spot the mistake in the code above? Look closely at the end of the first line.

The operator should be the + used in ggplot2 for layering, not the %>% operator used in dplyr for piping, like this:

 
ggplot(mtcars, aes(x = wt, y = mpg)) +
    geom_point() 

So what happens if you accidentally use the pipe operator instead of the +? You get the following error:

Error in get(x, envir = this, inherits = inh)(this, ...) : 
 Mapping should be a list of unevaluated mappings created by aes or aes_string

My Google search for this error did not yield my careless mistake as a potential cause. Since many people use these two packages together, I’m guessing such mix-up of operators can’t be too uncommon (right? I can’t be the only one…). So I’m leaving this post here for the next person who makes the same mistake.

 

To leave a comment for the author, please follow the link and comment on their blog: Citizen-Statistician » R Project.

R-bloggers.com offers daily e-mail updates about R news and tutorials about learning R and many other topics. Click here if you're looking to post or find an R/data-science job.
Want to share your content on R-bloggers? click here if you have a blog, or here if you don't.

Never miss an update!
Subscribe to R-bloggers to receive
e-mails with the latest R posts.
(You will not see this message again.)

Click here to close (This popup will not appear again)