Skip to content

Instantly share code, notes, and snippets.

@andrewheiss
Created May 23, 2020 14:41
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save andrewheiss/f43c6022841382d1825e394c1c01a268 to your computer and use it in GitHub Desktop.
Save andrewheiss/f43c6022841382d1825e394c1c01a268 to your computer and use it in GitHub Desktop.
library(tidyverse)
library(patchwork)
tornado <- tibble(x = 1:10,
y = c(2, 4, 5, 5.5, 4, 6, 7.5, 8, 7.5, 9)) %>%
mutate(dy = y - lag(y))
plot1 <- ggplot(tornado, aes(x = x, y = y)) +
geom_path(size = 2, linejoin = "round", lineend = "round") +
geom_point(size = 3) +
coord_cartesian(xlim = c(0, 10), ylim = c(0, 10)) +
labs(x = "X", y = "y") +
theme_bw()
plot2 <- ggplot(drop_na(tornado, dy), aes(x = dy, y = y)) +
geom_vline(xintercept = 0, size = 1, linetype = "dotted") +
geom_path(size = 2, linejoin = "round", lineend = "round") +
geom_point(size = 3) +
coord_cartesian(xlim = c(-2, 2), ylim = c(0, 10)) +
annotate(geom = "text", x = -Inf, y = 0,
label = "← y value decreasing",
hjust = -0.1, fontface = "bold") +
annotate(geom = "text", x = Inf, y = 0,
label = "y value increasing →",
hjust = 1.1, fontface = "bold") +
labs(x = "Change in y", y = "y") +
theme_bw()
plot1 + plot2
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment