Search code examples
rggplot2boxplotspacing

Add space between groups of boxplots in ggplot2


I have a dataset and I have made a boxplot from it. But I want to add some space between the two groups of my data.

This is my data:

structure(list(variable = structure(c(1L, 1L, 1L, 1L, 1L, 1L, 
1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 2L, 2L, 2L, 2L, 2L, 2L, 2L, 
2L, 2L, 2L, 2L, 2L, 2L, 2L, 2L, 3L, 3L, 3L, 3L, 3L, 3L, 3L, 3L, 
3L, 3L, 3L, 3L, 3L, 3L, 3L, 4L, 4L, 4L, 4L, 4L, 4L, 4L, 4L, 4L, 
4L, 4L, 4L, 4L, 4L, 4L, 5L, 5L, 5L, 5L, 5L, 5L, 5L, 5L, 5L, 5L, 
5L, 5L, 5L, 5L, 5L, 6L, 6L, 6L, 6L, 6L, 6L, 6L, 6L, 6L, 6L, 6L, 
6L, 6L, 6L, 6L, 7L, 7L, 7L, 7L, 7L, 7L, 7L, 7L, 7L, 7L, 7L, 7L, 
7L, 7L, 7L, 8L, 8L, 8L, 8L, 8L, 8L, 8L, 8L, 8L, 8L, 8L, 8L, 8L, 
8L, 8L, 9L, 9L, 9L, 9L, 9L, 9L, 9L, 9L, 9L, 9L, 9L, 9L, 9L, 9L, 
9L, 10L, 10L, 10L, 10L, 10L, 10L, 10L, 10L, 10L, 10L, 10L, 10L, 
10L, 10L, 10L, 11L, 11L, 11L, 11L, 11L, 11L, 11L, 11L, 11L, 11L, 
11L, 11L, 11L, 11L, 11L), levels = c("dm4m_c", "dm4l_c", "dm3_c", 
"dm2_c", "dm1_c", "dldm_c", "dm4m_i", "dm4l_i", "dm3_i", "dm2_i", 
"dm1_i"), class = "factor"), value = c(4.54, 3.73, 3.06, 2.92, 
3.01, 4.45, 2.36, 3.35, 4.35, 5.67, 2.91, 3.72, 5.39, 3.4, 3.07, 
1.2, 0.79, 0.43, 0.17, 0.03, 0.6, 0.15, -0.24, 0.98, 0.15, -0.37, 
0.2, 1.71, 0.46, 0.26, 0.46, 0.32, 0.49, -0.03, 0.39, 0.6, 0.47, 
0.32, 1.17, 0.55, -0.48, 0.13, 1.65, -0.2, 0.05, 2.86, 2.19, 
2.06, 1.66, 2, 2.95, 0.45, 1.29, 1.96, 1.07, 0.52, 0.27, 3.17, 
1.24, 1.03, -0.24, -0.41, -0.45, NA, NA, NA, -0.36, -0.39, -0.13, 
-0.49, -0.81, -0.58, 0.69, -0.93, -0.43, 0.28, -0.14, -0.04, 
-0.31, 0.27, 0.34, 0.9, 0.28, 0.8, 0.25, -0.37, -0.35, 0.59, 
-0.29, -0.07, 3.8, 2.28, 2.43, 1.29, 1.01, 2.65, 1.75, 2.42, 
3.86, 4.74, 1.7, 1.74, 2.55, 1.58, 1.32, 0.88, 0.31, 0.2, -0.9, 
-1.14, 0.4, 0.06, 0.06, 0.69, 0.81, 0.24, 0.17, 0.73, -0.1, 0.35, 
0.28, 0.03, -0.01, 0.13, 0.42, 0.79, 0.18, -0.02, 0.43, 1.68, 
-0.08, 0.23, 0.98, -0.6, -0.23, 2.02, 1.24, 1.46, 0.8, 0.89, 
1.89, 0.19, 0.55, 1.34, 1.05, 0, 0.04, 1.81, 0.2, 0.21, -0.09, 
-0.41, -0.4, NA, NA, NA, -0.54, -0.77, -0.32, -0.04, -0.98, -0.29, 
0.8, -0.99, -0.68), group = structure(c(1L, 1L, 1L, 1L, 1L, 1L, 
1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 
1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 
1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 
1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 
1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 
1L, 1L, 1L, 1L, 2L, 2L, 2L, 2L, 2L, 2L, 2L, 2L, 2L, 2L, 2L, 2L, 
2L, 2L, 2L, 2L, 2L, 2L, 2L, 2L, 2L, 2L, 2L, 2L, 2L, 2L, 2L, 2L, 
2L, 2L, 2L, 2L, 2L, 2L, 2L, 2L, 2L, 2L, 2L, 2L, 2L, 2L, 2L, 2L, 
2L, 2L, 2L, 2L, 2L, 2L, 2L, 2L, 2L, 2L, 2L, 2L, 2L, 2L, 2L, 2L, 
2L, 2L, 2L, 2L, 2L, 2L, 2L, 2L, 2L, 2L, 2L, 2L, 2L, 2L, 2L), levels = c("1", 
"2"), class = "factor")), row.names = c(NA, -165L), class = "data.frame")

This is the boxplot that I made and I want to make a space instead of the dashline:

this is the boxplot that I made and I want to make a space instead of the dashline

I have tried different things. I make the group of them and I have use facet_wrap but the result was not what I intended:

the facet_wrap function made 2 y axix that I don't want it.

This is my final code:

ggplot(data_long2, aes(x = variable, y = value,fill=variable)) +            # Applying ggplot function
  geom_boxplot(position=position_dodge(width=5)) +
  geom_point() +
  stat_summary(fun.y="mean", color="white", shape=7)+
  theme_classic()+
  ggtitle("Slop") +
  xlab("Zones") + ylab("Slop") +
  theme(axis.text.x = element_text(angle = 90, vjust = 0.5),legend.position = "none") +
  geom_vline(xintercept = 6.5, linetype = "dashed")

Solution

  • I've recently added a function called scale_x_manual() to {ggh4x}, which lets you manually place discrete values at continuous positions. You can use this to place your second set a bit further away. Unfortunately, it currently has a bug where only 'double' vectors (and not integers) are accepted. Example below (parts not related to question omitted).

    library(ggplot2)
    library(ggh4x)
    
    ggplot(data_long2, aes(x = variable, y = value, fill = variable)) +
      geom_boxplot() +
      scale_x_manual(as.double(c(1:6, 8:13))) # set numbers to your liking
    #> Warning: Removed 6 rows containing non-finite values (`stat_boxplot()`).
    

    Disclaimer: I'm the author of ggh4x.