Color ggscatter by R and P values

Viewed 474

I'm plotting a wrapped ggscatter like the image below. What I want is to color differently according to the R and P values. For example, when P is not significant, I want the plot gray; when P is significant is want the plot colored according the R value in a continue scale. The problem is I don't know how to get those values to make an if statement inside the ggscatter. Anyone can help me? Thank you!

Example of dataset:

conc   exposure   col
11.16  21294      0.139275104
11.16  18018      0.150012216
13.8   26208      0.067379679
18.1   29484      0.013190731  

Plot:

ggscatter(data, x = "exposure", y = col, add = "reg.line", conf.int = TRUE, color = cor.hilab[1], cor.coef = TRUE) +
    facet_wrap(~conc)+
    ylab("OD")+
    xlab("Exposure")

ggscatter

1 Answers

This is now implemented in package 'ggpmisc' in GitHub (future version 0.4.4), but not yet in version 0.4.3 available in CRAN. This does not do exactly what you asked as at the moment geom_poly_eq() does not return R as it is not meaningful for polynomials of higher order than 1. Keeping to the grammar of graphics paradigm allows easier customization at the cost of lengthier code than the all-in-one functions from package pubr. Each approach has its advantages and drawbacks. I show here three examples of different possible approaches using package 'ggpmisc' (together with 'ggplot2').

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

# First approach: faint lines for non-significant fits, with bands
ggplot(mpg, aes(displ, hwy)) +
  geom_point() +
  stat_poly_eq(aes(label = paste(after_stat(rr.label),
                                 after_stat(p.value.label),
                                 sep = "*\", \"*")),
               label.x = "right") +
  stat_poly_line(aes(colour = stage(after_scale = ifelse(p.value < 0.05, 
                                                         alpha(colour, 1), 
                                                         alpha(colour, 0.25)))),
                 se = TRUE,
                 mf.values = T) + 
  facet_wrap(~class, ncol = 2) +
  theme_bw()

# Second approach: faint lines for non-significant fits, no-bands
# colour mapped to class
ggplot(mpg, aes(displ, hwy)) +
  geom_point(aes(colour = class)) +
  stat_poly_eq(aes(label = paste(after_stat(rr.label),
                                 after_stat(p.value.label),
                                 sep = "*\", \"*")),
               label.x = "right") +
  stat_poly_line(aes(colour = stage(start = class,
                                    after_scale = ifelse(p.value < 0.05, 
                                                         alpha(colour, 1), 
                                                         alpha(colour, 0.25)))),
                 se = FALSE,
                 mf.values = T) + 
  facet_wrap(~class, ncol = 2) +
  theme_bw()
#> Warning: Failed to apply `after_scale()` modifications to legend

# Third approach: no bands or lines for non-significant fits
ggplot(mpg, aes(displ, hwy)) +
  geom_point(aes(colour = class)) +
  stat_poly_eq(aes(label = paste(after_stat(rr.label),
                                 after_stat(p.value.label),
                                 sep = "*\", \"*")),
                   label.x = "right") +
  stat_poly_line(aes(colour = stage(after_scale = ifelse(p.value < 0.05, 
                                                        colour, 
                                                        NA)),
                     fill = stage(after_scale = ifelse(p.value < 0.05, 
                                                       fill, 
                                                       NA))),
                 se = TRUE,
                 mf.values = T) + 
  facet_wrap(~class, ncol = 2) +
  theme_bw()
#> Warning: Duplicated aesthetics after name standardisation: NA

#> Warning: Failed to apply `after_scale()` modifications to legend

Created on 2021-09-07 by the reprex package (v2.0.1)

Related