In biology we often want to plot dose response curves. The R package \'drc\' is really useful and base graphics can easily handle \'drm models\'. However, I would like to ad
A recent paper from the authors of the drc
package included instructions for extracting parameters for use by ggplot2. They don't work within ggplot2 but extract data from the model. This is their solution applied to your data.
demo1 <- reshape2::melt(demo,id.vars = "X") # get numbers ready for use.
demo.LL.4 <- drm(data = demo1,value~X,fct=LL.4(),na.action = na.omit) # run model.
The predict
function can extract the parameters from drm
models. It isn't compatible with multiple curves that were fit using curveid
.
# predictions and confidence intervals.
demo.fits <- expand.grid(conc=exp(seq(log(1.00e-04), log(1.00e-09), length=100)))
# new data with predictions
pm <- predict(demo.LL.4, newdata=demo.fits, interval="confidence")
demo.fits$p <- pm[,1]
demo.fits$pmin <- pm[,2]
demo.fits$pmax <- pm[,3]
They advise shifting the zero concentration to avoid issues with coord_trans.
demo1$XX <- demo1$X
demo1$XX[demo1$XX == 0] <- 1.00e-09
Then comes plotting the curve, omitting geom_ribbon
stops the errors from being drawn.
ggplot(demo1, aes(x = XX, y = value)) +
geom_point() +
geom_ribbon(data=demo.fits, aes(x=conc, y=p, ymin=pmin, ymax=pmax), alpha=0.2) +
geom_line(data=demo.fits, aes(x=conc, y=p)) +
coord_trans(x="log")
To graph multiple curves together the process can be repeated. Add IDs to each set.
demo.fits_1 <- data.frame(label = "curve1", demo.fits)
Then use rbind
to combine all the extracted parameters. From there ggplot can handle colours.