Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

I'd like to paint an area but i don't know how to

Tags:

plot

r

ggplot2

my plot

I mean, I'd want to paint only the square area P1 X (Q1-Q2).

Not the trapezoid (P2+P1) X (Q1-Q2/2).

Here's code that I used. I used ggplot and dplyr. How can I solve this problem?

How can I paint the only square area not the trapezoied area!!!!

library(ggplot2)
library(dplyr)
supply <- Hmisc::bezier(x = c(1, 8, 9),
                        y = c(1, 5, 9)) %>%
  as_data_frame()

demand <- Hmisc::bezier(c(1, 3, 9),
                        c(9, 3, 1)) %>%
  as_data_frame()


fun_supply <- approxfun(supply$x, supply$y, rule = 2)

fun_supply(c(2, 6, 8))
fun_demand <- approxfun(demand$x, demand$y, rule = 2)

intersection_funs <-  uniroot(function(x) fun_supply(x) - fun_demand(x), c(1, 9))
intersection_funs


y_root <- fun_demand(intersection_funs$root)


curve_intersect <- function(curve1, curve2) {
  # Approximate the functional form of both curves
  curve1_f <- approxfun(curve1$x, curve1$y, rule = 2)
  curve2_f <- approxfun(curve2$x, curve2$y, rule = 2)

  # Calculate the intersection of curve 1 and curve 2 along the x-axis
  point_x <- uniroot(function(x) curve1_f(x) - curve2_f(x), 
                     c(min(curve1$x), max(curve1$x)))$root

  # Find where point_x is in curve 2
  point_y <- curve2_f(point_x)

  # Finish 
  return(list(x = point_x, y = point_y))
}

intersection_xy <- curve_intersect(supply, demand)
intersection_xy
intersection_xy_df <- intersection_xy %>% as_data_frame()


demand2 <- Hmisc::bezier(c(1.5, 3.5, 9.5),
                         c(9.5, 3.5, 1.5)) %>%
  as_data_frame()

supply2 <- Hmisc::bezier(c(1,7,8),
                         c(3,7,11)) %>%
  as_data_frame()
#Make a data frame of the intersections of the supply curve and both demand curves

intersections <- bind_rows(curve_intersect(supply, demand),
                           curve_intersect(supply2, demand2))

plot_labels <- data_frame(label = c("S", "D","S[1]","D[1]"),
                          x = c(9, 1, 6.5, 3),
                          y = c(8, 8, 8, 8))



ggplot(mapping = aes(x = x, y = y)) + 
  geom_path(data = supply, color = "#0073D9", size = 1, linetype = "dashed") + 
  geom_path(data = demand, color = "#FF4036", size = 1, linetype = "dashed") + 
  geom_path(data = demand2, color = "#FF4036", size = 1) + 
  geom_path(data = supply2, color = "#0073D9", size = 1) +
  geom_segment(data = intersections, 
               aes(x = x, y = 0, xend = x, yend = y), lty = "dotted") +
  geom_segment(data = intersections, 
               aes(x = 0, y = y, xend = x, yend = y), lty = "dotted") + 
  geom_segment(data = intersections,
               aes(x = x, y = y, xend = x, yend=  y), lty = "dotted") +
  geom_point(data = intersections, size = 3) +
  geom_text(data = plot_labels,
            aes(x = x, y = y, label = label), parse = TRUE) +
  scale_x_continuous(expand = c(0, 0), breaks = intersections$x,
                     labels = expression(Q[1], Q[2])) +
  scale_y_continuous(expand = c(0, 0), breaks = intersections$y,
                     labels = expression(P[1], P[2]))+
  labs(x = "Quantity", y = "Price") +
  geom_area(data =intersections, fill="#9999FF", alpha=0.5) +
  theme_classic() + 
  coord_equal()

Could you help me to paint the area that I mentioned.

like image 525
hyejuryu Avatar asked Mar 05 '23 14:03

hyejuryu


1 Answers

You might try adding geom_rect(data=intersections[1,], aes(xmin=0, xmax=x, ymin=0, ymax=y),fill='green', alpha=0.5) to your plot call.

So we have:

ggplot(mapping = aes(x = x, y = y)) + 
  geom_path(data = supply, color = "#0073D9", size = 1, linetype = "dashed") + 
  geom_path(data = demand, color = "#FF4036", size = 1, linetype = "dashed") + 
  geom_path(data = demand2, color = "#FF4036", size = 1) + 
  geom_path(data = supply2, color = "#0073D9", size = 1) +
  geom_segment(data = intersections, 
               aes(x = x, y = 0, xend = x, yend = y), lty = "dotted") +
  geom_segment(data = intersections, 
               aes(x = 0, y = y, xend = x, yend = y), lty = "dotted") + 
  geom_segment(data = intersections,
               aes(x = x, y = y, xend = x, yend=  y), lty = "dotted") +
  geom_point(data = intersections, size = 3) +
  geom_text(data = plot_labels,
            aes(x = x, y = y, label = label), parse = TRUE) +
  scale_x_continuous(expand = c(0, 0), breaks = intersections$x,
                     labels = expression(Q[1], Q[2])) +
  scale_y_continuous(expand = c(0, 0), breaks = intersections$y,
                     labels = expression(P[1], P[2]))+
  labs(x = "Quantity", y = "Price") +
  geom_area(data =intersections, fill="#9999FF", alpha=0.5) +
  theme_classic() + 
  coord_equal()+
  geom_rect(data=intersections[1,], aes(xmin=0, xmax=x, ymin=0, ymax=y),fill='green', alpha=0.5)

enter image description here

Edit based on comment:

geom_rect(data=intersections, aes(xmin=x[2], xmax=x[1], ymin=0, ymax=y[1]),fill='green', alpha=0.5)

enter image description here

like image 86
J.Con Avatar answered Apr 02 '23 11:04

J.Con