TIL what happens if you use %>% instead of + in ggplot2
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.
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.