Arturo Sbr
Arturo Sbr

Reputation: 6333

Plotting partial equilibrium with ggplot2 (two functions in one figure)

I would like to create a graph using ggplot2 to visualize a partial equilibrium problem. I looked around for a solution to this but couldn't find one.

I want to plot two functions in the same figure, preferably using ggplot2. I want to graph an inverse demand function and a discrete marginal cost curve with Q on the x-axis and P on the y-axis.

  1. The Marginal Cost curve is as follows:

    • P = 0.794 for all Q in [0,70]
    • P = 0.956 for all Q in (70,140]
    • P = 2.802 for all Q in (140,infty)

Then,

  1. The inverse demand curve is described by the following function:
    • P = (199/Q)^(1/0.14)

I can plot the marginal cost curve, but I'm not familiar with plotting custom functions using ggplot2. I managed to plot the function separately unsing the *function = * command, but I couldn't fix the visible domain (xlim = c(0,300)) and I couldn't combine it with the marginal cost curve.

Thanks in advance.

EDIT

The code I have so far is the following:

        # Graphic representation
#T1 is the discrete MgC curve
    T1 <- as.data.table(c(0,75, 75,140, 140,300))
    T1$P <- c(0.793,0.793,
              0.956,0.956,
              2.802,2.802)
    setnames(T1, c("V1"),c("Q"))
#D0 is the inverse demand curve    
    D0 <- data.table(c(1,2,3,4,5))
    setnames(D0,c("V1"),c("P"))
    D0$Q <- ((D0$P)^(-0.14))*199.01

# Q1 and Q2 are quantities demanded when P=2.802 and 1.9 respectively     
    Q1 <- data.table(c(rep(199.01*(2.802)^-0.14,3)),c(0,2.5,5))
    Q2 <- data.table(c(rep(199.01*(1.9)^-0.14,3)),c(0,2.5,5))
    setnames(Q1,c("V1","V2"),c("Q","P"))
    setnames(Q2,c("V1","V2"),c("Q","P"))


    ggplot(mapping = aes(x = Q, y = P)) +
      geom_line(data = T1, color = "red", size = 1) + 
      geom_path(data = D0, color = "blue", size = 1) +
      geom_line(data = Q1, color = "green") +
      geom_line(data = Q2, color = "green")

Upvotes: 2

Views: 211

Answers (1)

zlipp
zlipp

Reputation: 801

You can use ggplot2::stat_function to plot arbitrary functions. I think there may have been a typo in your post - let me know if this looks right.

library(ggplot2)

df = data.frame(Q = 0:200,
                MC = c(rep(.794, 71),
                       rep(.956, 70),
                       rep(2.802, 60)))

ggplot(df, aes(x = Q)) + 
  geom_path(aes(y = MC)) +
  stat_function(fun = function(Q) {(Q/199)^-0.14})

enter image description here

Upvotes: 1

Related Questions