Last active
March 17, 2022 20:35
-
-
Save mhpob/a89253dffd78e239a01bf29dd807f462 to your computer and use it in GitHub Desktop.
"Cave plot" proof of concept
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
library(dplyr) | |
df_north <- tibble( | |
week = 1:53, | |
N = sample(0:15, 53, replace = T) | |
) | |
df_south <- tibble( | |
week = 1:53, | |
N = sample(0:15, 53, replace = T) | |
) | |
library(ggplot2) | |
# Method 1: one plot | |
ggplot() + | |
geom_col(data = df_north, aes(x = week, y = -N), | |
# Need to nudge by at least twice the maximum value | |
position = position_nudge(y = 30)) + | |
geom_col(data = df_south, aes(x = week, y = N)) + | |
scale_x_continuous(breaks = round(seq(1, 53, length.out = 12)), | |
labels = month.abb) + | |
# Correct the y axis labels | |
scale_y_continuous(breaks = seq(0, 30, 5), labels = c(0, 5, 10, 15, 10, 5, 0)) + | |
labs(title = 'Method 1') | |
# Method 2: two plots combined, correct y axis | |
library(patchwork) | |
north <- ggplot() + | |
geom_col(data = df_north, aes(x = week, y = N)) + | |
scale_y_reverse() + | |
scale_x_continuous(breaks = round(seq(1, 53, length.out = 12))) + | |
theme(axis.text.x = element_blank(), | |
axis.title.x = element_blank(), | |
plot.margin = margin(0,0,0,0)) | |
south <- ggplot() + | |
geom_col(data = df_south, aes(x = week, y = N)) + | |
scale_x_continuous(breaks = round(seq(1, 53, length.out = 12)), | |
labels = month.abb) + | |
theme(plot.margin = margin(0,0,0,0)) | |
north / south + plot_annotation(title = 'Method 2') |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment