Hillary Le
Hillary Le

Reputation: 115

R- Stat_compare_means does not fit on ggplot?

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")

Upvotes: 2

Views: 1275

Answers (2)

Hillary Le
Hillary Le

Reputation: 115

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

Upvotes: 2

jared_mamrot
jared_mamrot

Reputation: 26484

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?

Upvotes: 3

Related Questions