• 1 Setup
  • 2 Objective
  • 3 Problem
  • 4 Solution

1 Setup

# Install packages 
if (!require("pacman")) install.packages("pacman")
## Loading required package: pacman
pacman::p_load(tidyverse, # tidyverse pkgs including purrr
               tictoc, # performance test 
               glue) # glue strings and objects 

2 Objective

  • Learning how to use map() and glue() to automate creating multiple plots

3 Problem

  • Making the following data visualization process more efficient.
data("airquality")

airquality %>%
    ggplot(aes(x = Ozone, y = Solar.R)) +
    geom_point() +
    labs(title = "Relationship between Ozone and Solar.R",
         y = "Solar.R")
## Warning: Removed 42 rows containing missing values (geom_point).

airquality %>%
    ggplot(aes(x = Ozone, y = Wind)) +
    geom_point() +
    labs(title = "Relationship between Ozone and Wind",
         y = "Wind")
## Warning: Removed 37 rows containing missing values (geom_point).

airquality %>%
    ggplot(aes(x = Ozone, y = Temp)) +
    geom_point() +
    labs(title = "Relationship between Ozone and Temp",
         y = "Temp")
## Warning: Removed 37 rows containing missing values (geom_point).

4 Solution

  • Learn how glue() works.
  • glue() combines strings and objects and it works simpler and faster than paste() or sprintif().
names <- c("Jae", "Aniket", "Avery")

fields <- c("Political Science", "Law", "Public Health")

glue("{names} studies {fields}.")
## Jae studies Political Science.
## Aniket studies Law.
## Avery studies Public Health.
  • So, our next step is to combine glue() and map().

  • Let’s first think about writing a function that includes glue().

Challenge 1 How can you create the character vector of column names?

Challenge 2 How can make ggplot2() take strings as x and y variable names? (Hint: Type ?aes_string())

airquality %>%
    ggplot(aes_string(x = names(airquality)[1], y = names(airquality)[2])) +
    geom_point() +
    labs(title = glue("Relationship between Ozone and {names(airquality)[2]}"),
         y = glue("{names(airquality)[2]}"))
## Warning: Removed 42 rows containing missing values (geom_point).

  • The next step is to write an automatic plotting function.

    • Note that in the function i (abstract argument) replaced 2 (specific number).
create_point_plot <- function(i){
  
  airquality %>%
    ggplot(aes_string(x = names(airquality)[1], y = names(airquality)[i])) +
    geom_point() +
    labs(title = glue("Relationship between Ozone and {names(airquality)[i]}"),
         y = glue("{names(airquality)[i]}"))
  
  }
  • The final step is to put the function in map().
map(2:ncol(airquality), create_point_plot)
## [[1]]
## Warning: Removed 42 rows containing missing values (geom_point).

## 
## [[2]]
## Warning: Removed 37 rows containing missing values (geom_point).

## 
## [[3]]
## Warning: Removed 37 rows containing missing values (geom_point).

## 
## [[4]]
## Warning: Removed 37 rows containing missing values (geom_point).

## 
## [[5]]
## Warning: Removed 37 rows containing missing values (geom_point).