const-ae/ggsignif

Feature request - add more distance between comparisons and data

MPietzke opened this issue · 4 comments

When not showing summarised data (e.g. mean + SD) but the data points the brackets tend to overlap with the highest data point.

library(tibble)
library(ggplot2)
library(ggsignif)

dataset = tibble(
  "Cond"   = rep(c("A", "B", "C"), each = 5),
  "Rep"    = rep(1:5, 3),
  "Value"  = c(runif(5, 10, 12),  #A
               runif(5, 11, 14),  #B
               runif(5, 10, 13))  #C
  )

ggplot(dataset, 
       aes(x = Cond, y = Value, 
           colour = as.factor(Cond), fill = as.factor(Cond) )) + 
  geom_point(size = 5, width = 0.2, alpha = 0.7, stroke = 1.5, shape = 21) + 
  stat_summary(fun.min = mean, fun.max = mean, size = 1.5, geom='errorbar') + 
  theme_bw()  + scale_y_continuous(limits = c(0, 16)) +
  geom_signif(comparisons = list(c("A", "B"), c("B", "C")),
              step_increase = 0.2,
              colour = "black") + 
  theme(legend.position = "none")

image

It would be great to offer an option to increase the distance between the statistics and the data, e.g. adding something like "step_offset".
step_increase unfortunately only increases the distance between the brackets.

You can use the y_position argument to do this:

library(tibble)
library(ggplot2)
library(ggsignif)

dataset <- tibble(
  "Cond" = rep(c("A", "B", "C"), each = 5),
  "Rep" = rep(1:5, 3),
  "Value" = c(
    runif(5, 10, 12), # A
    runif(5, 11, 14), # B
    runif(5, 10, 13)
  ) # C
)

ggplot(
  dataset,
  aes(
    x = Cond, y = Value,
    colour = as.factor(Cond), fill = as.factor(Cond)
  )
) +
  geom_point(size = 5, width = 0.2, alpha = 0.7, stroke = 1.5, shape = 21) +
  stat_summary(fun.min = mean, fun.max = mean, size = 1.5, geom = "errorbar") +
  theme_bw() +
  scale_y_continuous(limits = c(0, 16)) +
  geom_signif(
    comparisons = list(c("A", "B"), c("B", "C")),
    step_increase = 0.2, 
    y_position = c(14.75, 15),
    colour = "black"
  ) +
  theme(legend.position = "none")
#> Warning in geom_point(size = 5, width = 0.2, alpha = 0.7, stroke = 1.5, :
#> Ignoring unknown parameters: `width`

Created on 2022-12-01 with reprex v2.0.2

Okay yes if I want to manually fix the position.
Will this work as well without manually adding the number, e.g. for generating multiple plots with different intensities?

For example, could I read the y_position value from the underlying data (extracting the max) or supply this as additional data?

Yes, the y_position argument can be supplied programmatically. But this is something you, as a user, will need to do, or you can use other ggplot2-extensions that do this for you.

E.g., {ggstatsplot} does so internally to always separate the raw data from the comparisons. You can check out the relevant code here.

image

Hmm, too bad!
I thought it shouldn't be too complicated. Obviously the information of the highest point is somehow extracted and used for the positioning of the brackets. So I thought it would be easy to add some value to the starting point as well, similar to the offset between the following brackets.

PS: I noticed when adding an additional geom_hline() this successfully shifts the first position up a bit (to where the line is), maybe I can use this as workaround.