# 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
map()
and glue()
to automate creating multiple plotsdata("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).
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.
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]}"))
}
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).