用ggplot2绘制drc :: drc图 [英] drc:: drc plot with ggplot2
问题描述
我试图用 ggplot2
重现 drc
地块。这是我第一次尝试(MWE在下面给出)。不过,我的gplot2与基本的R图有些不同。我想知道我是否在这里失去了一些东西。任何反馈将不胜感激。谢谢
library(drc)
chickweed.m1< - drm(count〜start + end,data = chickweed ,fct = LL.3(),type =event)
plot(chickweed.m1,xlab =Time(hours),ylab =发芽比例,
xlim = c(0,340),ylim = c(0,0.25),log =,lwd = 2,cex = 1.2)
library(data.table )
dt1 < - data.table(杂音)
dt1Means1 < - dt1 [,。(发芽=平均值(计数值)/ 200),by =。(start)] $ (数据表)(dt1Means2 [开始!= 0],Pred =预测(对象=开始,发芽= cumsum(发芽))] chickweed.m1))
library(ggplot2)
ggplot(data = dt1Means,mapping = aes(x = start,y = Germinated))+
geom_point()+
geom_line(aes(y = Pred))+
lims(y = c(0,0.25))+
theme_bw()
编辑
我遵循给定
这与您在ggplot中使用predict创建的版本(object = chickweed.m1)相同。因此,差异不在模型线中,而在于绘制数据点的位置。我们可以通过将函数的最后一行从不可见(retData)
更改为 list(retData)来从drc ::: plot.drc中导出数据点,plotPoints)
。为了方便起见,我将drc ::: plot.drc的全部代码复制到一个新函数中。请注意,如果您想复制此步骤,drcplot调用的一些函数不会在drc命名空间中导出,因此需要将 drc :::
预置为所有对函数 parFct
, addAxes
, brokenAxis
的调用,和 makeLegend
。
drcplot< - function(x,.. 。,add = FALSE,level = NULL,type = c(average,
all,bars,none,obs,confidence),broken = FALSE,
bp,bcontrol = NULL,conName = NULL,axes = TRUE,gridsize = 100,
log =x,xtsty,xttrim = TRUE,xt = NULL,xtlab = NULL,
xlab,xlim ,yt = NULL,ytlab = NULL,ylab,ylim,cex,cex.axis = 1,
col = FALSE,lty,pch,legend,legendText,legendPos,cex.legend = 1,
normal = FALSE,normRef = 1,confidence.level = 0.95)
{
#...很多行被忽略...
list(retData,plotPo ints)
}
并将其与您的数据一起运行
pl < - drcplot(chickweed.m1,xlab =时间(小时),ylab =发芽比例,
xlim = c 0,340),ylim = c(0,0.25),log =,lwd = 2,cex = 1.2)
germ.points< - as.data.frame(pl [ 2]])
drc.fit< - as.data.frame(pl [[1]])
names(germ.points)< - c(x,y)
名称(drc.fit)< - c(x,y)
<现在,使用ggplot2来绘制这些图片会得到您要查找的内容。
$ p $ g $ p $ ggplot(data = dt1Means,mapping = aes(x =开始,y =发芽))+
geom_point(data = germ.points,aes(x = x,y = y))+
geom_line(data = drc.fit,aes(x = x, y = y))+
lims(y = c(0,0.25))+
theme_bw()
最后,比较使用原始ggplot( dt1Means
)中的数据点( germ.points
)的数据点值显示造成差异的原因。您在 dt1Means
中的计算点相对于plot.drc中的点更早地移动了一个时间段。换句话说,plot.drc将事件分配到它们出现的时间段的结束时间,而您正在将萌芽事件分配到它们出现的时间间隔的开始处。例如,您可以通过使用
dt1 < - data.table(杂烩)
dt1 [,发芽:=平均(计数)/ 200,by =开始]
dt1 [,cum_Germinated:= cumsum(发芽)]
dt1 [,Pred:= c(预测(object = chickweed.m1 ),NA)]#请注意,结束于'Inf'的最后时间段无法由模型预测,因此在最后一行添加了'NA'
ggplot(data = dt1,mapping = aes(x = end,y = cum_Germinated))+
geom_point()+
geom_line(aes(y = Pred))+
lims(y = c(0,0.25)) +
theme_bw()
I'm trying to reproduce drc
plots with ggplot2
. Here is my first attempt (MWE is given below). However, my ggplot2 is little bit different from base R plot. I wonder if I am missing something here. Any feedback will be highly appreciated. Thanks
library(drc)
chickweed.m1 <- drm(count~start+end, data = chickweed, fct = LL.3(), type = "event")
plot(chickweed.m1, xlab = "Time (hours)", ylab = "Proportion germinated",
xlim=c(0, 340), ylim=c(0, 0.25), log="", lwd=2, cex=1.2)
library(data.table)
dt1 <- data.table(chickweed)
dt1Means1 <- dt1[, .(Germinated=mean(count)/200), by=.(start)]
dt1Means2 <- dt1Means1[, .(start=start, Germinated=cumsum(Germinated))]
dt1Means <- data.table(dt1Means2[start!=0], Pred=predict(object=chickweed.m1))
library(ggplot2)
ggplot(data= dt1Means, mapping=aes(x=start, y=Germinated)) +
geom_point() +
geom_line(aes(y = Pred)) +
lims(y=c(0, 0.25)) +
theme_bw()
Edited
I followed the methodology (with some changes) given here.
NB, you can skip to the final paragraph for the simple answer. The rest of this answer documents how I arrived at that solution
Looking at the code for drc:::plot.drc, we can see that the final line invisibly returns a data.frame retData
function (x, ..., add = FALSE, level = NULL, type = c("average",
"all", "bars", "none", "obs", "confidence"), broken = FALSE,
bp, bcontrol = NULL, conName = NULL, axes = TRUE, gridsize = 100,
log = "x", xtsty, xttrim = TRUE, xt = NULL, xtlab = NULL,
xlab, xlim, yt = NULL, ytlab = NULL, ylab, ylim, cex, cex.axis = 1,
col = FALSE, lty, pch, legend, legendText, legendPos, cex.legend = 1,
normal = FALSE, normRef = 1, confidence.level = 0.95)
{
# ...lot of lines omitted...
invisible(retData)
}
retData contains the coordinates for the fitted model line, so we can use this to ggplot the same model that plot.drc uses
pl <- plot(chickweed.m1, xlab = "Time (hours)", ylab = "Proportion germinated",
xlim=c(0, 340), ylim=c(0, 0.25), log="", lwd=2, cex=1.2)
names(pl) <- c("x", "y")
ggplot(data= dt1Means, mapping=aes(x=start, y=Germinated)) +
geom_point() +
geom_line(data=pl, aes(x=x, y = y)) +
lims(y=c(0, 0.25)) +
theme_bw()
Which is the same as the version you created in ggplot using predict(object=chickweed.m1). So, the difference is not in the model lines, but in where the data points are plotted. We can export the data point from drc:::plot.drc by changing the last line of the function from invisible(retData)
to list(retData, plotPoints)
. For convenience, I copied the entire code of drc:::plot.drc into a new function. Note that if you wish to replicate this step, there are a few functions called by drcplot that are not exported in the drc namespace, so drc:::
needs to be prepended to all calls to the functions parFct
, addAxes
, brokenAxis
, and makeLegend
.
drcplot <- function (x, ..., add = FALSE, level = NULL, type = c("average",
"all", "bars", "none", "obs", "confidence"), broken = FALSE,
bp, bcontrol = NULL, conName = NULL, axes = TRUE, gridsize = 100,
log = "x", xtsty, xttrim = TRUE, xt = NULL, xtlab = NULL,
xlab, xlim, yt = NULL, ytlab = NULL, ylab, ylim, cex, cex.axis = 1,
col = FALSE, lty, pch, legend, legendText, legendPos, cex.legend = 1,
normal = FALSE, normRef = 1, confidence.level = 0.95)
{
# ...lot of lines omitted...
list(retData, plotPoints)
}
and run this with your data
pl <- drcplot(chickweed.m1, xlab = "Time (hours)", ylab = "Proportion germinated",
xlim=c(0, 340), ylim=c(0, 0.25), log="", lwd=2, cex=1.2)
germ.points <- as.data.frame(pl[[2]])
drc.fit <- as.data.frame(pl[[1]])
names(germ.points) <- c("x", "y")
names(drc.fit) <- c("x", "y")
Now, plotting these with ggplot2 gets what you were looking for
ggplot(data= dt1Means, mapping=aes(x=start, y=Germinated)) +
geom_point(data=germ.points, aes(x=x, y = y)) +
geom_line(data=drc.fit, aes(x=x, y = y)) +
lims(y=c(0, 0.25)) +
theme_bw()
Finally, comparing the data point values of this plot (germ.points
) with those in your original ggplot (dt1Means
), shows the reason for the discrepancy. Your calculated points in dt1Means
are shifted one time period earlier relative to those in plot.drc. In other words, plot.drc is assigning the events to the end time of the period in which they occur, whereas you are assigning germination events to the start of the time interval in which they occur. You can simply adjust this by, for example, using
dt1 <- data.table(chickweed)
dt1[, Germinated := mean(count)/200, by=start]
dt1[, cum_Germinated := cumsum(Germinated)]
dt1[, Pred := c(predict(object=chickweed.m1), NA)] # Note that the final time period which ends at `Inf` can not be predicted by the model, therefore added `NA` in the final row
ggplot(data= dt1, mapping=aes(x=end, y=cum_Germinated)) +
geom_point() +
geom_line(aes(y = Pred)) +
lims(y=c(0, 0.25)) +
theme_bw()
这篇关于用ggplot2绘制drc :: drc图的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!