0

I have a faceted plot like this:

df %>%
  ggplot(aes(x = x, y = IND_all, colour = Story))+
  facet_wrap(. ~ Story, scales = 'free_x') +
  geom_line(alpha = 0.5)+
  geom_point(alpha = 0.6)+
  geom_smooth(method = "lm", formula = y ~ poly(x, 2), se = FALSE, #colour = "grey60", 
              linetype = 1, lwd = 1)+
  geom_smooth(method = "lm", linetype = 2, lwd = 1, se = FALSE)+
  theme(legend.position = "none")+

enter image description here

to which I need to add some text information, for example one unique Mean_deriv value and one unique Slope value; ideally this value should be located topleft; also the values should be preceded by the strings "Mean derivative = " and "Slope = "; however adding a geom_text like this prints all (duplicated) Mean_deriv values:

  geom_text(aes(label = Mean_deriv[1]))

How can I add exactly one Mean_deriv and one Slope value per facet?

Data:

df <- structure(list(Story = c("Beer for plants ", "Beer for plants ", 
                               "Beer for plants ", "Drawing in the dark ", "Drawing in the dark ", 
                               "Drawing in the dark ", "Drawing in the dark ", "Drawing in the dark ", 
                               "Drawing in the dark ", "Drawing in the dark ", "Drawing in the dark ", 
                               "Drawing in the dark ", "Drawing in the dark ", "Easy work ", 
                               "Easy work ", "Easy work ", "Easy work ", "Easy work ", "Easy work ", 
                               "Easy work ", "Easy work ", "Easy work ", "Guessing game ", "Guessing game ", 
                               "Guessing game ", "Guessing game ", "Guessing game ", "Hiking in Serbia ", 
                               "Hiking in Serbia ", "Hiking in Serbia ", "Hiking in Serbia ", 
                               "Hiking in Serbia ", "Hiking in Serbia ", "Hiking in Serbia ", 
                               "Hiking in Serbia ", "Letters in the toilet ", "Letters in the toilet ", 
                               "Letters in the toilet ", "Letters in the toilet ", "Letters in the toilet ", 
                               "Letters in the toilet ", "Letters in the toilet ", "Letters in the toilet ", 
                               "Letters in the toilet ", "Letters in the toilet ", "Missing it ", 
                               "Missing it ", "Missing it ", "Missing it ", "Missing it ", "Missing it ", 
                               "Missing it ", "Missing it ", "Missing it ", "Missing it ", "Narrow streets ", 
                               "Narrow streets ", "Narrow streets ", "Narrow streets ", "Narrow streets ", 
                               "Narrow streets ", "Narrow streets ", "Narrow streets ", "Playing with pirates ", 
                               "Playing with pirates ", "Playing with pirates ", "Playing with pirates ", 
                               "Playing with pirates ", "Playing with pirates ", "Pocket lady ", 
                               "Pocket lady ", "Pocket lady ", "Pocket lady ", "Pocket lady ", 
                               "Pocket lady ", "Pocket lady ", "Pocket lady ", "Pocket lady ", 
                               "Pocket lady ", "Quiet carriage ", "Quiet carriage ", "Quiet carriage ", 
                               "Quiet carriage ", "Quiet carriage ", "Quiet carriage ", "Quiet carriage ", 
                               "Quiet carriage ", "Quiet carriage ", "Quiet carriage ", "Sad story ", 
                               "Sad story ", "Sad story ", "Sad story ", "Sad story ", "Sad story ", 
                               "Sad story ", "Sad story ", "Sad story ", "Sad story ", "Stupid mask ", 
                               "Stupid mask ", "Stupid mask ", "Stupid mask ", "Stupid mask ", 
                               "Stupid mask ", "Stupid mask ", "Stupid mask ", "Toilet woman ", 
                               "Toilet woman ", "Toilet woman ", "Toilet woman ", "Toilet woman ", 
                               "Toilet woman ", "Toilet woman ", "Toilet woman ", "Toilet woman ", 
                               "Toilet woman "), IND_all = c(0, 0.142857142857143, 0.214285714285714, 
                                                             0.0714285714285714, 0.0714285714285714, 0.0714285714285714, 0, 
                                                             0.0714285714285714, 0.0714285714285714, 0.142857142857143, 0.142857142857143, 
                                                             0.142857142857143, 0.0714285714285714, 0.214285714285714, 0.142857142857143, 
                                                             0.0714285714285714, 0.142857142857143, 0.142857142857143, 0.142857142857143, 
                                                             0.214285714285714, 0.142857142857143, 0.142857142857143, 0.142857142857143, 
                                                             0.0714285714285714, 0.0714285714285714, 0.642857142857143, 0.214285714285714, 
                                                             0.214285714285714, 0.285714285714286, 0.142857142857143, 0, 0, 
                                                             0.0714285714285714, 0.285714285714286, 0.214285714285714, 0.0714285714285714, 
                                                             0.142857142857143, 0.166666666666667, 0.214285714285714, 0.142857142857143, 
                                                             0.142857142857143, 0.142857142857143, 0.142857142857143, 0, 0.214285714285714, 
                                                             0.428571428571429, 0, 0, 0.142857142857143, 0.214285714285714, 
                                                             0, 0.285714285714286, 0.166666666666667, 0.142857142857143, 0.0714285714285714, 
                                                             0.285714285714286, 0.0714285714285714, 0.142857142857143, 0.214285714285714, 
                                                             0.0714285714285714, 0.357142857142857, 0.214285714285714, 0.142857142857143, 
                                                             0.285714285714286, 0.142857142857143, 0.214285714285714, 0.0833333333333333, 
                                                             0.142857142857143, 0.142857142857143, 0.0714285714285714, 0.0714285714285714, 
                                                             0.142857142857143, 0.142857142857143, 0.214285714285714, 0.214285714285714, 
                                                             0, 0.285714285714286, 0.214285714285714, 0.214285714285714, 0.214285714285714, 
                                                             0.0714285714285714, 0, 0, 0.0714285714285714, 0, 0.285714285714286, 
                                                             0.142857142857143, 0.142857142857143, 0.285714285714286, 0.0714285714285714, 
                                                             0.142857142857143, 0.142857142857143, 0.0714285714285714, 0.214285714285714, 
                                                             0, 0.0714285714285714, 0.0714285714285714, 0.0714285714285714, 
                                                             0.214285714285714, 0.142857142857143, 0.0714285714285714, 0.142857142857143, 
                                                             0.0714285714285714, 0, 0.0714285714285714, 0.285714285714286, 
                                                             0, 0.142857142857143, 0, 0.214285714285714, 0.0714285714285714, 
                                                             0.0714285714285714, 0.142857142857143, 0, 0.285714285714286, 
                                                             0.142857142857143, 0), x = c(1L, 2L, 3L, 1L, 2L, 3L, 4L, 5L, 
                                                                                          6L, 7L, 8L, 9L, 10L, 1L, 2L, 3L, 4L, 5L, 6L, 7L, 8L, 9L, 1L, 
                                                                                          2L, 3L, 4L, 5L, 1L, 2L, 3L, 4L, 5L, 6L, 7L, 8L, 1L, 2L, 3L, 4L, 
                                                                                          5L, 6L, 7L, 8L, 9L, 10L, 1L, 2L, 3L, 4L, 5L, 6L, 7L, 8L, 9L, 
                                                                                          10L, 1L, 2L, 3L, 4L, 5L, 6L, 7L, 8L, 1L, 2L, 3L, 4L, 5L, 6L, 
                                                                                          1L, 2L, 3L, 4L, 5L, 6L, 7L, 8L, 9L, 10L, 1L, 2L, 3L, 4L, 5L, 
                                                                                          6L, 7L, 8L, 9L, 10L, 1L, 2L, 3L, 4L, 5L, 6L, 7L, 8L, 9L, 10L, 
                                                                                          1L, 2L, 3L, 4L, 5L, 6L, 7L, 8L, 1L, 2L, 3L, 4L, 5L, 6L, 7L, 8L, 
                                                                                          9L, 10L), Mean_deriv = c(0.107142857142857, 0.107142857142857, 
                                                                                                                   0.107142857142857, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, -0.00892857142857143, 
                                                                                                                   -0.00892857142857143, -0.00892857142857143, -0.00892857142857143, 
                                                                                                                   -0.00892857142857143, -0.00892857142857143, -0.00892857142857143, 
                                                                                                                   -0.00892857142857143, -0.00892857142857143, 0.0178571428571429, 
                                                                                                                   0.0178571428571429, 0.0178571428571429, 0.0178571428571429, 0.0178571428571429, 
                                                                                                                   -7.93016446160826e-18, -7.93016446160826e-18, -7.93016446160826e-18, 
                                                                                                                   -7.93016446160826e-18, -7.93016446160826e-18, -7.93016446160826e-18, 
                                                                                                                   -7.93016446160826e-18, -7.93016446160826e-18, 0, 0, 0, 0, 0, 
                                                                                                                   0, 0, 0, 0, 0, -0.0210084033613445, -0.0210084033613445, -0.0210084033613445, 
                                                                                                                   -0.0210084033613445, -0.0210084033613445, -0.0210084033613445, 
                                                                                                                   -0.0210084033613445, -0.0210084033613445, -0.0210084033613445, 
                                                                                                                   -0.0210084033613445, -0.0204081632653061, -0.0204081632653061, 
                                                                                                                   -0.0204081632653061, -0.0204081632653061, -0.0204081632653061, 
                                                                                                                   -0.0204081632653061, -0.0204081632653061, -0.0204081632653061, 
                                                                                                                   -0.0285714285714286, -0.0285714285714286, -0.0285714285714286, 
                                                                                                                   -0.0285714285714286, -0.0285714285714286, -0.0285714285714286, 
                                                                                                                   0.00216450216450217, 0.00216450216450217, 0.00216450216450217, 
                                                                                                                   0.00216450216450217, 0.00216450216450217, 0.00216450216450217, 
                                                                                                                   0.00216450216450217, 0.00216450216450217, 0.00216450216450217, 
                                                                                                                   0.00216450216450217, -0.00324675324675324, -0.00324675324675324, 
                                                                                                                   -0.00324675324675324, -0.00324675324675324, -0.00324675324675324, 
                                                                                                                   -0.00324675324675324, -0.00324675324675324, -0.00324675324675324, 
                                                                                                                   -0.00324675324675324, -0.00324675324675324, 0.00446428571428571, 
                                                                                                                   0.00446428571428571, 0.00446428571428571, 0.00446428571428571, 
                                                                                                                   0.00446428571428571, 0.00446428571428571, 0.00446428571428571, 
                                                                                                                   0.00446428571428571, 0.00446428571428571, 0.00446428571428571, 
                                                                                                                   -0.0204081632653061, -0.0204081632653061, -0.0204081632653061, 
                                                                                                                   -0.0204081632653061, -0.0204081632653061, -0.0204081632653061, 
                                                                                                                   -0.0204081632653061, -0.0204081632653061, 0.0119047619047619, 
                                                                                                                   0.0119047619047619, 0.0119047619047619, 0.0119047619047619, 0.0119047619047619, 
                                                                                                                   0.0119047619047619, 0.0119047619047619, 0.0119047619047619, 0.0119047619047619, 
                                                                                                                   0.0119047619047619), Slope = c(x = 0.107142857142857, x = 0.107142857142857, 
                                                                                                                                                  x = 0.107142857142857, x = 0.00779220779220779, x = 0.00779220779220779, 
                                                                                                                                                  x = 0.00779220779220779, x = 0.00779220779220779, x = 0.00779220779220779, 
                                                                                                                                                  x = 0.00779220779220779, x = 0.00779220779220779, x = 0.00779220779220779, 
                                                                                                                                                  x = 0.00779220779220779, x = 0.00779220779220779, x = -2.95585848285666e-18, 
                                                                                                                                                  x = -2.95585848285666e-18, x = -2.95585848285666e-18, x = -2.95585848285666e-18, 
                                                                                                                                                  x = -2.95585848285666e-18, x = -2.95585848285666e-18, x = -2.95585848285666e-18, 
                                                                                                                                                  x = -2.95585848285666e-18, x = -2.95585848285666e-18, x = 0.0714285714285714, 
                                                                                                                                                  x = 0.0714285714285714, x = 0.0714285714285714, x = 0.0714285714285714, 
                                                                                                                                                  x = 0.0714285714285714, x = -0.00255102040816327, x = -0.00255102040816327, 
                                                                                                                                                  x = -0.00255102040816327, x = -0.00255102040816327, x = -0.00255102040816327, 
                                                                                                                                                  x = -0.00255102040816327, x = -0.00255102040816327, x = -0.00255102040816327, 
                                                                                                                                                  x = 0.00018896447467876, x = 0.00018896447467876, x = 0.00018896447467876, 
                                                                                                                                                  x = 0.00018896447467876, x = 0.00018896447467876, x = 0.00018896447467876, 
                                                                                                                                                  x = 0.00018896447467876, x = 0.00018896447467876, x = 0.00018896447467876, 
                                                                                                                                                  x = 0.00018896447467876, x = -0.00464396284829722, x = -0.00464396284829722, 
                                                                                                                                                  x = -0.00464396284829722, x = -0.00464396284829722, x = -0.00464396284829722, 
                                                                                                                                                  x = -0.00464396284829722, x = -0.00464396284829722, x = -0.00464396284829722, 
                                                                                                                                                  x = -0.00464396284829722, x = -0.00464396284829722, x = 0.00255102040816328, 
                                                                                                                                                  x = 0.00255102040816328, x = 0.00255102040816328, x = 0.00255102040816328, 
                                                                                                                                                  x = 0.00255102040816328, x = 0.00255102040816328, x = 0.00255102040816328, 
                                                                                                                                                  x = 0.00255102040816328, x = -0.0241496598639456, x = -0.0241496598639456, 
                                                                                                                                                  x = -0.0241496598639456, x = -0.0241496598639456, x = -0.0241496598639456, 
                                                                                                                                                  x = -0.0241496598639456, x = -0.00128778784240969, x = -0.00128778784240969, 
                                                                                                                                                  x = -0.00128778784240969, x = -0.00128778784240969, x = -0.00128778784240969, 
                                                                                                                                                  x = -0.00128778784240969, x = -0.00128778784240969, x = -0.00128778784240969, 
                                                                                                                                                  x = -0.00128778784240969, x = -0.00128778784240969, x = 0.00430547713156409, 
                                                                                                                                                  x = 0.00430547713156409, x = 0.00430547713156409, x = 0.00430547713156409, 
                                                                                                                                                  x = 0.00430547713156409, x = 0.00430547713156409, x = 0.00430547713156409, 
                                                                                                                                                  x = 0.00430547713156409, x = 0.00430547713156409, x = 0.00430547713156409, 
                                                                                                                                                  x = 0.00175070028011204, x = 0.00175070028011204, x = 0.00175070028011204, 
                                                                                                                                                  x = 0.00175070028011204, x = 0.00175070028011204, x = 0.00175070028011204, 
                                                                                                                                                  x = 0.00175070028011204, x = 0.00175070028011204, x = 0.00175070028011204, 
                                                                                                                                                  x = 0.00175070028011204, x = -0.00255102040816326, x = -0.00255102040816326, 
                                                                                                                                                  x = -0.00255102040816326, x = -0.00255102040816326, x = -0.00255102040816326, 
                                                                                                                                                  x = -0.00255102040816326, x = -0.00255102040816326, x = -0.00255102040816326, 
                                                                                                                                                  x = 0.00928571428571428, x = 0.00928571428571428, x = 0.00928571428571428, 
                                                                                                                                                  x = 0.00928571428571428, x = 0.00928571428571428, x = 0.00928571428571428, 
                                                                                                                                                  x = 0.00928571428571428, x = 0.00928571428571428, x = 0.00928571428571428, 
                                                                                                                                                  x = 0.00928571428571428)), row.names = c(NA, -117L), class = c("tbl_df", 
                                                                                                                                                                                                                 "tbl", "data.frame"))
Chris Ruehlemann
  • 20,321
  • 4
  • 12
  • 34
  • 1
    Is this link any use: https://stackoverflow.com/questions/11889625/annotating-text-on-individual-facet-in-ggplot2 – Peter Jul 07 '23 at 09:29

1 Answers1

1

You could create a column with text of your values using paste0. After that you can assign this to the aes of your geom_text with label and use x=-Inf, y=Inf with hjust and vjust in the aesthetic to have your text in top left corner like this:

library(ggplot2)
library(dplyr)

df %>%
  mutate(text = paste0("Mean derivative = ",round(Mean_deriv,3),"\n","Slope = ",round(Slope,3))) %>%
  ggplot(aes(x = x, y = IND_all, colour = Story))+
  facet_wrap(. ~ Story, scales = 'free_x') +
  geom_line(alpha = 0.5)+
  geom_point(alpha = 0.6)+
  geom_smooth(method = "lm", formula = y ~ poly(x, 2), se = FALSE, #colour = "grey60", 
              linetype = 1, lwd = 1)+
  geom_smooth(method = "lm", linetype = 2, lwd = 1, se = FALSE)+
  geom_text(aes(label = text, x = -Inf, y = Inf, hjust = 0, vjust = 1), size = 3) +
  theme(legend.position = "none")
#> `geom_smooth()` using formula = 'y ~ x'

Created on 2023-07-07 with reprex v2.0.2

Quinten
  • 35,235
  • 5
  • 20
  • 53