Load libraries.
library(here) ## good for managing paths
library(tidyverse) ## functions for plotting, data manipulation
library(ggbeeswarm) ## function for plotting raw data with quasirandom jitter
library(grid) ## functions for arranging plots
library(gridExtra) ## functions for arranging plots
library(ggpubr) ## functions for plotting SD, SEM, CI.Load the serzinc dataset (must be saved locally as “datasets/exercise/serzinc.txt”).
## load the serzinc dataset (has to be saved locally)
zinc <- read_delim(here("datasets/exercise/serzinc.txt"),
delim = "\t", trim_ws = T, col_names = "level", skip = 1)Set plot styles (so that multiple plots can be shown).
set_theme_left = theme_bw() +
theme(plot.margin = margin(
t = .5, b = .5,
l = 0, r = 0, unit = "cm"),
panel.grid.major.x = element_blank(),
panel.grid.minor.x = element_blank(),
aspect.ratio = 6/1,
axis.title.x = element_blank(),
axis.ticks.x = element_blank(),
axis.text.x = element_blank())
## all plots expect the leftmost one won't need y labels.
set_theme_right = theme_bw() +
theme(plot.margin = margin(
t = .5, b = .5,
l = 0, r = 0, unit = "cm"),
panel.grid.major.x = element_blank(),
panel.grid.minor.x = element_blank(),
aspect.ratio = 6/1,
axis.title.x = element_blank(),
axis.text.x = element_blank(),
axis.ticks.x = element_blank(),
axis.ticks.y = element_blank(),
axis.text.y = element_blank())Make plots individually, and put them together with grid.arrange().
p <- list(0, 1, 2, 3)
## plot raw data
point_size <- 3
p[[1]] <- ggplot(zinc, aes(y = level)) +
geom_quasirandom(aes(x = 0, y = level), alpha = .9, size = point_size/4) +
coord_cartesian(ylim = c(40, 160)) +
labs(title = "Raw data",
x = element_blank(), y = "Serum Zinc Level (µg/dl)") +
set_theme_left
## plot boxplot(median, 50%, 95%iles)
p[[2]] <- ggplot(zinc, aes(y = level)) +
geom_boxplot(alpha = .3, varwidth = T, color = "black",
outlier.size = point_size/4) +
coord_cartesian(ylim = c(40, 160)) +
labs(title = "Box plot",
x = element_blank(), y = "") +
set_theme_right
## plot SD
p[[3]] <- ggplot(zinc, aes(x = 0, y = level)) +
stat_summary(fun.data = mean_sd, geom = "errorbar", position = "dodge",
linetype = 1, color = "gray5", width = point_size/10,
size = point_size/5, show.legend = F) +
coord_cartesian(ylim = c(40, 160)) +
labs(title = "SD",
x = element_blank(), y = "") +
set_theme_right
## plot SEM
p[[4]] <- ggplot(zinc, aes(x = 0, y = level)) +
stat_summary(fun.data = mean_se, geom = "errorbar", position = "dodge",
linetype = 1, color = "gray5", width = point_size/10,
size = point_size/5, show.legend = F) +
coord_cartesian(ylim = c(40, 160)) +
labs(title = "SEM",
x = element_blank(), y = "") +
set_theme_right
## plot 99% CI
p[[5]] <- ggplot(zinc, aes(x = 0, y = level)) +
stat_summary(fun.data = mean_cl_normal,
fun.args = list(conf.int = 0.99),
geom = "errorbar", position = "dodge",
linetype = 1, color = "gray5", width = point_size/10,
size = point_size/5, show.legend = F) +
coord_cartesian(ylim = c(40, 160)) +
labs(title = "99% CI",
x = element_blank(), y = "") +
set_theme_right
## plot 90% CI
p[[6]] <- ggplot(zinc, aes(x = 0, y = level)) +
stat_summary(fun.data = mean_cl_normal,
fun.args = list(conf.int = 0.90),
geom = "errorbar", position = "dodge",
linetype = 1, color = "gray5", width = point_size/10,
size = point_size/5, show.legend = F) +
coord_cartesian(ylim = c(40, 160)) +
labs(title = "90% CI",
x = element_blank(), y = "") +
set_theme_right
pl <- list(p[[1]], p[[2]], p[[3]], p[[4]], p[[5]], p[[6]])
g <- grid.arrange(grobs = pl,
ncol = 6, nrow = 1,
respect = F
)Save figure.