Get startedGet started for free

A great ggplot twist

1. A great ggplot twist

So far, we've seen how to create functions including dplyr verbs with rlang. In this lesson, we'll explore how to use rlang with ggplot2 to customize our plots in a programmatic way.

2. ggplot2 basics

Let's first review the general syntax of ggplot2 code. We start by loading ggplot2 and calling the ggplot function. Here, we use world_bank_data as the data frame. Then we use the aes function to lay out how we'd like to map aesthetics of the plot with columns in the data. For example, we can use infant_mortality_rate for the single x aesthetic. Each layer of a plot gets attached with a plus sign before the next one in ggplot2. Next, we use one of the geom functions to specify which geometry we'd like on the plot. In this case, we make a histogram using the geom_histogram function. Recall we can also add in arguments here, like setting the color of the bar outline to white. This produces the plot on the right showing the different values and their occurrences for infant mortality rate in this data.

3. Adding a title

Recall we can go one step further and add a title to our ggplot using the ggtitle function, which expects a label argument as a string. We can see the string at the top of the plot.

4. Wrapping into a function

Specifying the title here is a little tedious, and we'd have to make sure to change the title if we made a histogram for a different variable. Let's work on creating a function to remove this redundancy. We give the function a name like my_histogram. And then, we specify the data as df and the x variable as x_var. Now we replace the previous code with df for world_bank_data and x_var for infant_mortality_rate. To copy in x_var, we use the paste function, separated by a comma.

5. Working on our function

Our function is ready to be called. The function has two argument names, which are df and x_var. The error that is returned here tells us that tidy evaluation via defusing is needed. This function won't quite work but gets us on the right path. We'll tweak this a bit using rlang to get it working next.

6. Adding in rlang

When we call the my_histogram function, we want x_var to be unquoted like what we do with ggplot2 usually. But remember that to do so, we need to invoke rlang operators and functions as needed. First, since we are passing in arguments to defuse immediately, as we've seen previously with dplyr, we wrap x_var in the ggplot function code with a curly-curly. The next step applies to the x_var in the ggtitle function call. There is a handy as_label function that works with enquo-ed objects to convert arguments like x_var into a string similar to the as_name function. as_name is more directed at naming objects, whereas as_label works for strings like titles.

7. Using our function

Let's try out this function on perc_rural_pop to update the title automatically. Since the data is the first argument to our function, we can use the pipe with my_histogram directly. We can see in the outputted plot that this function meets our needs. We've now seen how to tie rlang tidy evaluation into our ggplot2 code!

8. Let's practice!

It's time to create a function to customize a scatterplot in a ggplot twist!