0

I have a particular ggplot workflow, which I have to stick to due to constraints in the drc package (Christian Ritz), so the workflow is in a way non-standard.

Functions used in this workflow

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")
}

The Workflow

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

The problem:

When I run the above workflow I get "Error: Aesthetics must be either length 1 or the same as the data (200): x, y, colour"

But when I change "# Add the second curve" portion of the workflow to

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

I get the expected result.

I would like to know how can I correctly functionalize adding the second curve to the first one without generating the error.

Deb.M
  • 45
  • 8
  • 1
    Try to pare this down to just the essentials for solving the issue: a sample of data as it is when you need to plot it, the code needed to plot it, maybe an image of the plot. We don't need to define functions and build models and all that if you can just give us the resulting data that needs to be plotted. It's hard to debug when there's a lot of clutter. – camille Mar 16 '19 at 18:19
  • I beg to differ. What you are calling clutter, I think is essential to reproduce the error. 1. sessionInfo() tells you the libraries I am using, replaced it with the required libraries 2. The issue is happening when I am trying to add the second curve using the functions i have defined, therefore I have shared the functions. (edited to remove two lines from the run_drc) 3. The workflow section has the minimal sample set as fig1 (for the first curve and recycled for the second curve) and is quite straight forward. The images were missing, which I have added now. – Deb.M Mar 16 '19 at 21:29

0 Answers0