I am trying to add the values of R2 in scatter plots for several data set and also using facet_grid. So, I want to add several text (values of R2, each one for each data set) in each plot. I have been looking for similar examples, but I couldn't get the right approach, because I don't know how to set the x and y position for the text.
This a very short sample of my original data:
dput(test)
structure(list(code = c("AT0ENK1", "AT0ENK1", "AT0ENK1", "AT0ENK1",
"AT0ENK1", "AT0ENK1", "AT0ENK1", "AT0ENK1", "AT0ILL1", "AT0ILL1",
"AT0ILL1", "AT0ILL1", "AT0ILL1", "AT0ILL1", "AT0ILL1", "AT0ILL1"
), model = structure(c(2L, 2L, 2L, 2L, 6L, 6L, 6L, 6L, 2L, 2L,
2L, 2L, 6L, 6L, 6L, 6L), .Label = c("Obs", "EMEP", "LOTO", "MATCH",
"MINNI", "WRFF", "WRFM"), class = "factor"), O3 = c(118.037246704102,
105.963432312012, 102.795967102051, 107.245376586914,
101.879364013672,
124.914794921875, 129.386352539062, 115.475601196289,
96.2464294433594,
113.553771972656, 108.113143920898, 95.6128845214844,
104.497161865234,
111.243560791016, 121.166435241699, 118.756866455078), O3obs =
c(144.424,
151.726, 151.866, 139.439, 144.424, 151.726, 151.866, 139.439,
164.202, 171.715, 158.06, 137.473, 164.202, 171.715, 158.06,
137.473), r2 = c(0.485277006453918, 0.485277006453918,
0.485277006453918,
0.485277006453918, 0.277829662775301, 0.277829662775301,
0.277829662775301,
0.277829662775301, 0.0429530296631768, 0.0429530296631768,
0.0429530296631768,
0.0429530296631768, 0.0332266668960316, 0.0332266668960316,
0.0332266668960316,
0.0332266668960316)), .Names = c("code", "model", "O3", "O3obs",
"r2"), class = "data.frame", row.names = c(1L, 2L, 3L, 4L, 125L,
126L, 127L, 128L, 187L, 188L, 189L, 190L, 311L, 312L, 313L, 314L
))
And I tried it with:
ggplot( test, aes(O3obs,O3, group= model)) +
geom_point(aes(color=model),size=1)+xlim(0,200) + ylim (0,200) +
geom_abline(intercept = 0, slope = 1) + facet_wrap(~code) +
geom_text(data=test, aes(color = model, label = paste("R2: ", round(r2,2), sep="")), x=180, y=Inf, show.legend = F)
But the values of R2 are overlapped.
Any suggestion? How can I add the values of R2 for each data in each plot?