Hi. I’m Sharon Machlis at IDG Communications, here with episode 32 of Do More With R: Make
your own ggplot2 functions – without worrying about quoted or unquoted column names.
Tidyverse packages like ggplot2 and dplyr have a syntax that’s usually pretty handy:
You don’t have to put column names in quotation marks. Like you see here in this dplyr filter
code. The column name, mpg, is unquoted. That feature hasn’t been handy, though,
if you want to write your own R functions using tidyverse functions. That’s because
“regular” R usually needs quoted column names while tidyverse generally doesn’t.
But some good news: That problem now has a simple solution, thanks to the latest version
of the rlang package. Which means it’s now very easy to write your own ggplot functions
for your favorite customized graphs. Let me show you an example
I’m going to use data from Zillow with median home values by city neighborhoods across the
U.S. Here I’m loading a couple of packages and setting my data file name. The last line
of code uses base R’s download.file function to download a CSV from Zillow. I’m not going
to run that last line because I already downloaded the file.)
My last data prep steps: Import the CSV into R. I’m using the rio package because I love
rio, but you can use something else like read_csv or fread. Then I’m filtering just for rows
where the City is Boston. OK. The rest of this code creates a horizontal
bar chart with some customizations I often like to use. I’m ordering the bars from
highest to lowest values, outlining them in black, and coloring them in blue. I’m also
changing the default grey background. Now what if I’d like to make my own function
to generate a graph like this with any data frame? A function with input arguments the
data frame name, the x column, the y column, and the graph title?
Here’s one attempt to create a function called mybarplot with the customizations I
want. But if I try to run it using unquoted column names, like I’d do with ggplot, I
get an error (run line 11). If I quote the column names (run line 13) – well, I get
a graph, but not the one I want. This is the problem of base R needing quoted column names
while ggplot doesn’t. The old rlang package had a multi-step answer
for this. The current one solves it with a new operator called the tidy evaluation operator
– double curly braces. You just put that around the unquoted column names inside your
function, and you’re done!
You need at least version 0.4.0 of the rlang package for this to work. At the time I recorded
this video, it was on CRAN but you needed to compile it from source when given that
option during installation.
Here, I load rlang and then just tweak my barplot function so every time I refer to
a column name within ggplot inside my function, I surround it with double curly braces – “curly
curly” is how the package creators refer to it. Everything else is the same.
Now I can call my function. And, just like with tidyverse functions, I don’t need to
put the column names in quotation marks. It works!
And I can still tweak the graph created by my function, using ggplot commands.
Here I’m saving the result of my barplot function to a variable I’m calling mygraph.
It will display like any other ggplot object. Starting on line 4, I’m customizing the
graph a bit more – that geom_text() line adds the value onto each bar; the theme code
sets the graph headline size. And now you know how to save your favorite
ggplot graphs in your own custom functions for future use!
That’s it for this episode, thanks for watching! For more R tips, head to the Do More With
R page at go dot infoworld dot com slash more with R, all lowercase except for the R
You can also find the Do More With R playlist on the YouTube IDG Tech Talk channel.
Hope to see you next episode!