简体   繁体   中英

Several model regressions in one plot in R

I am working with drc-package https://cran.r-project.org/web/packages/drc/drc.pdf .

This package add sigmoidal regression to my data set and usually I can plot the model regression by plot(model) function. However, I have now 4 regression models in my code and try to compile them in one plot. points() and lines() functions don't work properly for me so far.

Now, I compile all curves in one plot via par(new = TRUE) function, but the y-axis contains 4 denotations for each plot.

Do you know how I get all 4 regression models in one plot with a proper y-axis?

conc <- c(0.001, 0.04, 0.1, 0.4, 1, 4)
#meanOD1 <-c(1.69355, 1.43355, 0.847025, 0.74654, 0.70775, 0.57331)
#meanOD2 <-c(1.4211666667, 1.2064333333, 0.9020533333, 0.7642633333, 0.5352066667, 0.332945)
#meanOD3 <-c(1.07451, 0.98538, 1.2969333333, 0.9872366667, 0.78839, 0.51288)
#meanOD4 <-c(1.2974333333, 1.3021333333, 1.2579333333, 1.138, 0.9517233333, 0.332945)
meanOD1 <-c(100, 84.6476336689, 50.0147618907, 44.0813675416, 41.7909125801, 33.8525582357)
meanOD2 <-c(100, 84.8903483054, 63.4727336695, 53.7771783746, 37.6596692858, 23.4275829717)
meanOD3 <-c(100, 89.7321264979, 95.0517302454, 72.3541842256, 57.7807907948, 37.5887720327)
meanOD4 <-c(100, 100.3622536803, 96.9555275801, 87.7116357937, 73.3543149295, 25.6618194898)

df.ELISA1<-data.frame(meanOD1, conc)
df.ELISA1
df.ELISA2<-data.frame(meanOD2, conc)
df.ELISA2
df.ELISA3<-data.frame(meanOD3, conc)
df.ELISA3
df.ELISA4<-data.frame(meanOD4, conc)
df.ELISA4
model1 <- drm(df.ELISA1, fct = LL.4())
model1
summary(model1)
modelFit(model1)

model2 <- drm(df.ELISA2, fct = LL.4())
model2
summary(model2)
modelFit(model2)

model3 <- drm(df.ELISA3, fct = LL.4())
model3
summary(model3)
modelFit(model3)
model4 <- drm(df.ELISA4, fct = LL.4())
model4
summary(model4)
modelFit(model4)


plot(model1, type="average", col="red", pch = "o")
par(new=TRUE)
plot(model2, type="average", col="dark green",  pch = "+" )
par(new=TRUE)
plot(model3, type="average", col="blue",  pch = "-" )
par(new=TRUE)
plot(model4, type="average", col="orange",  pch = "*")

legend(1,100,legend=c("V1","V2","V3", "V4"), col=c("red","green","blue", "orange"),
       pch=c("o","+","-", "*"),lty=c(1,2,3,4), ncol=1)

# plot the first curve by calling plot() function
# First curve is plotted
#plot(model1, type="o", col="blue", pch="o", lty=1, ylim=c(0,110) )

# Add second curve to the same plot by calling points() and lines()
# Use symbol '*' for points.
#points(model2, col="red", pch="*")
#lines(model2, col="red",lty=2)

# Add Third curve to the same plot by calling points() and lines()
# Use symbol '+' for points.
#points(model3, col="dark red",pch="+")
#lines(model3, col="dark red", lty=3)

# Add Third curve to the same plot by calling points() and lines()
# Use symbol '+' for points.
#points(model4, col="green",pch="-")
#lines(model4, col="green", lty=3)

Using ggplot2 and a little bit of simplification/automation. I make a loop to not repeat the same stuff.

I added solution for plot.drc - you have to simple use add argument equal to true for second and next plots.

df.ELISA<- list(data.frame(meanOD1, conc),
                data.frame(meanOD2, conc),
                data.frame(meanOD3, conc),
                data.frame(meanOD4, conc))

library(drc)

results <- list()

for (i in seq_len(length(df.ELISA))) {
model <- drm(df.ELISA[[i]], fct = LL.4())
modelFit(model)
datas <-  cbind(model$data, i)
colnames(datas) <- c("conc","meanOD","a","b", "weights", "i")
results[[i]] <- list(model = model, summy = summary(model), data = datas)
}
plot(results[[1]]$model, type="average", col="red", pch = "o")
plot(results[[2]]$model, type="average", col="dark green",  pch = "+", add = TRUE )
plot(results[[3]]$model, type="average", col="blue",  pch = "-", add = TRUE )
plot(results[[4]]$model, type="average", col="orange",  pch = "*", add = TRUE)

legend(0.3,100,legend=c("V1","V2","V3", "V4"), col=c("red","green","blue", "orange"),
       pch=c("o","+","-", "*"),lty=c(1,2,3,4), ncol=1)

在此处输入图片说明

Bonus - using poly regression:

data_plot <- do.call(rbind, lapply(1:length(results), function(i) results[[i]]$data))
data_plot$iter <- factor(data_plot$i)

library(ggplot2)

formula <- y ~ poly(x, 2, raw = TRUE)

ggplot(data_plot, aes(x= log(conc), y = meanOD, col = iter, group = iter, shape = iter))  +
  geom_point() +
  stat_smooth(method = "lm", formula = formula, level = 0.1)

在此处输入图片说明

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM