r - ggplot2: How to shade an area above a function curve and below a line? -


so have dataframe this:

a_data <-   data.frame(     f = f,     alpha = alpha,     asymptote = alpha_1_est) 

and function this:

a_formula <- function(x) { 0.7208959 - 0.8049132 * exp(-21.0274 * x)} 

i use them ggplot2:

ggplot(a_data, aes(x = f, y = alpha)) +   geom_point() +  #function curve stat_function(fun = a_formula,               color = "red") +  #asymptote of alpha geom_hline(   yintercept = asymptote,   linetype = "longdash",   color = "blue") 

which yields plot this: basic plot

what want , can't find way shade area between y axis, function curve (red) , asymptote line (dashed), this: shaded plot

i have tried squeeze ribbon or polygon in there, doesn't work correctly - maybe it's because want shade above curve, not below (below works fine).

this how dataframe looks like:

> head(a_data)      f       alpha asymptote 1 0.01 0.007246302 0.7208959 2 0.03 0.374720198 0.7208959 3 0.05 0.484362949 0.7208959 4 0.07 0.540090209 0.7208959 5 0.09 0.625383303 0.7208959 6 0.11 0.590898201 0.7208959 

p.s. new stackoverflowing, if broke convention or otherwise messed question, don't hesitate point out.

the example below shows how geom_ribbon can conveniently used coloring area between horizontal line , curve.

df1 <- structure(list(x = c(0.01, 0.03, 0.05, 0.07, 0.09, 0.11), y = c(0.007246302,  0.374720198, 0.484362949, 0.540090209, 0.625383303, 0.590898201 ), asymptote = c(0.7208959, 0.7208959, 0.7208959, 0.7208959,  0.7208959, 0.7208959)), .names = c("x", "y", "asymptote"), class = "data.frame", row.names = c("1",  "2", "3", "4", "5", "6"))  a_formula <- function(x) { 0.7208959 - 0.8049132*exp(-21.0274*x) }  xs <- seq(min(df1$x),max(df1$x),length.out=100) ysmax <- rep(0.7208959, length(xs)) ysmin <- a_formula(xs) df2 <- data.frame(xs, ysmin, ysmax)  library(ggplot2) ggplot(data=df1) + geom_point(aes(x=x, y=y)) + geom_line(aes(x=x, y=asymptote), lty=2, col="blue", lwd=1) + stat_function(fun = a_formula, color="red", lwd=1) + geom_ribbon(aes(x=xs, ymin=ysmin, ymax=ysmax), data=df2, fill="#bb000033") 

enter image description here


Comments

Popular posts from this blog

html - How to set bootstrap input responsive width? -

javascript - Highchart x and y axes data from json -

javascript - Get js console.log as python variable in QWebView pyqt -