Search code examples
rauc

Area under curve


curve(-12 * log(x) - (415 / x), 25, 50)
abline(h = -55, lty = 2)

I would like to plot the area between the curve and solid line and shade it, but couldn't get to do it. I tried using the trapz function from pracma package. Any suggestions would be appreciated?


Solution

  • Then you can use polygon() where you basically define the x- and y-coordinates of enough points to make it look smooth. I'll go with 120:

    ff <- function(x) -12 * log(x) - (415 / x)  ## define curve
    
    # get a vector or potential x values for the polygon
    x1 <- seq(from=25, to=50, length.out=120)
    x1 <- x1[ff(x1) >= -55]  ## filter only relevant section
    
    x2 <- rev(x1)    ## reverse the vector for the return trip
    xx <- c(x1, x2)  ## concatenate to get all x coordinates
    yy <- c(rep(-55, length(x1)), ff(x2))
    
    curve(ff, 25, 50)
    abline(h = -55, lty = 2)
    # join the dots and fill the space orange
    polygon(xx, yy, col='orange')
    

    EDIT: Added code to the whole procedure to reflect comment from @epsilone