facet_wrap stat_fit_glance 的文本标签问题

facet_wrap text labelling issues with stat_fit_glance

我想知道为什么文本在图中的趋势更高...它不会与 facet_wrap 或 facet_grid 保持一致。在更复杂的数据集图中,文本由于重叠而难以辨认。

以下是重现情节和问题的数据和代码。将 geom="text" 添加到 stat_fit_glance,结果是 Error: Discrete value supplied to continuous scale

library(ggpmisc)
library(ggplot2)

DF <- data.frame(Site = rep(LETTERS[20:24], each = 4),
                 Region = rep(LETTERS[14:18], each = 4),
                 time = rep(LETTERS[1:10], each = 10),
                 group = rep(LETTERS[1:4], each = 10),
                 value1 = runif(n = 1000, min = 10, max = 15),
                 value2 = runif(n = 1000, min = 100, max = 150))
DF$time <- as.numeric(DF$time)
formula1 <- y~x
plot1 <- ggplot(data=DF, 
                aes(x=time, y= value2,group=Site)) +
  geom_point(col="gray", alpha=0.5) +
  geom_line(aes(group=Site),col="gray", alpha=0.5) +
  geom_smooth(se=F, col="darkorange", alpha=0.8, fill="orange",
              method="lm",formula=formula1) +
  theme_bw() + 
  theme(strip.text.x = element_text(size=10),
        strip.text.y = element_text(size=10, face="bold", angle=0),
        strip.background = element_rect(colour="black", fill="gray90"),
        axis.text.x = element_text(size=10),  # remove x-axis text
        axis.text.y = element_text(size=10), # remove y-axis text
        axis.ticks = element_blank(),  # remove axis ticks
        axis.title.x = element_text(size=18), # remove x-axis labels
        axis.title.y = element_text(size=25), # remove y-axis labels
        panel.background = element_blank(), 
        panel.grid.major = element_blank(),  #remove major-grid labels
        panel.grid.minor = element_blank(),  #remove minor-grid labels
        plot.background = element_blank()) + 
  labs(y="", x="Year", title = "")+ facet_wrap(~group)

plot1 + stat_fit_glance(method = "lm", label.x="right", label.y="bottom",
                        method.args = list(formula = formula1),
                        aes(label = sprintf('R^2~"="~%.3f~~italic(p)~"="~%.2f',
                                            stat(..r.squared..),stat(..p.value..))),
                        parse = TRUE)

当自动设置标签的位置时,npcy 位置会针对分组变量中的每个级别增加。您将 Site 映射到 group 审美,因为 Site 有 5 个级别不均匀地出现在不同的方面,'ggpmisc' 中相当粗糙的算法定位标签不均匀:五行对应一个到五个站点中的每一个。我已将映射更改为使用颜色,以便它变得更加明显。我也删除了所有与这个问题无关的代码。

plot1 <- ggplot(data=DF, 
                aes(x=time, y= value2, color=Site)) +
  geom_smooth(se=F, alpha=0.8,
              method="lm",formula=formula1) +
  facet_wrap(~group)

plot1 + 
  stat_fit_glance(method = "lm", label.x="right", label.y="bottom",
                        method.args = list(formula = formula1),
                        aes(label = sprintf('R^2~"="~%.3f~~italic(p)~"="~%.2f',
                                            stat(..r.squared..),stat(..p.value..))),
                        parse = TRUE) +
  expand_limits(y = 110)

如果使用默认 "geom_text_npcy()" 或传递数据坐标并使用 "geom_text()",则可以传递 npcy 坐标以使用固定位置。一个位置对应分组因子的每一水平Site。如果向量较短,则将其回收。当然,为了容纳更多标签,您可以减小文本的大小并通过扩大绘图区域来添加 space。在任何情况下,在实践中,您都需要以某种方式指示哪些估计对应于哪一行。

plot1 + 
  stat_fit_glance(method = "lm", label.x="right", label.y= c(0.01, 0.06, 0.11, 0.01, 0.06),
                  method.args = list(formula = formula1),
                  aes(label = sprintf('R^2~"="~%.3f~~italic(p)~"="~%.2f',
                                      stat(..r.squared..),stat(..p.value..))),
                  parse = TRUE, size = 2.5) +
  expand_limits(y = 110)

注意:Error: Discrete value supplied to continuous scale 尝试使用 geom_text() 是我几天前修复的 'ggpmisc' 中的一个错误,但还没有进入 CRAN(未来版本 0.3.3)。