title: R, ggplot, loop, for multiple panels
body: <p>Hey I am trying to do <code>ggplot</code> looping through 4 different cases, I have a data with 5 variables, I would like to plot out all points, regression lines, and add <code>y=ax+b</code>, and <code>r^2</code> in the plots. All are plotted in a 4 panels plots. I use <code>print(do.call(grid.arrange,plot)</code> in the end, it will generate 4 panels in same plot</p>
  
  <p><a href="https://i.stack.imgur.com/EPC9L.png" rel="nofollow noreferrer"><img src="https://i.stack.imgur.com/EPC9L.png" alt="enter image description here"></a></p>
  
  <p>If I comment out this line, it generates: </p>
  
  <p><a href="https://i.stack.imgur.com/U4lbg.png" rel="nofollow noreferrer"><img src="https://i.stack.imgur.com/U4lbg.png" alt="enter image description here"></a></p>
  
  <p>As you can see, the scatter plots are different. I wonder why this happens. Also, the <code>y=ax+b</code> and <code>r^2</code> look ugly, how to clean it up?</p>
  
  <p>Thank you for your help</p>
  
  <pre><code>lm_eqn1 &lt;- function(df,x,y){
    m &lt;- lm(y ~ x, df);
  #  eq &lt;- substitute(italic(y) == a + b %.% italic(x)*","~~italic(r)^2~"="~r2,
  #                   list(a = format(coef(m)[1], digits = 2),
  #                        b = format(coef(m)[2], digits = 2),
  #                        r2 = format(summary(m)$r.squared, digits = 3)))
    eq&lt;-  substitute(italic(y) == b %.% italic(x)+ a,list(a = format(coef(m)[1], digits = 2),b = format(coef(m)[2], digits = 2)))
    as.character(as.expression(eq));
  }
  lm_eqn2 &lt;- function(df,x,y){
    m &lt;- lm(y ~ x, df);
    eq2&lt;- substitute(italic(r)^2~"="~r2,
                     list(r2 = format(summary(m)$r.squared, digits = 3)))
    as.character(as.expression(eq2));
  }
  
  plot = list()
  df &lt;-    data.frame(as.vector(data[[1]]),as.vector(data[[2]]),as.vector(data[[3]]),as.vector(data[[4]]),as.vector(data[[5]]))
  colnames(df) &lt;- case
  p = 1
  for (k in 1:1){
    for (j in 2:5){
  #    for (p in 1:4){
      x_lab &lt;- paste(case[k]," [ug/m3]",sep=" ")
      y_lab &lt;- paste(case[j]," [ug/m3]",sep=" ")
      x=df[,case[k]]
      y=df[,case[j]]
      print(case[k])
      print(case[j])
      plot[[p]] = ggplot(df,aes(x=x,y=y))+
                  geom_point(size=2,alpha = 0.3,color="red")+
                  theme_bw(base_size=12, base_family = "Helvetica")+
                  xlab(x_lab)+
                  ylab(y_lab)+
                  theme(aspect.ratio=1)+
                  ggtitle(case[j]) +
                  geom_smooth(method='lm',se = FALSE, color="black",formula = y ~ x)+
                  geom_text(x=-0.0005,y=0.05,label=lm_eqn1(df,x,y),parse = TRUE)+
                  geom_text(x=-0.0005,y=0.04,label=lm_eqn2(df,x,y),parse = TRUE)
  #    }
       print(plot[[p]])
       p = p + 1
  }
  }
  #print(do.call(grid.arrange,plot))
  </code></pre>