0

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.

enter image description here

Any suggestion? How can I add the values of R2 for each data in each plot?

Kamil Slowikowski
  • 4,184
  • 3
  • 31
  • 39
user3231352
  • 799
  • 1
  • 9
  • 26
  • 1
    I suggest use to give a look at [ggrepel](https://cran.r-project.org/web/packages/ggrepel/vignettes/ggrepel.html). This package provide usefull tools to avoid overlapping in plot labels. – Thomas Feb 02 '18 at 13:06
  • Possible duplicate of [Annotating text on individual facet in ggplot2](https://stackoverflow.com/questions/11889625/annotating-text-on-individual-facet-in-ggplot2) – Kamil Slowikowski Feb 04 '18 at 00:11

1 Answers1

0

When you specify x and y in geom_text you are assigning the same coordinates for all the text so it would make sense that they overlap. I usually get around this by creating a data frame that has x and y coordinates for each group. For your data this could look like:

require(dplyr)
require(ggplot2)
new_data = test %>% group_by(code, model) %>% summarise(r2  = max(r2))
new_data$xposition = 40
new_data$yposition = c(200,170,200,170)

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=new_data,aes(x = xposition, y = yposition, color=model, label = paste("R2: ", 
                                                 round(r2,2),sep="")),show.legend = F) 

enter image description here

Kamil Slowikowski
  • 4,184
  • 3
  • 31
  • 39