1

我有一个构面图,必须调整每个单独的 r 方标签很痛苦,这包括将它们从左到右、上下、大小移动。一个可能比另一个大或隐藏在图表的底部,而其他的则可见且略高于。这是一种痛苦,在视觉上看起来很可怕。我希望等式在图表上方,如下所示:

在此处输入图像描述

可重现代码示例:

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"))

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

我已经尝试过label.xlabel.y但他们只在图表中放置标签。

4

1 回答 1

1

恐怕代码示例和数据都不是最小的示例,示例也不是完全可重现的。请提供示例中使用的数据框,并删除绘图中与问题无关的所有组件。ggplot stats 不会将任何图形对象添加到绘图中。所以相关问题减少了我如何geom_text()在绘图区域之外添加标签。答案将是:设置clip = "off"coord_cartesian(). 但一般不建议这样做。如果您希望方程作为绘图区域之外的标题,则 astat不是正确使用的工具。如果将方程放在绘图区域内是可以接受的,那么您可以扩大y比例以为其腾出空间。

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

于 2019-12-10T19:25:50.940 回答