rargumentsparameter-passingpass-by-referenceggstatsplot

Pass list of arguments to ggstatsplot function


I am using ggstatsplot to make lots of similar plots but the exact data or variable plotted will be different between each plot. I want to assign most of the (constant) arguments to a variable that I can call for each plot. This way, if I want to update an argument for all the plots, I only need to change it at one place (i.e. trying not to repeat myself).

According to this StackOverflow solution, I tried two ways of doing this.

First try: [Edited to add list() inside of c(), which gave the same error as Try 2]

    Def_Args2 <- list(
  pairwise.display = "significant",
  plot.type = "box",
  results.subtitle = FALSE, # Remove freq stat subtitle
  violin.args = list(alpha = 0), # Remove violin
  point.args = list(alpha = 0), # Remove points (unless can do beeswarm here?)
  boxplot.args = list(notch = TRUE,
                      width = 0.4,
                      alpha = 0.5,
                      aes(fill = Treatment)
                      ),
  pairwise.comparisons = TRUE,
  centrality.plotting = FALSE, # Don't plot mean
  bf.message = FALSE # Remove Bayes stat subtitle
)

do.call(ggbetweenstats,
   c(
     list(
       data = FluorByTreatment_Fltr_Iter3.tdf,
       x = FluorByTreatment_Fltr_Iter3.tdf$Treatment,
       y = FluorByTreatment_Fltr_Iter3.tdf$Area,
       type = "parametric", # ANOVA or Kruskal-Wallis
       var.equal = FALSE), # ANOVA or Welch ANOVA
     Def_Args2
     )
   )

Second try:

    Def_Args1 <- list(
  data = FluorByTreatment_Fltr_Iter3.tdf,
  x = FluorByTreatment_Fltr_Iter3.tdf$Treatment,
  y = FluorByTreatment_Fltr_Iter3.tdf$Area,
  type = "parametric", # ANOVA or Kruskal-Wallis
  var.equal = FALSE, # ANOVA or Welch ANOVA
  pairwise.display = "significant",
  plot.type = "box",
  results.subtitle = FALSE, # Remove freq stat subtitle
  violin.args = list(alpha = 0), # Remove violin
  point.args = list(alpha = 0), # Remove points (unless can do beeswarm here?)
  boxplot.args = list(notch = TRUE,
                      width = 0.4,
                      alpha = 0.5,
                      aes(fill = Treatment)
                      ),
  pairwise.comparisons = TRUE,
  centrality.plotting = FALSE, # Don't plot mean
  bf.message = FALSE # Remove Bayes stat subtitle
)

exec(ggbetweenstats, !!!Def_Args1)

Both give the error:

Error in ensym(x)

My guess is that some of the arguments aren't compatible with these input methods. Is there another input method I could try? Or should I abandon hope? Thank you!

Crossposted from Reddit


Solution

  • You can write a custom function around ggbetweestats() that accepts arguments for those ggbetweenstats()-arguments that change from plot to plot (data, x, y, type, var.equal). The other arguments can be hard-coded into the function. You can modify the function code when you want to change argument values for all plots. If you later notice that you want to vary some of the hard-coded arguments, you can add these as arguments to the custom function.

    In the example below I added pairwise.display as an argument with a default value “significant”. That means if you don’t specify that argument, it will default to “significant,” but if you want to change it for some plots, you can specify another value in the function call (see 2nd custom_ggbs() call in the example.

    Since you didn’t share your data, my example below uses the gapminder dataset from the eponymous package.

    library(ggstatsplot)
    library(gapminder)
    
    custom_ggbs <- 
      function(data, 
               x, 
               y, 
               type, 
               var.equal, 
               pairwise.display = "significant") ggbetweenstats(
                  data = data,
                  x = {{x}},
                  y = {{y}},
                  type = type,
                  var.equal = var.equal,
                  pairwise.display = "significant",
                  plot.type = "box",
                  results.subtitle = FALSE, 
                  violin.args = list(alpha = 0), 
                  point.args = list(alpha = 0), 
                  pairwise.comparisons = TRUE,
                  centrality.plotting = FALSE,
                  bf.message = FALSE 
                )
    
    custom_ggbs(data = gapminder,
                x = continent,
                y = lifeExp,
                type = "parametric",
                var.equal = FALSE)
    

    
    custom_ggbs(data = gapminder,
                x = continent,
                y = gdpPercap,
                type = "parametric",
                var.equal = FALSE,
                pairwise.display = "all")