使用 ggplot2 和 drc 绘制剂量反应曲线 [英] Plotting dose response curves with ggplot2 and drc

查看:33
本文介绍了使用 ggplot2 和 drc 绘制剂量反应曲线的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

在生物学中,我们经常想绘制剂量反应曲线.R 包drc"非常有用,基本图形可以轻松处理drm 模型".但是,我想将我的 drm 曲线添加到 ggplot2.

我的数据集:

 library("drc")图书馆(重塑2")图书馆(ggplot2")演示=结构(列表(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))

使用基本图形:

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

产生一个很好的 4 参数剂量反应图.

试图在 ggplot2 中绘制相同的图,我偶然发现了 2 个问题.

  1. 无法直接添加drm模型曲线.我需要将4-PL重写为一个函数,并以stat_function的形式添加,这至少可以说很麻烦.

    ggplot(reshape2::melt(demo,id.vars = "X"),aes(X,value)) +geom_point() +stat_function(乐趣=函数(x){drm_y=函数(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))})

  2. 如果这还不够,它只有在 scale_x 是连续的情况下才有效.如果我想添加 scale_x_log10(),我得到:警告信息:在 log(x) 中:产生的 NaN .

我意识到 log10(0) = -Inf 但有一些处理方法.要么(与 plot.drc 的情况一样)x=0 值绘制在 x 轴上,基本上是最低 x 值的 1/100.(demo$X[which.min(demo$X)+1]/100) 或在 GraphPad Prism 中,0 完全从剂量反应曲线中省略.

我的问题是:

  1. 有没有办法直接在ggplot2中绘制drm模型?

  2. 如何将数据集与其相应的 4-PL 曲线拟合关联起来,以便它们以相同的颜色绘制?

解决方案

A

要一起绘制多条曲线,可以重复该过程.为每个集合添加 ID.

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

然后使用rbind将所有提取的参数组合起来.从那里 ggplot 可以处理颜色.

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 add my drm curves to a 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 a nice 4-parameter dose response plot.

Trying to plot the same plot in ggplot2, I stumble upon 2 issues.

  1. There is no way of directly adding the drm model curve. I need to rewrite the 4-PL as a function and add it in the form of a stat_function, which is cumbersome to say the 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))
     })
    

  2. If that wasn't 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.

I realise that log10(0) = -Inf but there are ways of handling this. Either (as is the case with plot.drc) the x=0 value is plotted on the x-axis essentially as 1/100 of the pre-lowest x-value. (demo$X[which.min(demo$X)+1]/100) or as in GraphPad Prism, the 0s are omitted from the dose response curve entirely.

My questions are:

  1. Is there a way of plotting drm models in ggplot2 directly?

  2. How can I link a dataset with its corresponding 4-PL curvefit so that they will be plotted in the same colour?

解决方案

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.

这篇关于使用 ggplot2 和 drc 绘制剂量反应曲线的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆