R:将ggplot工作流程功能化(DRC建模)会导致错误

时间:2019-03-16 16:11:45

标签: r ggplot2 drc

我有一个特定的ggplot工作流程,由于drc软件包(Christian Ritz)中的限制,我必须坚持使用该流程,因此该工作流程在某种程度上是非标准的。

此工作流程中使用的功能

library(ggplot2)
library(ggpubr)
library(drc)


("run_drc_model") 
run_drc_model <- function(d,x,y,z,a,b){
demo.LL.4 <- drm(data = d, y~x, fct=LL.4(),na.action = na.omit) # run model.
demo.fits <<- expand.grid(Pr=exp(seq(log(a), log(b), length=200))) # generate a data frame with simulated x values
pm <<- predict(demo.LL.4, newdata=demo.fits, interval="confidence") # predict the y values of the simulated x values using the above model
    demo.fits$p <<- pm[,1] # store the predicted y values in the p column of a dataframe
    demo.fits$pmin <<- pm[,2] # store the prediction CI lower limit for each prediction
    demo.fits$pmax <<- pm[,3] # store the prediction CI upper limit for each prediction
}

("idrcplot")  
idrcplot <- function(d,x,y,xlab="Cell Lysate %",ylab="Specific A450",col="FBS"){
  ggplot(d, aes(x = x, y = y, color=col)) +
  scale_color_manual(values = treatment.colors)+
  geom_point() +
  geom_ribbon(data=demo.fits, aes(x=Pr, y=p, ymin=pmin, ymax=pmax), alpha=0.2,color=NA) +
  geom_line(data=demo.fits, aes(x=Pr, y=p), size=.8)+
  coord_trans(x="log")+
  xlab(xlab) +
  ylab(ylab) +
  annotation_logticks(scaled = FALSE)+
  theme_pubr()      
}

("idrcadd")
idrcadd <- function(x="#00a14b"){
p1 +
  geom_point(data=fig1, aes(x = Protein, y = A450_bc, color=x)) +
  geom_ribbon(data=demo.fits, aes(x=Pr, y=p, ymin=pmin, ymax=pmax), alpha=0.2, color=NA) +
  geom_line(data=demo.fits, aes(x=Pr, y=p, color=x), size=.8)+
  coord_trans(x="log")
}

工作流程

treatment.colors <- c("col1"="#1c75bc","col2"="#ed1c24")
fig1 <- data.frame(Protein = c(NA,NA,1.842959,1.842959,3.685917,3.685917,7.371834,7.371834,14.743668,14.743668,29.487337,29.487337,58.974674,58.974674,117.949347,117.949347), A450_bc = c(0.000,0.000,0.140,0.194,0.350,0.399,0.574,0.714,1.231,1.188,2.511,2.318,3.447,3.233,3.542,3.555)) # First curve

run_drc_model(fig1,fig1$Protein,fig1$A450_bc,"col1",1,250)
p1 <- idrcplot(fig1,fig1$Protein,fig1$A450_bc,"Protein","A450_bc","col1")
p1 #See image 1

image1

# Add the second curve

fig1 <- data.frame(Protein = c(NA,NA,2.332072,2.332072,4.664145,4.664145,9.328290,9.328290,18.656579,18.656579,37.313158,37.313158,74.626317,74.626317,149.252633,149.252633),A450_bc = c(0.000,0.000,0.138,0.294,0.246,0.432,0.406,0.547,0.895,0.849,1.842,1.810,3.007,2.918,3.419,3.469)) # Second curve

run_drc_model(fig1,fig1$Protein,fig1$A450_bc,"col2",1,250)
    p1 <- idrcadd("col2")
    p1 # See image 2

image2

问题:

  

运行上述工作流程时,我收到“错误:美学的长度必须为1或与数据(200)相同:x,y,颜色”

但是当我将工作流程的“#Add the第二条曲线”部分更改为

fig1 <- data.frame(Protein = c(NA,NA,2.332072,2.332072,4.664145,4.664145,9.328290,9.328290,18.656579,18.656579,37.313158,37.313158,74.626317,74.626317,149.252633,149.252633), A450_bc = c(0.000,0.000,0.138,0.294,0.246,0.432,0.406,0.547,0.895,0.849,1.842,1.810,3.007,2.918,3.419,3.469)) # Second curve
run_drc_model(fig1,fig1$Protein,fig1$A450_bc,"col2",1,250)
p1 <- p1 +
  geom_point(data=fig1, aes(x=Protein, y=A450_bc, color="col2")) +
  geom_ribbon(data=demo.fits, aes(x=Pr, y=p, ymin=pmin, ymax=pmax), alpha=0.2, color=NA) +
  geom_line(data=demo.fits, aes(x=Pr, y=p, color="col2"), size=.8)+
  coord_trans(x="log")
p1 # See image 3

image3

我得到了预期的结果。

我想知道如何正确地将第二条曲线添加到第一条曲线而不会产生错误。

0 个答案:

没有答案