# reconPlots (economics graphs)

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.
``````

Do you have any suggestion how to proceed?

Try

``````na.rm = TRUE
``````

Assuming this is clear?

Plot along these lines.

``````library(ggplot2)

d <- data.frame(
Quantity = seq(0, 100, by = 1),
Price = 100 * exp(-0.05 * seq(0, 100, by = 1)) + 10)
chopt <- d[1:24,]
ggplot(d, aes(x = Quantity, y = Price)) +
geom_line(color = "red") +
geom_vline(xintercept = 25,color = "blue") +
geom_hline(yintercept = 40, linetype = "dashed") +
geom_hline(yintercept = 30, linetype = "dashed") +
geom_point(aes(25,40), color = "red", shape = 1, size = 2) +
geom_area(chopt, mapping = aes(Quantity,Price), fill = "lightpink") +
geom_rect(aes(xmin = 0, xmax = 25, ymin = 0, ymax = 30), fill = "lightsteelblue") +
geom_rect(aes(xmin = 0, xmax = 25, ymin = 30, ymax = 40), fill = "lightgray") +
geom_text(aes(x = 40, y = 45, label = "Market price")) +
geom_text(aes(x = 12.5, y = 35, label = "Tax revenue")) +
geom_text(aes(x = 70, y = 35, label = "Tax rate")) +
geom_text(aes(x = 12.5, y = 50, label = "Consumer\nsurplus")) +
geom_text(aes(x = 12.5, y = 12.5, label = "Producer\nsurplus")) +
geom_errorbar(aes(x = 60, ymin = 30, ymax = 40), width = 0.2) +
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"))
``````

Created on 2023-09-20 with reprex v2.0.2

2 Likes

This topic was automatically closed 7 days after the last reply. New replies are no longer allowed.

If you have a query related to it or one of the replies, start a new topic and refer back with a link.