Created
March 2, 2018 00:55
-
-
Save jthomasmock/16d5f80dc209339872b0b0fdd63846ed to your computer and use it in GitHub Desktop.
Part of the purrr resolution by Isabella R. Ghement, Ph.D.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#======================================================================= | |
# Install required packages | |
#======================================================================= | |
install.packages("ggridges") | |
#======================================================================= | |
# Load required packages | |
#======================================================================= | |
library(ggplot2) | |
library(dplyr) | |
library(ggridges) | |
library(ggthemes) | |
library(Hmisc) | |
library(purrr) | |
#======================================================================= | |
# Load airquality data set which comes with R | |
#======================================================================= | |
data(airquality) | |
head(airquality) | |
?airquality | |
str(airquality) | |
#======================================================================= | |
# Prepare airquality data set for further analysis | |
#======================================================================= | |
# convert Month variable from integer to factor | |
airquality$Month <- factor(airquality$Month, | |
levels=5:9, | |
labels=month.name[5:9]) | |
# assign a label and units to Ozone variable | |
label(airquality$Ozone) <- "Ozone" | |
units(airquality$Ozone) <- "ppb" | |
# assign a label and units to Wind variable | |
label(airquality$Wind) <- "Wind" | |
units(airquality$Wind) <- "mph" | |
# assign a label and units to Temp variable | |
label(airquality$Temp) <- "Temperature" | |
units(airquality$Temp) <- "degrees F" | |
# assign a label to Month variable | |
label(airquality$Month) <- "Month" | |
#======================================================================= | |
# Define a custom function which will be used to produce joyplots | |
# (e.g., joyplots of Ozone for the months of May, June, July, August and | |
# September). Joyplots (or density ridgeline plots) are partially overlapping | |
# density plots that create the impression of a mountain range when considered together. | |
# See https://cran.r-project.org/web/packages/ggridges/vignettes/introduction.html | |
# for more details on this type of plots. | |
#======================================================================= | |
joyplots <- function(data, x, y) { | |
.e = environment() | |
data <- dplyr::select_(data, .dots=c(x,y) %>% unname) | |
head(data) | |
names(data) <- c("x","y") | |
data.nonmiss <- data[complete.cases(data), ] | |
ggplot(data=data.nonmiss, aes(x=x,y=y, fill=y, height = ..density..), environment=.e) + | |
geom_density_ridges(rel_min_height = 0.005, stat = "density") + | |
scale_y_discrete(expand = c(0.01, 0)) + | |
scale_x_continuous(expand = c(0.01, 0)) + | |
scale_fill_few() + | |
xlab(paste0(label(data.nonmiss$x), " (",units(data.nonmiss$x),")")) + | |
ylab(paste0(label(data.nonmiss$y))) + | |
theme_minimal() + | |
theme(legend.position="none") | |
} | |
#======================================================================= | |
# Test the joyplots() function for the Ozone variable | |
#======================================================================= | |
joyplots(airquality, x="Ozone", y="Month") | |
#======================================================================= | |
# Apply the joyplot() function simultaneously to the variables | |
# Ozone, Wind and Temp from the airquality data set using the | |
# map2() function from the purrr package. Assign relevant names to | |
# the resulting plots, which will be stored into a list called plots. | |
#======================================================================= | |
xvars <- c("Ozone", "Wind", "Temp") # specify all variables for which we need to build joyplots | |
yvars <- c("Month") # specify the Month variable | |
plots <- map2(xvars, yvars, joyplots, data=airquality) # apply joyplots simultaneously to every combination of elements of xvars and yvars | |
names(plots) <- xvars # name the plots | |
plots | |
#======================================================================= | |
# Use the pwalk() function from the purrr package to export each of the | |
# plots from the list of plots to a pdf file. The names of all pdf files | |
# are specified in the filenames object. The files are saved with | |
# ggsave() in the current working directory. | |
#======================================================================= | |
filenames <- stringr::str_c(names(plots), ".pdf") # create the filenames under which the plots will be saved | |
filenames | |
pwalk(list(filenames, plots), ggsave, path = getwd()) # save the plots under these filenames with pwalk() | |
getwd() # pdf files are saved here |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment