How to adjust the horizontal width between boxplots inside one facet_wrap() panel ?

Hi,

I refer to this topic as is very similat to mine.
https://stackoverflow.com/questions/52341385/how-to-automatically-adjust-the-width-of-each-facet-for-facet-wrap

My question is how to sepatare those boxplots, so gap between them would be wider:

Inside the box of boxplot there is median I think, so if it had a label, that would be overlapped by neighboring median or box and not clearly visible. I tried with position dodge a bit, but even if I got it to work, medians' labels still stayed where they were initially so messy and not correct positioned.
How to make those medians' labels to follow medians correctly ?

Any help will be much appreciated.

I think you can get what you want by adjusting some combination of the width of the boxes and the width of position_dodge(). Compare these three plots.

DF <- data.frame(X =rep(c("A","B"), each = 50),
                 G = rep(c("W","X","Y","Z"), each = 25),
                 Val = c(rnorm(50), rnorm(50, 1.4)))
library(ggplot2)
ggplot(DF, aes(X, Val, fill = G)) + 
  geom_boxplot()


ggplot(DF, aes(X, Val, fill = G)) + 
  geom_boxplot(width = 0.2)


ggplot(DF, aes(X, Val, fill = G)) + 
  geom_boxplot(width = 0.2, position = position_dodge(width = 0.7))

Created on 2024-01-14 with reprex v2.0.2

Thank you very much indeed, it helps.

I have got 4 different variants here below:

I would like to deal separately with those numbers depends on boxplot labels for mean and median. They must be place differently in order to provide clarity for the reader. So in case 1 I want mean label value 4.9 place a bit higher above mean line and to the right and median 4.1 value to the right as well, in case 2, I want the same, in case 3 I want median 8.7 value place to the right beside boxplot beside median line respectively, and mean 8.4 value a bit down under red mean line and to the right a bit, in case 4 I want mean 2.5 value move down a bit under a mean line and to the right and median 2.5 value to the right a bit horizontally.
Is it doable ?

My desired output (meaning placing labels in a custom way for each boxplot) would be:

I have just prepared a reprex:

library(plyr)
library(tidyverse)

datas <- structure(list(Pain = structure(c(2L, 1L, 1L, 1L, 2L, 2L, 1L,
2L, 2L, 2L, 1L, 2L, 2L, 2L, 2L, 2L, 1L, 1L, 1L, 2L, 1L, 2L, 1L,
1L, 1L, 2L, 2L, 1L, 1L, 2L, 1L, 1L, 1L, 2L, 2L, 2L, 1L, 1L, 1L,
1L, 1L, 1L, 2L, 2L, 1L, 1L, 1L, 1L, 2L, 2L, 1L, 2L, 2L, 1L, 1L,
1L, 1L, 2L), levels = c("No", "Yes"), class = "factor"), Sex = structure(c(1L,
1L, 2L, 1L, 1L, 1L, 2L, 1L, 2L, 1L, 1L, 1L, 2L, 2L, 1L, 1L, 2L,
1L, 2L, 1L, 1L, 1L, 1L, 2L, 1L, 1L, 2L, 1L, 1L, 1L, 1L, 1L, 1L,
1L, 1L, 2L, 1L, 1L, 1L, 1L, 1L, 2L, 2L, 1L, 1L, 1L, 1L, 1L, 1L,
1L, 1L, 2L, 2L, 1L, 1L, 2L, 1L, 1L), levels = c("Female", "Male"
), class = "factor"), `23` = c(3.7, 9.9, 18.1, 12.8, 2.3, 0,
8.7, 3.7, 1.5, 8.6, 5.7, 5.8, 2.1, 1.5, 6.1, 1.6, 0.6, 2.9, 9.1,
7.4, 0, 0, 9.5, 8.7, 4.6, 0, 0, 8.6, 1.6, 7.3, 6.3, 3, 7.8, 2.2,
0.7, 3.9, 2.8, 0.4, 6.2, 1.1, 2.3, 4.9, 4.5, 8, 8.6, 3.9, 1.5,
1.6, 0, 1.4, 3.2, 3.6, 3, 5.1, 4.1, 8.8, 9.6, 0.7)), row.names = c(NA,
-58L), class = c("tbl_df", "tbl", "data.frame"))


medians <- ddply(datas, .(Pain, Sex), summarise, m = round(median(`23`), 1))
means <- ddply(datas, .(Pain, Sex), summarise, m = round(mean(`23`), 1))


datas %>%
  dplyr::select(Pain, Sex, `23`) %>%
  dplyr::group_by(Pain, Sex) %>%
  ggplot(aes(y = `23`, x = Sex, fill = Pain)) +
  geom_boxplot(fatten = 1, outlier.alpha = 0.50, position = position_dodge(width = 1.5), width = 0.8) +
  geom_text(data = medians, aes(x = Sex, y = m, label = m), color = 'blue', vjust = -0.5, size = 5, position = position_dodge(width = 1.5)) +
  geom_text(data = means, aes(x = Sex, y = m, label = m), color = 'red', vjust = 0.5, size = 5, position = position_dodge(width = 1.5)) +
  stat_boxplot(geom ='errorbar', position = position_dodge(width = 1.5)) +
  stat_summary(aes(ymax = ..y.., ymin = ..y.., color = 'Mean'),
               fun = mean, geom = 'errorbar', width = .75, linetype = 'solid', position = position_dodge(width = 1.5)) +
  stat_summary(aes(ymax = ..y.., ymin = ..y.., color = 'Median'),
               fun = median, geom = 'errorbar', width = .75, linetype = 'solid', position = position_dodge(width = 1.5)) +
  scale_colour_manual('Values', values = c(Median = 'blue', Mean = 'red')) +
  scale_fill_manual(values = c('lightblue', 'lightgreen')) +
  theme(legend.position = 'top',
        text = element_text(size = 12),
        strip.text = element_text(size = 11, face = "bold"),
        legend.text = element_text(size = 12),
        axis.text.x = element_text(size = 16, vjust = 1),
        panel.spacing.x = unit(2, "lines")) +
  facet_wrap(vars(Sex), scales = 'free')

Any help much appreciated, thank you

Hi again,
tried to figure this out, but not getting still what I want. A help is needed please.

This topic was automatically closed 42 days after the last reply. New replies are no longer allowed.

If you have a query related to it or one of the replies, start a new topic and refer back with a link.