econocharts
econocharts copied to clipboard
Budget constraint graph
Hello, your initiative is excellent.
It would be possible to add that a basic graph could be added that joins the budget constraint with the indifference curve.
Thanks.
I also thought about this. Right now the function I developed (not published yet) allows creating budget constraint as in the image below, but not the corresponding indifference curves. The problem is that I can't add the indifference curve from a line in an automatic manner, but add a tanget to a indifference curve, so I'm not sure if this functionality should be added within the indifference function.
budget(rect1, rect2, x = c(3, 5), labels = c("A", "B"))

What I've tried so far is the following:
# Data
curve2 <- data.frame(Hmisc::bezier(c(1, 3, 9),
c(9, 3, 1)))
x <- curve2$x
y <- curve2$y
plot(x, y)
spl <- smooth.spline(y ~ x)
lines(spl, col = 2)
newx <- 4
pred0 <- predict(spl, x = newx, deriv = 0)
pred1 <- predict(spl, x = newx, deriv = 1)
yint <- pred0$y - (pred1$y * newx)
xint <- -yint / pred1$y
ggplot(curve2, aes(x = x, y = y)) +
geom_line(data = data.frame(x = x, y = yint + pred1$y * x), color = "red", lwd = 2) +
geom_line(lwd = 2) +
geom_point(data = data.frame(x = pred0$x, y = pred0$y), size = 3, color = "green")

The previous code can be used to add the tangent of a curve. What do you think? If you have any other suggestions or ideas please let me know.