1

Hey I am trying to do ggplot looping through 4 different cases, I have a data with 5 variables, I would like to plot out all points, regression lines, and add y=ax+b, and r^2 in the plots. All are plotted in a 4 panels plots. I use print(do.call(grid.arrange,plot) in the end, it will generate 4 panels in same plot

enter image description here

If I comment out this line, it generates:

enter image description here

As you can see, the scatter plots are different. I wonder why this happens. Also, the y=ax+b and r^2 look ugly, how to clean it up?

Thank you for your help

lm_eqn1 <- function(df,x,y){
  m <- lm(y ~ x, df);
#  eq <- 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<-  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 <- function(df,x,y){
  m <- lm(y ~ x, df);
  eq2<- substitute(italic(r)^2~"="~r2,
                   list(r2 = format(summary(m)$r.squared, digits = 3)))
  as.character(as.expression(eq2));
}

plot = list()
df <-    data.frame(as.vector(data[[1]]),as.vector(data[[2]]),as.vector(data[[3]]),as.vector(data[[4]]),as.vector(data[[5]]))
colnames(df) <- case
p = 1
for (k in 1:1){
  for (j in 2:5){
#    for (p in 1:4){
    x_lab <- paste(case[k]," [ug/m3]",sep=" ")
    y_lab <- 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))
Jaap
  • 81,064
  • 34
  • 182
  • 193
  • 1
    For the text use `annotate` instead of `geom_text` as explained [here](http://stackoverflow.com/questions/10952832/ggplot2-is-there-a-fix-for-jagged-poor-quality-text-produced-by-geom-text/10953050#10953050) – aosmith Jul 11 '16 at 21:22
  • 2
    (1) It is also probably better to rename your function from `plot` to another name as there is also a `plot` function already present in R. (2) Did you look at `facet_wrap` from `ggplot2`? – Jaap Jul 11 '16 at 21:23
  • 1
    Probably better to not use a `d` object which exists outside the ggplot2 evaluation frames. Otherwise there may not be a proper set of x and y values of values at time of evaluation. – IRTFM Jul 11 '16 at 21:38
  • Perhaps you can provide us with a _minimal reproducible_ example. – Axeman Jul 11 '16 at 21:54
  • Seems your axes are the same in your loop. Why not just rely on the built-in `facet_wrap`? – ako Jul 12 '16 at 04:02
  • Thanks all, I will try facet_wrap. How can I provide a minimal reproducible example? – Jiaoyan Huang Jul 12 '16 at 12:32
  • Search SO for "minimal reproducible example" and look for the one with lots of votes - it's not very far down the list... – Mist Jul 13 '16 at 04:29
  • I finally made my plot by assign data with two different levels (factors) and facet_grid. Thank all of your help. here is my final code https://gist.github.com/JiaoyanHuang/c10e51c4779885ace1ec272a135128c4 – Jiaoyan Huang Jul 15 '16 at 13:33

1 Answers1

0

Huang. Your script was very helpful to me. For your question, if you use 'aes_string()' instead of 'aes()' in the first line in ggplot(), it will works.

ggplot(df, aes_string(x=x, y=y))
changkx
  • 13
  • 5