Hi,
I'm trying to replicate the economics graph below using R:
I found a suitable package for this 'reconPlots'
But I got stuck at this stage:
library(tidyverse)
library(reconPlots)
curve1 <- data.frame(Hmisc::bezier(c(4, 4, 4), c(1, 5, 10)))
curve2 <- data.frame(Hmisc::bezier(c(1, 3, 7), c(10, 5, 2)))
curve_intersection <- curve_intersect(curve1, curve2)
#> Warning in regularize.values(x, y, ties, missing(ties), na.rm = na.rm):
#> collapsing to unique 'x' values
curve_intersection
#> $x
#> [1] 4
#>
#> $y
#> [1] 4.864169
# Calculate consumer surplus area
consumer_surplus <- curve_intersection$x - curve2$y
consumer_surplus_area <- sum(consumer_surplus) * (curve_intersection$x - curve1$x)
ggplot() +
geom_line(data = curve1, aes(x = x, y = y, color = "nabídka"), linewidth = 1) +
geom_line(data = curve2, aes(x = x, y = y, color = "poptávka"), linewidth = 1) +
geom_hline(yintercept = curve_intersection$y, linetype = "dotted") +
geom_ribbon(data = curve2,
aes(x = x,
ymin = curve_intersection$y, ymax = curve2$y,
fill = "Consumer Surplus"),
alpha = 0.2,
show.legend = F) +
theme_minimal() +
theme(
legend.position = 'bottom',
legend.box.margin = margin(0, -200, -1, -200),
legend.title = element_blank(),
text = element_text(size = 16),
legend.text = element_text(size = 14),
axis.title.x = element_text(size = 14, margin = margin(15, 0, 0, 0)),
axis.title.y = element_text(size = 14, margin = margin(0, 15, 0, 0)),
panel.grid.major = element_blank(),
panel.grid.minor = element_blank(),
axis.text = element_blank()
) +
labs(x = "množství", y = "cena") +
scale_color_manual(values = rev(c("#00254B", "#ECB925")),
labels = rev(c("nabídka", "poptávka")),
breaks = c("poptávka", "nabídka")) +
scale_fill_manual(values = c("Consumer Surplus" = "#F2CE6E"))
#> Warning: Use of `curve2$y` is discouraged.
#> ℹ Use `y` instead.
Do you have any suggestion how to proceed?
Many thanks for your time!