相关文章推荐
博学的瀑布  ·  No module named ...·  2 月前    · 
近视的橙子  ·  Java Review - ...·  2 月前    · 
独立的日记本  ·  [Day 23] React ...·  1 年前    · 
发怒的楼房  ·  angular - Unexpected ...·  1 年前    · 
豁达的西装  ·  cornerstone - 掘金·  1 年前    · 
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

enter image description here 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")
                It's easier to help you if you include a simple reproducible example with sample input and desired output that can be used to test and verify possible solutions. Also include all the packages you are using so we know exactly where these non-base R functions are coming from.
– MrFlick
                Mar 9, 2022 at 0:08
                Excellent advice, I changed the code so it should work when put in R, let me know if you have any ideas on what to do.
– Hillary Le
                Mar 9, 2022 at 0:22

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.