Warning: file_get_contents(/data/phpspider/zhask/data//catemap/4/r/69.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
R 如何将stat\u poly\u eq标签放置在刻面图之外?_R_Ggplot2_Ggpmisc - Fatal编程技术网

R 如何将stat\u poly\u eq标签放置在刻面图之外?

R 如何将stat\u poly\u eq标签放置在刻面图之外?,r,ggplot2,ggpmisc,R,Ggplot2,Ggpmisc,我有一个刻面图,调整每个单独的r-square标签是一件痛苦的事情,这包括从左到右、上到下、大到小的移动。一个可能比另一个大,或者隐藏在图表的底部,而另一个是可见的,略高于此。这是一种痛苦,视觉上看起来很可怕。我希望方程在图的上方,如下所示: 可复制代码样本: structure(list(Species = structure(c(1L, 1L, 1L, 1L, 2L, 2L, 2L, 2L, 3L, 3L, 3L), .Label = c("Blackbird", "Blue Tit",

我有一个刻面图,调整每个单独的r-square标签是一件痛苦的事情,这包括从左到右、上到下、大到小的移动。一个可能比另一个大,或者隐藏在图表的底部,而另一个是可见的,略高于此。这是一种痛苦,视觉上看起来很可怕。我希望方程在图的上方,如下所示:

可复制代码样本:

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"))
原始
数据帧的小视图

         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
绘图代码:

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"))
库(ggplot2)
图书馆(ggrepel)
图书馆(ggpmisc)

my.formula恐怕代码示例和数据不是最小示例,示例也不是完全可复制的。请提供示例中使用的数据框,并删除与问题无关的所有绘图部分。ggplot统计不会向绘图添加任何图形对象。因此,相关问题减少了如何在绘图区域之外添加标签。答案是:在
coord\u cartesian()
中设置
clip=“off”
。但通常不建议这样做。如果希望在绘图区域外将方程式作为标题,则
stat
不是合适的工具。如果可以将方程式放在绘图区域内,则可以展开y比例以为其留出空间

(这个答案是暂时的,因为我认为问题和例子需要重新表述。)