0

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??
  • 2
    To help us to help would you mind sharing [a minimal reproducible example](https://stackoverflow.com/questions/5963269/how-to-make-a-great-r-reproducible-example) including a snippet of your data or some fake data. To share your data, you could type `dput(NAME_OF_DATASET)` into the console and copy & paste the output starting with `structure(....` into your post. If your dataset has a lot of observations you could do e.g. `dput(head(NAME_OF_DATASET, 20))` for the first twenty rows of data. – stefan Oct 18 '21 at 07:45
  • 4
    Instead of using a for loop it would be much more "ggplot like" to use an aggregated data.frame (with intercepts for each facet) to plot the hlines all in one go... If you added a MRE (and focus on the **minimal** aspect, just as much data and code as is required to only reproduce the *actual* problem) we could give you some hints. – dario Oct 18 '21 at 08:26
  • OP, you're going to have to share some data or an example using a built-in dataset for us to help you more. As @dario mentioned, it seems what you want to do does not require a `for` loop, but instead just requires you to use `facet_wrap()`. Are you looking to get one plot (each facet with different lines), or are you looking to have *multiple* plots, each one with many facets with different lines? – chemdork123 Oct 18 '21 at 12:18

0 Answers0