Search code examples
rggplot2font-family

stat_compare_means(): Unable to change font family when using comparisons() argument?


I am following up on this question here Change font family of stat_compare_mean() output in ggplot. I am new here and didnt want to reply to the orginal question as i didnt know if it was the correct thing to do! Hope this is ok...

Below, I am also trying to use Times New Roman font in the stat_compare_means() function in ggplot. I am on a Windows computer so I am setting Family = "Serif". I want to use the comparisons argument so I have the brackets etc. making it consistent with some previous work. However, when using the comparisons argument, it seems to overule the family argument and I am unable to change the font of the p value (if i comment out the comparisons argument, I am able to change the font). I have also tried setting the font in theme(text = element_text(family = "serif")). This changes the font of everything to Times New Roman in the plot, apart from the stat_compare_means() output. Code that I used is below:

    ggboxplot(ToothGrowth, x = "supp", y = "len",
              color = "supp", palette = "jco",
              add = "jitter",
              facet.by = "dose", 
              short.panel.labs = FALSE) +
    stat_compare_means(comparisons = list(c("OJ", "VC")),
                       label = "p.format",
                       family = "serif",
                       size = 5)

Can anyone help please? Thank you.

SessionInfo: R version 4.3.1 (2023-06-16 ucrt) Platform: x86_64-w64-mingw32/x64 (64-bit) Running under: Windows 10 x64 (build 19045)


Solution

  • This is a bug (or at least an instance of unhandled user input) in stat_compare_means. It comes from the ... parameters not being passed to geom_signif when comparisons are used. We can demonstrate this by creating a local version of stat_compare_means which is 'fixed' so that the ... are passed when comparisons are used:

    fixed_call <- quote(ggsignif::geom_signif(comparisons = comparisons, 
                                              y_position = label.y, 
                          test = method, test.args = method.args, 
                          step_increase = step.increase, 
                          size = bracket.size, textsize = 5, color = color, 
                          map_signif_level = map_signif_level, 
                          tip_length = tip.length, data = data,
                          vjust = vjust, ...))
    
    scm <- as.list(ggpubr::stat_compare_means)
    scm[[26]][[2]][[3]][[13]] <- fixed_call
    stat_compare_means <- function(...) {
      ggp <- getNamespace('ggpubr')
      .method_info <- ggp$.method_info
      .add_item <- ggp$.add_item
      .is_p.signif_in_mapping <- ggp$.is_p.signif_in_mapping
      .is_empty <- ggp$.is_empty
      do.call(as.function(scm), list(...), quote = FALSE)
    }
    

    Now we get:

    ggboxplot(ToothGrowth, x = "supp", y = "len",
              color = "supp", palette = "jco",
              add = "jitter",
              facet.by = "dose", 
              short.panel.labs = FALSE) +
      stat_compare_means(comparisons = list(c("OJ", "VC")),
                         label = "p.format", family = 'serif')
    

    enter image description here

    I'm not sure whether this is just an oversight by the authors or whether there were too many parameter name clashes to safely include ... in this call, but it might be worth filing a bug report.