I am trying to recreate the attached plot in ggplot. Reading other posts on SO, I have been able to figure out everything (see code, with data) except for how to center the y-axis titles–“Mask” and “No Mask”–on top of the axes as pictured. (The code puts them to the left and right of the axes.) Is there a way to do this?
Plot to recreate
dates <- c("07/12/2020", "07/13/2020", "07/14/2020", "07/15/2020", "07/16/2020",
"07/17/2020", "07/18/2020", "07/19/2020", "07/20/2020", "07/21/2020",
"07/22/2020", "07/23/2020", "07/24/2020", "07/25/2020", "07/26/2020",
"07/27/2020", "07/28/2020", "07/29/2020", "07/30/2020", "07/31/2020",
"08/01/2020", "08/02/2020", "08/03/2020")
dates <- factor(dates)
mask = c(25.3181818181818, 19.6883116883116, 19.6298701298701, 20.4870129870129,
19.7467532467532, 19.7467532467532, 20.4675324675324, 19.8636363636363,
20.5357142857142, 21.4415584415584, 19.7857142857142, 19.7857142857142,
20.4675324675324, 19.0454545454545, 19.6493506493506, 17.0194805194805,
16.2207792207792, 16.4545454545454, 16.551948051948, 16.0064935064935,
16.1623376623376, 15.8311688311688, 15.9285714285714)
noMask = c(9.827922077922, 9.0974025974025, 9.3311688311688, 9.8181818181818,
9.9155844155844,9.4675324675324, 9.4675324675324, 8.9610389610389,
8.512987012987, 8.512987012987, 8.6883116883116, 8.4935064935064,
9.8571428571428, 9.9155844155844, 10.090909090909, 9.5064935064935,
9.5357142857142, 9.5746753246753, 9.9935064935064, 8.8636363636363,
9.0487012987013, 8.8636363636363, 9.1363636363636)
data <- data.frame(date = dates,
mask = mask,
noMask = noMask)
data %>%
ggplot(mapping = aes(x = date, group = 1)) +
geom_line(mapping = aes(y = mask, color = "mask"), size = 1.5) +
geom_line(mapping = aes(y = noMask + 11, color = "noMask"), size = 1.5) +
scale_colour_manual(values = c("#ffc000", "#5b9bd5")) +
scale_x_discrete(expand = c(0, 0)) +
scale_y_continuous(name = "Mask",
breaks = seq(15, 25, 2),
expand = c(0, 0),
limits = c(15, 26),
sec.axis = sec_axis(~. - 11,
name = "No Mask",
breaks = seq(4, 14, 2))) +
theme_minimal() +
expand_limits(x = c(0.5, 23.5)) +
geom_vline(xintercept=seq(0.5, 23.5, 1), colour='grey85') +
theme(legend.position = "None",
plot.margin = unit(c(1, 0, 0, 0), "cm"),
panel.grid.major.x = element_blank(),
panel.grid.minor.y = element_blank(),
axis.title.x = element_blank(),
axis.text.x = element_text(angle = 45,
hjust = 1,
vjust = 1,
size = 8),
axis.title.y.left = element_text(angle = 0,
vjust = 1,
hjust = 0,
color = "#ffc000"),
axis.title.y.right = element_text(angle = 0,
vjust = 1,
hjust = 1,
color = "#5b9bd5"))