0

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?

user438383
  • 5,716
  • 8
  • 28
  • 43
Norruas
  • 61
  • 1
  • 9

2 Answers2

1

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

elielink
  • 1,174
  • 1
  • 10
  • 22
  • 1
    Thank you, this is a really neat solution. Unfortunately I have to use the drm() function, which Pauls answer below solves. But this is also useful to know, thanks!:) – Norruas Aug 11 '21 at 12:22
  • 1
    Turns out this solution was much more versatile after some modifications, thank you! – Norruas Aug 27 '21 at 13:00
1

I do not run into an error if I use only plot and add = TRUE. Moreover, if you look at ?plot.drc you will find how to use the plot() function with drc objects.

  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)
plot(fit2, add = TRUE, col = "orange")
plot(fit3, add = TRUE, col = "blue")

Created on 2021-08-09 by the reprex package (v2.0.0)

Paul
  • 2,850
  • 1
  • 12
  • 37
  • Thank you! This is exactly what I needed! Also, thank you for clarifying how to look it up myself, I tried to google but didn't find anything (to my knowledge at least). :) – Norruas Aug 11 '21 at 12:24