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

Time:04-28

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

CodePudding user response:

Yes, you can set colors with the ggtext package.

library(ggplot2)
library(ggpmisc)
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, parse = FALSE, geom = "richtext",
               label.x = 30, vjust = c(0,0.5),
               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)) 

enter image description here

  • Related