Search code examples
rplotdrc

Plot several fitted curves on one plot with drm function


I'm trying to plot some data that has been fitted with the drm() function from the drc package in R. I want to have several curves in the same plot overlapping each other.

I can get one fitted curve and the rest un-fitted into one curve like this:

#This is only mock data to show the concept

library(drc)

CurveData1 <- c(1, 1.1, 1.2, 1.3,2,3,4,5,5.2,5.4, 5.5, 5.6)
CurveData2 <- c(2, 2.1, 2.2, 2.3,3,4,5,6,6.2,6.4, 6.5, 6.6)
CurveData3 <- c(3, 3.1, 3.2, 3.3,4,5,6,7,7.2,7.4, 7.5, 7.6)
Conc <- c(1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12)

fit1 <- drm(CurveData1 ~ Conc, fct = LL.5())


plot(fit1, col = "black")
lines(CurveData2, Conc, col = "orange", type = "b")
lines(CurveData3, Conc, col = "blue", type = "b")

However, when I try to get all of the fitted curves into the same plot, like this:

#This is only mock data to show the concept

library(drc)

CurveData1 <- c(1, 1.1, 1.2, 1.3,2,3,4,5,5.2,5.4, 5.5, 5.6)
CurveData2 <- c(2, 2.1, 2.2, 2.3,3,4,5,6,6.2,6.4, 6.5, 6.6)
CurveData3 <- c(3, 3.1, 3.2, 3.3,4,5,6,7,7.2,7.4, 7.5, 7.6)
Conc <- c(1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12)

fit1 <- drm(CurveData1 ~ Conc, fct = LL.5())
fit2 <- drm(CurveData2 ~ Conc, fct = LL.5())
fit3 <- drm(CurveData3 ~ Conc, fct = LL.5())

plot(fit1, col = "black")
lines(fit2, col = "orange", type = "b")
lines(fit3, col = "blue", type = "b")

I get the following error message:

Error in xy.coords(x, y) : 'x' is a list, but does not have components 'x' and 'y'

Any idea why this occurs and how to get around it? Is it due to a limitation in the lines() function or in the plot() function?


Solution

  • Is this doing the trick?

    library(drc)
    library(ggplot2)
    CurveData1 <- as.data.frame(as.matrix(c(1, 1.1, 1.2, 1.3,2,3,4,5,5.2,5.4, 5.5, 5.6)))
    CurveData2 <- as.data.frame(as.matrix(c(2, 2.1, 2.2, 2.3,3,4,5,6,6.2,6.4, 6.5, 6.6)))
    CurveData3 <- as.data.frame(as.matrix(c(3, 3.1, 3.2, 3.3,4,5,6,7,7.2,7.4, 7.5, 7.6)))
    CurveData1$type = '1'
    CurveData2$type = '2'
    CurveData3$type = '3'
    Conc <- c(1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12)
    all = rbind(CurveData1,CurveData2,CurveData3)
    all$conc = rep(Conc,3)
      
    ggplot(all, aes(x = conc, y = V1, col = type))+
      geom_point()+
      geom_smooth(method = drm, method.args = list(fct = LL.5()), se = FALSE)
    

    Result:

    enter image description here