0

I searched for the solution as it sounds like a frequent question, but couldn't find the answer, so will ask it here. My example is using labels created by ggpubr statistical package, but the question is more general.

Let's say I have this dataset and I'm trying to plot it and mark t-test p-value to compare the groups like that:

library(ggplot2)
library(ggpubr)
seed(42)

data <- data.frame(category = c("green", "red", "blue", "yellow"), 
                  type = sample(LETTERS[1:4], 100, replace = T),
                  values = c(1, 2, 50, 5) * runif(100))

ggplot(data, aes(x = type, y = values, color = type)) +
 geom_point(size = 3) +
 stat_compare_means(method = "t.test", aes(label = ..p.value..), size = 5, 
                    comparisons = list(c("A" , "C"), c("A" , "D"))) +
 facet_wrap( ~ category, scales = "free_y")

That results in this kind of graph, where top p-values don't fit the plot. enter image description here

I'd like to actually fit those numbers in the plot in this exact size (without decreasing their size). I tried that by manipulating ylim parameter by expanding range of y-axis and adding some number to the maximum value in the dataset ylim(0, max(data$values)+5). The problem, that R considers that max value before facetting, so it's not facet-specific and results in what I mean to get:

enter image description here

Is there any way to manipulate and expand ylim individually for facets or any other way to fit in those labels ? Also, why facetting doesn't take max value only for the part of dataset it's working with and how to provide the values for a part of dataset for facetting in this case ?

Thank you!

Eugene
  • 85
  • 8
  • 2
    Likely related: https://stackoverflow.com/q/63550588/3358272. I cannot vouch for integration with `ggpubr`, though I suspect it won't impact it. – r2evans Jul 10 '23 at 19:19
  • 2
    You can add `... + scale_y_continuous(expand = c(.1, .1))`, thus adjusting the expansion of the axis beyond the data range (change expansion values to suit your plot). – I_O Jul 10 '23 at 19:52
  • Thanks a lot for the comments! scale_y_continuous works very well for me! – Eugene Jul 10 '23 at 19:59

1 Answers1

1

Here is as an alternative approach adding extra space to max value:

library(dplyr)
library(ggplot2)
library(ggpubr)

data %>%
  mutate(values_adj = ifelse(values == max(values), values + 10, values), .by = category) %>% 
  ggplot(aes(x = type, y = values_adj, color = type)) +
  geom_point(size = 3) +
  stat_compare_means(method = "t.test", aes(label = ..p.value.., y = values), size = 5, 
                     comparisons = list(c("A" , "C"), c("A" , "D"))) +
  facet_wrap( ~ category, scales = "free_y")

enter image description here

TarJae
  • 72,363
  • 6
  • 19
  • 66
  • Thank you for the answer! If I understand this code correctly it graphs modified (not original) values. And my goal is to graph exact same data, just to fit in p-values into the plot. ```...+scale_y_continuous(expand = c(.1, .1))``` suffices that goal perfectly, so I'll use that solution. Thank you for the answer! – Eugene Jul 11 '23 at 15:46