Collectives™ on Stack Overflow
Find centralized, trusted content and collaborate around the technologies you use most.
Learn more about Collectives
Teams
Q&A for work
Connect and share knowledge within a single location that is structured and easy to search.
Learn more about Teams
I am looking at biological data of guinea pig with 2 treatment groups (hifat or no hifat diet) and when I facet_wrap the boxplots and add the stat_compare_means (t.test) function, the p-value is cut off. When I remove the scales="free", it still cuts off the p-value. I used the function to move the wording but since all graphs have different scales a fixed value for instance at y=1 would force all the axes to be the same. Would love any guidance.
library(tidyverse)
library(ggforce)
library(ggpubr)
ex <- data.frame(hifat=rep(c('yes','no'),each=8),
treat=rep(rep(c('bmi','heart'),4),each=4),
value=rnorm(32) + rep(c(3,1,4,2),each=4))
ex %>%
ggplot(aes(x = hifat,
y = value)) +
geom_boxplot() +
geom_point() +
stat_compare_means(method = "t.test") +
facet_wrap(~ treat, scales = "free")
–
–
Thank you for editing your question to add an example dataset! Here is a potential solution:
library(tidyverse)
library(ggforce)
library(ggpubr)
ex <- data.frame(hifat=rep(c('yes','no'),each=8),
treat=rep(rep(c('bmi','heart'),4),each=4),
value=rnorm(32) + rep(c(3,1,4,2),each=4))
ex %>%
ggplot(aes(x = hifat,
y = value)) +
geom_boxplot() +
geom_point() +
stat_compare_means(method = "t.test",
position = position_nudge(y = 0.5)) +
facet_wrap(~ treat, scales = "free")
Created on 2022-03-09 by the reprex package (v2.0.1)
Original answer
I don't have your guinea pig data so I can't reproduce your problem, but here is a minimal reproducible example using the palmerpenguins dataset and 'nudging' the t-test values using position_nudge()
:
library(tidyverse)
library(palmerpenguins)
library(ggpubr)
penguins %>%
na.omit() %>%
ggplot(aes(x = sex,
y = flipper_length_mm)) +
geom_boxplot() +
geom_jitter(width = 0.2) +
stat_compare_means(method = "t.test") +
facet_wrap(~ island, scales = "free")
penguins %>%
na.omit() %>%
ggplot(aes(x = sex,
y = flipper_length_mm)) +
geom_boxplot() +
geom_jitter(width = 0.2) +
stat_compare_means(method = "t.test",
position = position_nudge(y = 2)) +
facet_wrap(~ island, scales = "free")
Created on 2022-03-09 by the reprex package (v2.0.1)
In your case, perhaps you want to nudge the values 'closer' to the values (e.g. position_nudge(y = -2)
)? Does that solve your problem?
Resolved! Thanks everyone who made suggestions, the answer that worked best for me was to expand the y axis by 30% of its max value to give the words room to fit. Additionally, learning the position_nudge function from Jared also worked.
ggplot(data = all_selected_long,
aes(x = hifat,
y = values)) +
geom_boxplot() +
geom_point() +
scale_y_continuous(expand = expansion(mult = .3)) +
stat_compare_means(method = "t.test") +
facet_wrap_paginate(~ measurement, scale = "free", ncol = 3, nrow = 3, page = 1)
Thanks for contributing an answer to Stack Overflow!
- Please be sure to answer the question. Provide details and share your research!
But avoid …
- Asking for help, clarification, or responding to other answers.
- Making statements based on opinion; back them up with references or personal experience.
To learn more, see our tips on writing great answers.