How to define aesthetic mappings of ggplot2 within another function
ggplot2 is great for visualization, but sometimes we need to include ggplot2 functions within another function. ggplot() uses aes() to define aesthetic mappings of different variables. The question is: how to define aesthetic mappings when writing functions using ggplot()? In this post I will present several approaches. First of all, let’s load the required packages and some test data. library(ggplot2) library(dplyr) data("mtcars") Using aes_string() There are two standard ways to define aesthetic mappings in ggplot2, aes_string() and aes_(). Writing a function using aes_string() is the same as using aes(), but calling the function requires passing the variable names as strings to the function.
2020-03-28
1 min read