stat_poly_eq() red text for significant p-value in facetwrap

353 views Asked by At

I have a series of regressions wrapped into facet_wrap and I'm wondering whether it is possible to denote the text for significant pvals with red text or a "*" inside of the stat_poly_eq() [ggpmisc] function?

Here is my sample code

ggplot(my.data, aes(x=age, y=logRR))+
  geom_hline(yintercept=0, linetype="dashed", color = "gray", size = 0.5) +
  geom_ribbon(aes(ymax=eCI_95, ymin=eCI_5), group=1, alpha=0.2, fill = "#BC1605") +
  geom_point(shape=19, size=3, color = "#BC1605") +
  geom_smooth(method='lm', se = T, formula=y~x, linetype='solid', size=0.5) +
  stat_poly_eq(formula = y ~ x, 
               aes(label = paste(..rr.label.., ..p.value.label.., sep = "*`,`~")), 
               parse = TRUE,
               label.x.npc = "right",
               vstep = 0.05) + # sets vertical spacing
  scale_color_manual(values=c("#000000", "#A4A4A4"), drop = FALSE) +
  scale_linetype_manual(values=c("solid","twodash"), drop = FALSE) +
  labs(y=expression(bold(Log(Diversity[MPA]/Diversity[REF])))) +
  labs(colour = 'Difference', linetype = 'Slope') +
  facet_wrap(mlpa_region~group, scales="free_y", nrow=3)+
  scale_x_continuous(breaks = seq(from=-5, to=19, by=2))+
  theme_classic() 
2

There are 2 answers

1
Stéphane Laurent On BEST ANSWER

Yes, you can set colors with the ggtext package.

library(ggplot2)
library(ggpmisc)
#> Loading required package: ggpp
#> 
#> Attaching package: 'ggpp'
#> The following object is masked from 'package:ggplot2':
#> 
#>     annotate
library(ggtext)

set.seed(4321)
x <- 1:100
y <- (x + x^2 + x^3) + rnorm(length(x), mean = 0, sd = mean(x^3) / 4)
my.data <- data.frame(x = x, 
                      y = y,
                      group = c("A", "B"))

formula <- y ~ poly(x, 1, raw = TRUE)

ggplot(my.data, aes(x, y)) + 
  facet_wrap(~ group) + 
  geom_point() +
  geom_smooth(method = "lm", formula = formula) +
  stat_poly_eq(formula = formula, geom = "richtext",
               label.x = 30, label.y = 10e5,
               mapping = aes(
                 label = ifelse(after_stat(r.squared) > 0.81, 
                                paste0(
                                  "<span style='color:red'>", 
                                  after_stat(rr.label), 
                                  "</span>"
                                ),
                                paste0(
                                  "<span style='color:blue'>", 
                                  after_stat(rr.label), 
                                  "</span>"
                                )
                 ),
                 label.color = NA)) 

Created on 2022-06-02 by the reprex package (v2.0.1)

0
Pedro J. Aphalo On

You can also set colors without any additional package as long as you are not using a color scale for grouping data, as this answer relies on scale_color_identity().

library(ggplot2)
library(ggpmisc)
#> Loading required package: ggpp
#> 
#> Attaching package: 'ggpp'
#> The following object is masked from 'package:ggplot2':
#> 
#>     annotate

set.seed(4321)
x <- 1:100
y <- (x + x^2 + x^3) + rnorm(length(x), mean = 0, sd = mean(x^3) / 4)
my.data <- data.frame(x = x, 
                      y = y,
                      group = c("A", "B"))

formula <- y ~ poly(x, 1, raw = TRUE)

ggplot(my.data, aes(x, y)) + 
  facet_wrap(~ group) + 
  geom_point() +
  geom_smooth(method = "lm", formula = formula) +
  stat_poly_eq(formula = formula,
               label.x = "left",
               mapping = aes(
                 color = ifelse(after_stat(r.squared) > 0.81, 
                                "red", "black"
                                )
                 )) +
  scale_color_identity()

Created on 2022-05-29 by the reprex package (v2.0.1)