I have produced some graphs using the base plot function and a for-loop - which provides me with the desired output. However, the plots look horrendous - so I've tried to use ggplot2, but the for-loop fails to produce the same graphs as the base plot
for(j in d1_split$Machine){
plot(d1$Num[which(d1$Machine==j)], d1$Cq[which(d1$Machine==j)],
xlab = j,
ylim = c(d1_split$Lower_1[which(d1_split$Machine==j)]-0.25,
d1_split$Upper_1[which(d1_split$Machine==j)]+0.25),
type = "b", col = "black", pch = 16)
abline(h = d1_split$Avg[which(d1_split$Machine==j)], col = "red")
abline(h = d1_split$Upper_1[which(d1_split$Machine==j)], col = "red")
abline(h = d1_split$Lower_1[which(d1_split$Machine==j)], col = "red")
}
# This semi-works - gives multiple plots with all machines, each with different mean/sd lines
for(i in d1_split$Machine){
print(ggplot(d1,
aes(x = Num, y = Cq, col = Machine)) +
geom_point(size=2) +
geom_line() +
xlab(NULL) +
ylab("Cq value") +
facet_wrap(~ Machine, nrow = 2, ncol = 4) +
geom_hline(yintercept=d1_split$Avg[which(d1_split$Machine==i)],
linetype="dashed", color="red") +
geom_hline(yintercept=d1_split$Upper_1[which(d1_split$Machine==i)],
linetype="dashed", color="red") +
geom_hline(yintercept=d1_split$Lower_1[which(d1_split$Machine==i)],
linetype="dashed", color="red") +
theme(axis.text.x = element_text(colour = "black", angle = 45, vjust = 0.5),
axis.title=element_text(size=16, colour = "black"),
axis.text=element_text(size=14, colour = "black"),
axis.line = element_line(colour = "black"),
panel.background = element_blank(),
legend.text = element_text(size=12),
legend.title = element_text(size=14),
legend.box.background = element_rect(colour = "black")))
}```
I'd like for the ggplot2 for-loop to produce graphs each with different hlines for each facet wrap - but they are always the same - Can anyone spot where I may be going wrong??