Created
April 29, 2020 21:46
-
-
Save vincentarelbundock/4e06ed1b0deb4259ad1a0b70379f91e3 to your computer and use it in GitHub Desktop.
example
This file contains hidden or 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
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