rggplot2ggstatsplot

ggbetweenstats: logarithmic y axis removes grouped analysis from plot


I am conducting a kruskal-wallis test to determine statistically significance between three groups of a measurement. I use ggbetweenstats to determine between which group there is a statistically significant association.

Here is the code for sample data and the plot:

sampledata <- structure(list(ID = c(1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 
                                    13, 14, 15, 16, 17, 18, 19, 20), group = c(1, 2, 3, 1, 2, 3, 
                                                                               1, 2, 3, 1, 2, 3, 1, 2, 3, 1, 2, 3, 1, 2), measurement = c(0, 
                                                                                                                                          1, 200, 30, 1000, 6000, 1, 0, 0, 10000, 20000, 700, 65, 1, 8, 
                                                                                                                                          11000, 13000, 7000, 500, 3000)), class = "data.frame", row.names = c(NA, 
                                                                                                                                                                                                               20L))
library(ggstatsplot)
library(ggplot2)

ggbetweenstats(
 data = sampledata,
 x = group,
 y = measurement,
 type = "nonparametric",
 plot.type = "box",
 pairwise.comparisons = TRUE,
 pairwise.display = "all",
 centrality.plotting = FALSE,
 bf.message = FALSE
) 

You can see the results from the kruskal wallis test on the top of the plot as well as the groupes analysis in the plot. Now I want to change y axis to logarithmic scale:

ggbetweenstats(
  data = sampledata,
  x = group,
  y = measurement,
  type = "nonparametric",
  plot.type = "box",
  pairwise.comparisons = TRUE,
  pairwise.display = "all",
  centrality.plotting = FALSE,
  bf.message = FALSE
) +
  ggplot2::scale_y_continuous(trans=scales::pseudo_log_trans(sigma = 1, base = exp(1)), limits = c(0,25000), breaks = c(0,1,10,100,1000,10000)
)

However, this removes the grouped analysis. I have tried different scaling solutions and browsed SO for a solution but couldn't find anything. Thank you for your help!


Solution

  • It seems that the y_position parameter in the geom_signif component is not affected by the y axis transformation. You will need to pass the log values of the desired bracket heights manually. In theory, you can pass these via the ggsignif.args parameter, but it seems that in the latest version of ggstatsplot this isn't possible because the y_position is hard-coded.

    One way tound this is to store the plot then change the y positions after the fact. Here's a full reprex with the latest versions of ggplot2, ggstatsplot and their dependencies (at the time of writing)

    sampledata <- structure(list(ID = c(1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 
    13, 14, 15, 16, 17, 18, 19, 20), group = c(1, 2, 3, 1, 2, 3, 
    1, 2, 3, 1, 2, 3, 1, 2, 3, 1, 2, 3, 1, 2), measurement = c(0, 
    1, 200, 30, 1000, 6000, 1, 0, 0, 10000, 20000, 700, 65, 1, 8, 
    11000, 13000, 7000, 500, 3000)), class = "data.frame", row.names = c(NA, 
    20L))
    
    library(ggstatsplot)
    library(ggplot2)
    library(scales)
    
    p <- ggbetweenstats(
      data = sampledata,
      x = group,
      y = measurement,
      type = "nonparametric",
      plot.type = "box",
      pairwise.comparisons = TRUE,
      pairwise.display = "all",
      centrality.plotting = FALSE,
      bf.message = FALSE
    ) + scale_y_continuous(trans = pseudo_log_trans(sigma = 1, base = exp(1)),
                            limits = c(0, exp(13)), 
                            breaks = c(0, 10^(0:5)),
                            labels = comma)
    #> Scale for y is already present.
    #> Adding another scale for y, which will replace the existing scale.
    
    i <- which(sapply(p$layers, function(x) inherits(x$geom, "GeomSignif")))
    p$layers[[i]]$stat_params$y_position <-  c(10, 10.8, 11.6)
    
    p
    

    Created on 2023-01-15 with reprex v2.0.2