2

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

Pedro J. Aphalo
  • 5,796
  • 1
  • 22
  • 23
  • I think it would be easier if you could add a small example of your dataset so that people could try reproduce your problem : https://stackoverflow.com/questions/5963269/how-to-make-a-great-r-reproducible-example – Basti Aug 31 '21 at 13:12
  • I think for the time being you will need to compute the R^2 and p-values before plotting and encode with a factor in your data which panels you want with data or lines in grey. On the other hand automating this is a good idea that I will probably implement in package 'ggpmisc' in a future version. – Pedro J. Aphalo Sep 02 '21 at 23:23
  • @Pedro, Should we also give the option to make a Bonferroni correction given we are doing multiple comparisons? – Mark Neal Sep 02 '21 at 23:55
  • 1
    @MarkNeal Good point. I think it would be enough to allow users to set the target value for significance as then the users can apply the Bonferroni correction to it instead of to the computed estimates. At least, at first I would like not to make the code too complex. – Pedro J. Aphalo Sep 03 '21 at 08:07

1 Answers1

1

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)

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