Calculation of dose versus ggplot2 and drc curves

In biology, we often want to display dose-response curves. The drc R package is really useful, and basic graphics can easily handle drm models. However, I would like to add my drm curves to 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 basic graphics:

plot(drm(data = reshape2::melt(demo,id.vars = "X"),value~X,fct=LL.4(),na.action = na.omit),type="bars")

creates a good 4-parameter dose response graph.

Trying to build the same plot in ggplot2, I came across 2 questions.

  • It is not possible to directly add a drm model curve. I need to rewrite 4-PL as a function and add it as a stat_function, which is at least cumbersome.

    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 this is not enough, it only works if scale_x is continuous. If I want to add scale_x_log10(), I get: Warning message: In log(x): NaNs produced.

, log10(0) = -Inf, . ( plot.drc) x = 0 x, , 1/100 x. (demo$X[which.min(demo$X)+1]/100) GraphPad Prism, 0s .

:

  • drm ggplot2?

  • 4-PL , ?

+3
2

A drc ggplot2. ggplot2, . .

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.

predict drm. , 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]

, .

demo1$XX <- demo1$X
demo1$XX[demo1$XX == 0] <- 1.00e-09

, geom_ribbon, .

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") 

enter image description here

, . .

demo.fits_1 <- data.frame(label = "curve1", demo.fits)

rbind, . ggplot .

+10

, , , .

, ggplot2 drc geom_ stat_smooth (method=drm, fct=LL.4(),se=FALSE) geom_ stat_smooth (method=drm, fct=L.4(),se=FALSE), scale_x_log10().

log10, :

demo <- demo %>% 
      mutate(X = 
       ifelse(X == 0, 
              yes = (sort(demo$X[which.min(sort(demo$X)) + 1]/100)),
              no = X
              )
            )         #looks for the pre-lowest value in X and divides it by 100

X = 0 X = 1/100th X- ( 1e-10). 0, , , , Prism. , , , ggplot , , log10 (0).

, stat_smooth drm method = drm, , SE. , se = FALSE , , - .

, , fct = LL.4() fct = L.4() log10, , . , , , ggplot log10, logit-4P (.. L.4()) log-logit-4P (LL 0,4()).

geom_smooth() stat_smooth(), , , , , .

:

demo <- demo %>% 
      mutate(X = 
       ifelse(X == 0, 
              yes = (sort(demo$X[which.min(sort(demo$X)) + 1]/100)),
              no = X
              )
            )
demo.long <- reshape2::melt(demo,id.vars = "X") #reshapes the demo dataset to long format
ggplot(data = demo.long,
       aes(x = X, y = value, col = variable)
      ) + 
   geom_point() + 
   geom_smooth(method = drm, fct = L.4(), se = FALSE) +
   scale_x_log10() #plots out the dataset with the corresponding 4-parameter log-logit dose response curves
+1

All Articles