Lime
Lime

Reputation: 756

How to place stat_poly_eq labels outside of facet graph?

I have a facet graph and it's a pain having to tweak each individual r-square label, this includes moving them left to right, up and down, big and small. One may be larger than the other or hidden away at the bottom of the graph whilst the others are visible and slightly above. It's a pain and looks horrible visually. I want for the equation to be above the graph, like so:

enter image description here

sample of reproducible code:

structure(list(Species = structure(c(1L, 1L, 1L, 1L, 2L, 2L, 
2L, 2L, 3L, 3L, 3L), .Label = c("Blackbird", "Blue Tit", "Bullfinch"
), class = "factor"), Year = c(1994L, 1998L, 2004L, 2009L, 1994L, 
1999L, 2005L, 2010L, 1995L, 2000L, 2006L), Population = c(1.91022169582369, 
1.76053109777675, 1.97132885598781, 1.94651234008203, 1.43940573527339, 
1.64546504124046, 1.75291614453774, 1.70743198528975, 0.467706836356934, 
0.425310951188478, 0.427227566136717), sd.value = c(2.10254751340886, 
2.28211160643506, 2.11164731199098, 2.04945233591971, 2.34575079367241, 
2.46584590480183, 2.14306252633967, 2.24067809521947, 1.56970929050149, 
1.62034304778156, 1.64948219178796), count = c(99L, 146L, 138L, 
161L, 88L, 142L, 155L, 159L, 21L, 25L, 33L), se.mean = c(0.211313975935513, 
0.188868900393346, 0.179755356499148, 0.161519471302936, 0.25005787480135, 
0.206929191574193, 0.172134924420264, 0.177697331649771, 0.342538649657072, 
0.324068609556313, 0.287137993487115)), row.names = c(NA, -11L
), groups = structure(list(Species = structure(c(1L, 3L, 4L), .Label = c("Blackbird", 
"Blackcap", "Blue Tit", "Bullfinch", "Buzzard", "Canada Goose", 
"Carrion Crow", "Chaffinch", "Chiffchaff", "Coal Tit", "Collared Dove", 
"Coot", "Corn Bunting", "Cuckoo", "Dunnock", "Feral Pigeon", 
"Garden Warbler", "Goldcrest", "Goldfinch", "Great Spotted Woodpecker", 
"Great Tit", "Green Woodpecker", "Greenfinch", "Grey Partridge", 
"Greylag Goose", "House Martin", "House Sparrow", "Jackdaw", 
"Jay", "Kestrel", "Lapwing", "Lesser Whitethroat", "Linnet", 
"Long-tailed Tit", "Magpie", "Mallard", "Meadow Pipit", "Mistle Thrush", 
"Moorhen", "Mute Swan", "Oystercatcher", "Pheasant", "Red-legged Partridge", 
"Reed Bunting", "Reed Warbler", "Robin", "Rook", "Sedge Warbler", 
"Shelduck", "Skylark", "Song Thrush", "Sparrowhawk", "Starling", 
"Stock Dove", "Swallow", "Swift", "Turtle Dove", "Whitethroat", 
"Willow Warbler", "Woodpigeon", "Wren", "Yellow Wagtail", "Yellowhammer"
), class = "factor"), .rows = list(1:4, 5:8, 9:11)), row.names = c(NA, 
-3L), class = c("tbl_df", "tbl", "data.frame"), .drop = TRUE), class = c("grouped_df", 
"tbl_df", "tbl", "data.frame"))

A small view into the original data.frame:

         Species Year Population sd.value count   se.mean
1      Blackbird 1994  1.9102217 2.102548    99 0.2113140
2      Blackbird 1995  1.8781008 2.172558   116 0.2017169
3      Blackbird 1996  1.8566768 2.159796   125 0.1931780
4      Blackbird 1997  1.6561201 2.115925   146 0.1751152
5      Blackbird 1998  1.7605311 2.282112   146 0.1888689
6      Blackbird 1999  1.9721596 2.160936   150 0.1764397
7      Blackbird 2000  2.0197406 2.189046   124 0.1965821
8      Blackbird 2002  2.1294262 2.147883   131 0.1876614
9      Blackbird 2003  2.1036905 2.137380   122 0.1935093
10     Blackbird 2004  1.9713289 2.111647   138 0.1797554
11     Blackbird 2005  2.0354752 2.029201   167 0.1570243
12     Blackbird 2006  1.9782186 2.034861   174 0.1542624
13     Blackbird 2007  1.9602156 2.133097   218 0.1444716
14     Blackbird 2008  2.0192111 2.199758   164 0.1717722
15     Blackbird 2009  1.9465123 2.049452   161 0.1615195
16     Blackbird 2010  1.9716616 1.980645   163 0.1551361
17     Blackbird 2011  1.9516421 2.076481   159 0.1646756
18     Blackbird 2012  1.9300603 2.057271   172 0.1568655
19     Blackbird 2013  1.8760379 2.020385   179 0.1510106

plot code:

library(ggplot2)
library(ggrepel)
library(ggpmisc)
my.formula <- y ~ x
ggplot(G_frame, aes(Year, Population, group = Species)) +
  geom_line() +
  geom_smooth(method="lm", se=F, formula=my.formula, level = 0.95) +
  stat_poly_eq(aes(label = paste(..eq.label.., ..rr.label.., sep = "~~~")),
               formula = my.formula, parse = TRUE, 
               label.x = c(rep('right', 3), 'left'),
               label.y = c(rep(-0.9, 2), 0.9, rep(-0.9, 5)) * 
                            G_frame$Population, size = 2.7) +
  facet_wrap(~Species, scales = "free_y") + 
  geom_errorbar(aes(ymin = Population - se.mean, ymax = Population + se.mean),
                size = 0.5, width=0.5) +  
  theme_bw() +  
  theme(panel.grid.major = element_blank(),
        panel.grid.minor = element_blank(),
        strip.background = element_blank(),
        panel.border = element_rect(colour = "black"))

I have tried label.x and label.y but they only position the labels within the graph.

Upvotes: 1

Views: 709

Answers (1)

Pedro J. Aphalo
Pedro J. Aphalo

Reputation: 6528

I am afraid the code example and data are not a minimal example, neither the example is fully reproducible. Please, provide a data frame as used in the example, and remove all components of your plot that are irrelevant to the question. ggplot stats do not add any graphic object to the plot. So the relevant question reduces how do I make geom_text() add labels outside the plotting area. Answer would then be: set clip = "off" in coord_cartesian(). But in general this is not recommended. If you want the equations as a title outside the plotting area, then a stat is not the right tool to use. If putting the equation within the plotting area is acceptable, then you can expand the y scale to make space for it.

(This answer is provisional, as the question and the example, I think, need to be reformulated.)

Upvotes: 2

Related Questions