0

The data that I am working with is as follows:

      "low_rd"

id  source      x   y
-----------------------
id1 source a    1   1
id1 source a    2   2
id1 source a    3   3
id1 source x    1   4
id1 source x    2   5
id1 source x    3   6
id2 source x    1   1
id2 source x    2   2
id2 source a    3   3
id2 source x    4   7
id2 source y    5   8
id2 source z    6   9
id3 source y    1   1
id3 source y    2   2
id3 source y    3   3
id3 source y    4   7
id3 source z    5   8
id3 source z    6   9
id4 source a    1   1
id4 source x    2   2
id4 source x    3   3
id4 source a    4   7
id4 source y    5   8
id4 source y    6   9
id5 source y    1   1
id5 source z    2   2
id5 source x    3   3
id5 source y    4   7
id5 source z    5   8
id5 source y    6   9

The plots that I get when run the code below, on the data above,

ggplot(low_rd,aes(x = x, y = y)) +
  geom_point(aes(colour = factor(source)), show.legend = FALSE) + 
  geom_smooth(method = 'lm', se = FALSE) + 
  geom_smooth(method = 'lm', aes(colour = factor(source)), se = FALSE, show.legend = FALSE) +
  facet_wrap(~id, scales = "free", shrink = FALSE)

is as follows:

enter image description here

The dark blue line in every plot is the overall regression line that I get from regressing all the data points for each "id". However, for each id, every data point is associated with a specific "source", and I plotted the slopes for each of the data points associated with each source as seen in the image.

What I want to now do is to display a legend (ideally to the top right corner), specific to each subplot, which displays all the sources associated with that particular id. Additionally, I am also trying to display the R2 and the overall slope value for each of the ids as well.

I would greatly appreciate any input as to how I can implement the aspects mentioned above into my code.

Cheers

AP_98
  • 29
  • 3
  • 1
    maybe this can help you: [https://stackoverflow.com/questions/52278623/ggplot2-how-to-add-unique-legend-for-multiple-plots-with-grid-arrange) – Kian May 21 '21 at 03:37
  • 1
    For the R2 you can look at the ```stat_cor()``` function from the ```ggpubr``` package. ```CODE SO FAR + stat_cor(aes(label = ..rr.label..))``` – maarvd May 21 '21 at 06:43

0 Answers0