rcurve

r - Plot intersecting curves


I want to create a simple plot displaying two intersecting curves like the one below:

enter image description here

(with an arrow indicating the intersection point)

I have been able to draw the intersecting curves with the following code:

I0 <- log(1)
b <- .1
d <- .014

curve(exp(I0 - b * x), 0, 50, col = "blue", lwd = 2)
curve(exp(d * x) - 1, 0, 50, add = TRUE, col = "red", lwd = 2)

But what I got is not a pair of "symmetrical" curves:

enter image description here

Any hints? (For this example I did not try to plot the arrow).


Solution

  • To find the point of intersection (your "d"), you can use uniroot.

    f <- function(x) exp(I0 - b * x)
    g <- function(x) exp(I0 - b * (30-x))
        
    I0 <- log(1)
    b <- 0.1
    
    x <- uniroot(function(x) f(x) - g(x), interval=c(0,30))$root
    y <- g(x)
    
    curve(exp(I0 - b * x), 0, 30, col="blue", lwd=2)
    curve(exp(I0 - b * (30-x)), 0, 30, col="red", lwd=2, add=TRUE)
    
    y.axs <- par("usr")[3:4]
    y0 <- y + diff(y.axs) / 10
    
    arrows(x0=x, y0=y0, x1=x, y1=y, length = 0.15)
    text(x=x, y=y0, expression(hat(S)), pos=3)
    

    enter image description here