【发布时间】:2016-04-21 20:59:37
【问题描述】:
在生物学中,我们经常想要绘制剂量反应曲线。 R 包“drc”非常有用,基础图形可以轻松处理“drm 模型”。但是,我想将我的 drm 曲线添加到 ggplot2。
我的数据集:
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
))
使用基础图形:
plot(drm(data = reshape2::melt(demo,id.vars = "X"),value~X,fct=LL.4(),na.action = na.omit),type="bars")
生成一个漂亮的 4 参数剂量反应图。
试图在 ggplot2 中绘制相同的图,我偶然发现了 2 个问题。
-
没有办法直接添加drm模型曲线。我需要将 4-PL 重写为函数,并以 stat_function 的形式添加,至少可以说很麻烦。
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)) }) 如果这还不够,它只有在 scale_x 是连续的情况下才有效。如果我想添加
scale_x_log10(),我会得到:Warning message: In log(x): NaNs produced。
我知道log10(0) = -Inf 但有一些方法可以处理这个问题。无论是(与 plot.drc 的情况一样),x=0 值都绘制在 x 轴上,基本上是之前最低 x 值的 1/100。 (demo$X[which.min(demo$X)+1]/100) 或在 GraphPad Prism 中,0 从剂量响应曲线中完全省略。
我的问题是:
有没有办法直接在 ggplot2 中绘制 drm 模型?
如何将数据集与其对应的 4-PL 曲线拟合链接起来,以使它们以相同的颜色绘制?
【问题讨论】:
-
我很想在 ggplot 之外对 drm 进行计算,将结果放入 data.frame,然后将其提供给 ggplot
标签: r plot graphics ggplot2 drc