Skip to content

Instantly share code, notes, and snippets.

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 vincentarelbundock/4e06ed1b0deb4259ad1a0b70379f91e3 to your computer and use it in GitHub Desktop.
Save vincentarelbundock/4e06ed1b0deb4259ad1a0b70379f91e3 to your computer and use it in GitHub Desktop.
example
draw_plot <- function(funs = list('S' = function(x) x, 'D' = function(x) 100 - x),
xlim = c(0, 100)) {
# data.frame of 1000 points to trace smooth functions
dat <- tibble(x = seq(xlim[1], xlim[2], length.out = 1000),
y1 = funs[[1]](x),
y2 = funs[[2]](x))
# find equilibrium and add it to data.frame
equilibrium <- curve_intersect(funs[[1]], funs[[2]], domain = xlim)
tmp <- tibble(x_eq = equilibrium[[1]],
y_eq = equilibrium[[2]],
equilibrium = 'Equilibrium')
dat <- bind_rows(dat, tmp)
# TODO: smart segment detection
zero_y <- min(c(dat$y1, dat$y2), na.rm = TRUE)
zero_x <- min(dat$x, na.rm = TRUE)
# plot
p <- ggplot(dat, aes(x, y1, label = equilibrium)) +
geom_line() +
geom_line(aes(x, y2)) +
geom_point(aes(x_eq, y_eq), size = 2) +
geom_segment(aes(x = 50, y = 0, xend = 50, yend = 50), linetype = 'dashed') +
geom_segment(aes(x = 0, y = 50, xend = 50, yend = 50), linetype = 'dashed') +
theme_minimal() +
scale_x_continuous(expand = c(0, 0)) +
scale_y_continuous(expand = c(0, 0)) +
theme(panel.grid = element_blank(),
axis.line = element_line(size = 1),
axis.text = element_blank(),
axis.title.x = element_text(hjust = 1),
axis.title.y = element_text(hjust = 1, angle = 0)) +
labs(x = 'Q', y = 'P')
p
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment