Skip to content

Instantly share code, notes, and snippets.

@mhpob
Last active March 17, 2022 20:35
Show Gist options
  • Star 2 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save mhpob/a89253dffd78e239a01bf29dd807f462 to your computer and use it in GitHub Desktop.
Save mhpob/a89253dffd78e239a01bf29dd807f462 to your computer and use it in GitHub Desktop.
"Cave plot" proof of concept
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