我想从 中制作剂量反应曲线library(drc)
,并坚持如何正确准备我的数据集以制作绘图。特别是,我正在努力如何准备好我的 y 轴。
我制作了一个数据框(df)
来帮助阐明我想做什么。
df <- read.table("https://pastebin.com/raw/TZdjp2JX", header=T)
为今天的练习打开必要的库
library(drc)
library(ggplot2)
假设我喜欢蜂鸟,并用不同浓度的糖做一个实验,目的是看看哪种浓度最适合蜂鸟。因此,我在封闭环境(此处为“房间”列)中进行了一项实验,有 4 种不同的糖浓度(列浓度),每个浓度有 10 只单独的鸟。我还用 4 个平行重复运行每个实验,这就是为什么有 4 个“房间”。36 小时后(“时间”列),我进入房间,检查有多少只鸟幸存下来,创建一个“是/否”变量,或 1 和 0(这里,这是我的“状态”列),其中 1= =生存,0 ==死亡。
使用这个数据集,我特别设定了大多数在浓度 0 下存活,50% 在浓度 1 中存活,25% 在浓度 2 中存活,只有 10% 在浓度 3 中存活。
我遇到的第一个问题是:如何将我的“状态”列生成的 y 轴转换为百分比?我在做 kaplan-meier 生存曲线时已经这样做了,但不幸的是,这在这里不起作用。显然,这应该列应该从 0% 到 100%(我们可以将列称为“死亡率”)。在我成功之后,我想制作一个如下所示的剂量反应曲线(我在网上找到了这个例子,将直接复制到这里使用示例。它来自R中包含的黑麦草数据集)
ryegrass.LL.4 <- drm(rootl ~ conc, data = ryegrass, fct = LL.3())
我必须承认,接下来的代码步骤对我来说有点混乱。
# new dose levels as support for the line
newdata <- expand.grid(conc=exp(seq(log(0.5), log(100), length=100)))
# predictions and confidence intervals
pm <- predict(ryegrass.LL.4, newdata=newdata, interval="confidence")
# new data with predictions
newdata$p <- pm[,1]
newdata$pmin <- pm[,2]
newdata$pmax <- pm[,3]
# plot curve
# need to shift conc == 0 a bit up, otherwise there are problems with coord_trans
ryegrass$conc0 <- ryegrass$conc
ryegrass$conc0[ryegrass$conc0 == 0] <- 0.5
# plotting the curve
ggplot(ryegrass, aes(x = conc0, y = rootl)) +
geom_point() +
geom_ribbon(data=newdata, aes(x=conc, y=p, ymin=pmin, ymax=pmax), alpha=0.2) +
geom_line(data=newdata, aes(x=conc, y=p)) +
coord_trans(x="log") +
xlab("Ferulic acid (mM)") + ylab("Root length (cm)")
最后,我想生成一条类似的曲线,但死亡率在 y 轴上,从 0 到 100(从低开始,到高),并且还在回归线周围的灰色阴影区域中显示置信区间。意思是,我的第一步代码应该类似于以下内容:
model <- drc(mortality ~ Concentration, data=df, fct = LL.3())
但我迷失在“死亡”创作部分,还有一点关于 ggplot 的下一步
谁能帮我实现这一目标?从 的示例中ryegrass
,我很困惑如何将其翻译为对我的假装数据集有所帮助。我希望这里有人能够帮助我解决这个问题!非常感谢,如果有其他方法可以让我的数据集结构化等,我将不胜感激。
-安迪