r - Plotting dose response curves with ggplot2 and drc -
in biology want plot dose response curves. r package 'drc' useful , base graphics can handle 'drm models'. however, add drm curves ggplot2.
my dataset:
library("drc") library("reshape2") library("ggplot2") demo=structure(list(x = c(0, 1e-08, 3e-08, 1e-07, 3e-07, 1e-06, 3e-06, 1e-05, 3e-05, 1e-04, 3e-04), y1 = c(0, 1, 12, 19, 28, 32, 35, 39, na, 39, na), y2 = c(0, 0, 10, 18, 30, 35, 41, 43, na, 43, na), y3 = c(0, 4, 15, 22, 28, 35, 38, 44, na, 44, na)), .names = c("x", "y1", "y2", "y3"), class = "data.frame", row.names = c(na, -11l ))
using base graphics:
plot(drm(data = reshape2::melt(demo,id.vars = "x"),value~x,fct=ll.4(),na.action = na.omit),type="bars")
produces nice 4-parameter dose response plot.
trying plot same plot in ggplot2, stumble upon 2 issues.
there no way of directly adding drm model curve. need rewrite 4-pl function , add in form of stat_function, cumbersome least.
ggplot(reshape2::melt(demo,id.vars = "x"),aes(x,value)) + geom_point() + stat_function(fun = function(x){ drm_y=function(x, drm){ coef(drm)[2]+((coef(drm)[3]-coef(drm)[2])/(1+exp((coef(drm)[1]*(log(x)-log(coef(drm)[4])))))) } + drm_y(x,drm = drm(data = reshape2::melt(demo,id.vars = "x"), value~x, fct=ll.4(), na.action = na.omit)) })
if wasn't enough works if scale_x continuous. if want add
scale_x_log10()
, get:warning message: in log(x): nans produced
.
i realise log10(0) = -inf
there ways of handling this. either (as case plot.drc) x=0 value plotted on x-axis 1/100 of pre-lowest x-value. (demo$x[which.min(demo$x)+1]/100
) or in graphpad prism, 0s omitted dose response curve entirely.
my questions are:
is there way of plotting drm models in ggplot2 directly?
how can link dataset corresponding 4-pl curvefit plotted in same colour?
a recent paper authors of drc
package included instructions extracting parameters use ggplot2. don't work within ggplot2 extract data model. solution applied data.
demo1 <- reshape2::melt(demo,id.vars = "x") # numbers ready use. demo.ll.4 <- drm(data = demo1,value~x,fct=ll.4(),na.action = na.omit) # run model.
the predict
function can extract parameters drm
models. isn't compatible multiple curves fit using curveid
.
# predictions , confidence intervals. demo.fits <- expand.grid(conc=exp(seq(log(1.00e-04), log(1.00e-09), length=100))) # new data 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 0 concentration avoid issues coord_trans.
demo1$xx <- demo1$x demo1$xx[demo1$xx == 0] <- 1.00e-09
then comes plotting curve, omitting geom_ribbon
stops errors 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 process can repeated. add ids each set.
demo.fits_1 <- data.frame(label = "curve1", demo.fits)
then use rbind
combine extracted parameters. there ggplot can handle colours.
Comments
Post a Comment