So, I am calculating a statistic between two values, one random and the other one "real" scores. As it is sort of paired, I plot the distributions of the differences for different conditions and states with facet_grid but I am also interested in the marginal (global distribution, i.e. margin = T). Now I add the confidence level for the t-test (does it make sense btw?) as they are normal distributed for each condition. Nonetheless, I do not want the horizontal bars crammed in the marginal distribution (all). Is it possible? It would be actually nice to add only one horizontal bar to indicate the confidence limit for the global distribution.

The code to produce the problem is in the following, along with the output plot:

library(tidyverse)
library(rstatix)
main_tb <- tibble("scores" = rnorm(300, 0.6, 0.1), 
                  "rnd_scores" = rnorm(300, 0.4, 0.1), 
                  "condition" = sample(c("A", "B", "C"), 300, replace = T), 
                  "state" = sample(letters[5:10], 300, replace = T)) %>% 
  mutate(diff = scores - rnd_scores) %>% 
  print(n = 5)

conf_tb <- main_tb %>% 
  select(-diff) %>% 
  pivot_longer(names_to = "type", values_to = "fin_scores", c(-condition, -state)) %>% 
  group_by(condition) %>% 
  rstatix::t_test(fin_scores ~ type, detailed = T) %>% 
  rstatix::add_significance() %>% print(n = 5)

ggplot(main_tb, aes(y = diff, x = state)) + 
  geom_boxplot() + 
  geom_hline(data = conf_tb, aes(yintercept = -conf.low)) +
  facet_grid(~condition, margins = T) +
  theme_classic()

plot

0

There are 0 best solutions below