4

I want to show the linear equation and the R-squared in the each plot in facet mode. This is my code so far.

library("ggplot2")
datos <- read.table("~/Documents/master2/plots/dosis_todos/datos.dat", header=TRUE, quote="\"")
ggplot(datos, aes(x = corriente, y = dosis, colour = cristal)) +    
geom_point() + geom_smooth(method="lm", se=F) + 
facet_wrap(~datos$cristal)

After reading about ggpmisc in this answer, I tried

my.formula <- y ~ x
library("ggpmisc")
ggplot(datos, aes(x = corriente, y = dosis, colour = cristal)) +    
geom_point() + 
geom_smooth(method="lm", se=F, formula=my.formula) +
stat_poly_eq(aes(label = paste(..eq.label.., ..rr.label.., sep = "~~~")), formula = my.formula, parse = TRUE) +
facet_wrap(~datos$cristal)

Which kinda works, except that the position of the equation goes down for every plot until disappears...

enter image description here

If I save my plot big enough, I can see all my text in the 9 plots ....going down.

So I guess the question is how to keep fixed the position of the equation and the R-squared information?

Thanks

Ps. Yes, I know N57 has only 3 points :(

Ps. Here is the link to my data

murpholinox
  • 639
  • 1
  • 4
  • 14
  • 1
    Please share sample of your data using `dput()` (not `str` or `head` or picture/screenshot) so others can help. See more here https://stackoverflow.com/questions/5963269/how-to-make-a-great-r-reproducible-example?rq=1 – Tung Jul 10 '18 at 20:26
  • Based on reading [the vignette](https://cran.r-project.org/web/packages/ggpmisc/vignettes/user-guide-1.html#stat_poly_eq), I think it's because you are coloring the equation so they are stacked (there is an example of this pretty far down in the examples). You could remove the global `color` mapping and put it in the other layers or use something like `color = black` to override that aesthetic. – aosmith Jul 10 '18 at 20:27

2 Answers2

3

@murpholinox Yes, you are correct, the code in 'ggpmisc' is not smart enough (yet) to detect when aesthetics values like the different colours are unique to each panel. However, it is possible to manually position the equations passing a position in data units to parameters label.y and/or label.x. So, there is a work-around.

library("ggplot2")
library("ggpmisc")
datos <- read.table("datos.dat", header=TRUE, quote="\"")
my.formula <- y ~ x
ggplot(datos, aes(x = corriente, y = dosis, colour = cristal)) +
geom_point() +
geom_smooth(method="lm", se=F, formula=my.formula) +
stat_poly_eq(aes(label = paste(..eq.label.., ..rr.label.., sep = "~~~")),
             formula = my.formula, parse = TRUE, label.y = 0.9) +
ylim(0, 1) +
facet_wrap(~datos$cristal)

plot

It is also possible to pass a vector to label.y and label.x, so that each equation can be manually positioned for each panel.

ggplot(datos, aes(x = corriente, y = dosis, colour = cristal)) +
geom_point() +
geom_smooth(method="lm", se=F, formula=my.formula) +
stat_poly_eq(aes(label = paste(..eq.label.., ..rr.label.., sep = "~~~")),
             formula = my.formula, parse = TRUE, 
             label.y = c(rep(0.9, 6), rep(0.15, 2), 0.9)) +
ylim(0, 0.95) +
facet_wrap(~datos$cristal)

enter image description here

Pedro J. Aphalo
  • 5,796
  • 1
  • 22
  • 23
  • Nice. I was manually adjusting the equation and the R squared in inkscape. :P – murpholinox Jul 14 '18 at 18:13
  • In all of the posts I've seen about stat_poly_eq everyone sets `sep = "~~~"` to add some space. What would the sep argument look like if I wanted R^2 on the next line? My graphs, like the example above, do not have enough horizontal space to accomodate the equation and R^2 on the same line, unless I really shrink the text size. – JJGabe Jun 10 '20 at 13:39
  • 1
    As far as I know a line break is not possible within an R expression. In this case best option would be to add two layers, one with the equation and one with R2. By this I mean adding stat_poly_eq() twice. Another option is to map size, say `size = 2.7`. – Pedro J. Aphalo Jun 10 '20 at 20:32
  • @PedroAphalo Thanks for that. I can get exactly the results I want by adding two stat_poly_eq() lines as you suggested. Plus I get the added benefit of being able to manipulate the appearance of the equation and R2 separately! – JJGabe Jun 28 '20 at 20:07
  • @PedroAphalo Another question, do you know how to force the regression line through the origin. I tried `OriginFormula <- y ~ (0 + x)` based on other posts I saw but the results don't look right to me. Does this make sense to you? – JJGabe Jun 28 '20 at 20:26
  • OriginFormula <- y ~ x - 1 – Pedro J. Aphalo Jun 28 '20 at 22:48
0

Thanks to the comment of @aosmith, I could do what I want. enter image description here

The code is:

ggplot(datos, aes(corriente, dosis)) +
     geom_point(shape = 21, size = 3) +
     geom_smooth(method="lm", se=F, formula=my.formula) +
     stat_poly_eq(aes(label = paste(..eq.label.., ..rr.label.., sep = "~~~")), formula = my.formula, parse = TRUE) +
     facet_wrap(~datos$cristal)
murpholinox
  • 639
  • 1
  • 4
  • 14