I have a plot done outside of a loop that I want to do through a loop.
The script for this plot is the following:
#perform the fitting
mm.nls <- nls(M2$rate ~ (Vmax * M2$conc /
(Km + M2$conc)), data=M2,
start=list(Km=200, Vmax=1), trace=TRUE )
summary(mm.nls)
# extract coefficients
Km <- unname(coef(mm.nls)["Km"])
Vmax <- unname(coef(mm.nls)["Vmax"])
# prepare data for geom_line
x <- c(M2$conc)
y <- (Vmax*x/(Km+x))
#ggplot
ggplot(M2, aes(x=conc, y=rate, color=trt)) +
geom_point(shape = 16) +
geom_errorbar(aes(ymin=rate-SD, ymax=rate+SD), width = 5)+
geom_line(data = M2, aes(x, y), colour = "black") +
scale_color_brewer(palette="Paired")+
xlab(expression(µmol*~l^-1)) +
ylab(expression(µmol*~l^-1*s^-1)) +
ggtitle("P+") +
theme_bw()+theme(panel.grid.major = element_blank(),
panel.grid.minor = element_blank(),
panel.background = element_blank())+
theme(plot.title=element_text(hjust=0.5))
Now I am making many of these plots in a loop, but I don't know how to add the line so that the formula uses the respective values, but so that it is only one average line.
If I do it like this I get three lines, made according to the formula for each biological sample, but not one average line.
#####loop ggplot geomsmooth 3 lines#####
M3 <- subset(MonoR3, MonoR3$conc<600)
trtlist2 <- unique(M3$trtlist)
for (i in seq_along(trtlist2)) { plot <-
ggplot(subset(M3, M3$trtlist==trtlist2[i]),
aes(conc, rate, color=trt)) +
geom_errorbar(aes(ymin=rate-SD, ymax=rate+SD), width = 5) +
geom_point() +
scale_color_discrete("sample")+
geom_smooth(method = nls, formula = y ~ (a *x/(b + x)),
method.args = list(start = list(a = 1000, b = 0.00001)),
se = F, size = 0.5) +
theme_bw()+theme(panel.grid.major = element_blank(),
panel.grid.minor = element_blank(),
panel.background = element_blank())+
theme(plot.title=element_text(hjust=0.5))+
xlab(expression(µmol*~l^-1)) +
ylab(expression(µmol*~l^-1*s^-1)) +
ggtitle(paste(trtlist2[i], sep=''))
print(plot)
}
So is it possible to calculate an average line of three "datagroups" in geom_smooth? Otherwise I can calculate the values for x and y (according to this: x <- c(M2$conc) y <- (Vmax*x/(Km+x))) put them in a dataframe and then use them, but I didn't manage to then in a loop use the right values for the respective samples/plots haha. Thanks so much!