2

enter image description hereI 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")
Hillary Le
  • 103
  • 5
  • 1
    It's easier to help you if you include a simple [reproducible example](https://stackoverflow.com/questions/5963269/how-to-make-a-great-r-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 09 '22 at 00: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 09 '22 at 00:22
  • 1
    Check out [`ggplot2::expansion()`](https://ggplot2.tidyverse.org/reference/expansion.html) inside a scale to expand by a multiple. – Dan Adams Mar 09 '22 at 00:29

2 Answers2

3

Edit

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?

jared_mamrot
  • 22,354
  • 4
  • 21
  • 46
2

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)

enter image description here

Hillary Le
  • 103
  • 5