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.
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:
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!