1

I am wondering why the text is trending higher in the plots... it won't stay put with the facet_wrap or facet_grid. In a more complex dataset plot, the text is illegible because of the overlap.

Below is data and code to reproduce the plot and issue. Adding geom="text" to stat_fit_glance, results in Error: Discrete value supplied to continuous scale . enter image description here

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)
Pedro J. Aphalo
  • 5,796
  • 1
  • 22
  • 23
ecology
  • 606
  • 3
  • 9
  • 29
  • 1
    What is `formula1` ? I can't reproduce your plot due to the absence of formula1 on geom_smooth. – dc37 Nov 21 '19 at 16:43
  • 1
    added it in an edit! thanks for pointing that out – ecology Nov 21 '19 at 17:49
  • 1
    @FISHnR The code above triggered an error and three warnings unrelated to the question. The name of the package is 'ggplot2'. "name" is not the name of a ggplot aesthetics and was being ignored. I edited the code example above. – Pedro J. Aphalo Nov 21 '19 at 18:57

1 Answers1

4

When the position of the labels is set automatically, the npcy position is increased for each level in the grouping variable. You map Site to the group aesthetic, as Site has 5 levels unevenly appearing in different facets, the rather crude algorithm in 'ggpmisc' positions the labels unevenly: the five rows correspond one to each of the five Sites. I have changed the mapping to use colour so that this becomes more obvious. I have also deleted all code that is irrelevant to this question.

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)

enter image description here

To use fixed positions one can pass the npcy coordinates if using the default "geom_text_npcy()" or passing data coordinates and using "geom_text()". One position corresponds to each level of the grouping factor Site. If the vector is shorter, it is recycled. Of course to fit more labels you can reduce the size of the text and add space by expanding the plotting area. In any case, in practice, you will need to indicate in a way or another which estimates correspond to which line.

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)

enter image description here

Note: Error: Discrete value supplied to continuous scale when attempting to use geom_text() is a bug in 'ggpmisc' that I fixed some days ago, but has not made it yet to CRAN (future version 0.3.3).

Pedro J. Aphalo
  • 5,796
  • 1
  • 22
  • 23