[R] tidy evaluation in ggplot2

[This article was first published on R on Zhenguo Zhang's Blog, 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.
Zhenguo Zhang’s Blog /2023/10/23/r-tidy-evaluation-in-ggplot2/ –

Since ggplot2 version 3.0.0, it started to support tidy evaluation, and use the technique to replace aes_ and aes_string(). In this post, I would like to show how the syntax of ggplot2 changes accordingly.

knitr::opts_chunk$set(echo=T, fig.align = "center", fig.width = 6, fig.height = 5, dpi=150, warning=FALSE)
library(knitr)
library(ggplot2)

Let’s start with an example of making a scatter plot with the dataset mtcars.

library(ggplot2)
plt<-ggplot(mtcars, aes(x=wt, y=mpg, color=factor(cyl))) + theme_bw()
plt<-plt+geom_point()
plt

But what if we create a function to make the plot and accept different variables for the color aesthetics?

make_scatter_plot<-function(colorVar) {
  plt<-ggplot(mtcars, aes(x=wt, y=mpg, color=factor(colorVar))) + theme_bw()
  plt<-plt+geom_point()
  plt
}
make_scatter_plot("cyl")

As you can see, this doesn’t work as expected: it actually created a new variable which has only one value “cyl” for the fill aesthetics. What we need is to use the cyl column of the mtcars dataset to fill the colors.

Here is tidy evaluation kicks in: we use enquo() to quote an input variable, and then unquote it with !!, as shown in the function below:

make_scatter_plot<-function(colorVar) {
  colorVar<-enquo(colorVar)
  plt<-ggplot(mtcars, aes(x=wt, y=mpg, color=factor(!!colorVar))) + theme_bw()
  plt<-plt+geom_point()
  plt
}
make_scatter_plot(cyl)

Here we used the function ggplot2::enquo() which delayed the evaluation of an expression, and it will only be evaluated when prefixed with !!. This is the key feature of tidy evaluation. With this function, we can call a different column to color it, for example:

make_scatter_plot(gear)

Note that you can’t put the variable name in a quote, such as “gear”, and it would not work.

Another useful function is ggplot2::vars(), it can quasiquote more than one variables and be used in functions such as facet_wrap() and facet_grid(). Let’s see an example:

make_scatter_plot(gear) + facet_wrap(vars(am), labeller=label_both)

We can also put both color and facet variable in another function make_scatter_plot_facet which calls make_scatter_plot. As you can see, the color variable need be enquo and unquote (use !!) in this wrapping function too.

make_scatter_plot_facet<-function(colorVar, facetVar) {
  #facetName<-quo_name(facetVar)
  facetVar<-enquo(facetVar)
  colorVar<-enquo(colorVar)
  make_scatter_plot(!!colorVar) + facet_wrap(vars(!!facetVar), labeller=label_both)
}

make_scatter_plot_facet(gear, am)

This is just the first post on tidy evaluation. More is coming.

Happy programming 😄

- /2023/10/23/r-tidy-evaluation-in-ggplot2/ -
To leave a comment for the author, please follow the link and comment on their blog: R on Zhenguo Zhang's Blog.

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)