Widen and Space Boxplots

I am trying to make a nice figure of multiple boxplots but I am having trouble getting reasonable width and spacing between them. Can someone please help me widen each boxplot (ideally all being the same width) but prevent them from overlapping? This is what I've tried so far:


library(ggplot2)
library(ggExtra)
library(ggpattern)

structure(list(Data = c("South_Africa", "Lesbos", "Costa_Rica", 
"Lesbos", "South_Africa", "Lesbos", "Lesbos", "Lesbos", "Lesbos", 
"Lesbos", "Costa_Rica", "South_Africa", "South_Africa", "Costa_Rica", 
"Lesbos", "Lesbos", "Lesbos", "Lesbos", "Lesbos", "Lesbos", "Lesbos", 
"Costa_Rica", "South_Africa", "Costa_Rica", "Lesbos", "South_Africa", 
"Lesbos", "South_Africa", "Lesbos", "Lesbos", "Lesbos", "Costa_Rica", 
"South_Africa", "Costa_Rica", "Costa_Rica", "Costa_Rica", "Lesbos", 
"Costa_Rica", "South_Africa", "Lesbos"), Soil = c("NU", "NU", 
"NU", "NU", "NU", "NU", "NU", "U", "NU", "NU", "NU", "NU", "NU", 
"NU", "NU", "NU", "U", "NU", "NU", "U", "NU", "NU", "NU", "NU", 
"U", "NU", "U", "U", "U", "U", "NU", "NU", "NU", "NU", "NU", 
"NU", "U", "NU", "NU", "U"), Taxa = c("Gisekia africana", "Dactylis_glomerata", 
"piper", "Crepis_commutata", "Ruellia patula", "Hordeum_bulbosum", 
"Bromus_commutatus", "Torilis_nodosa", "Lagoecia_cuminoides", 
"Aegilops_biuncialis", "guamac", "Celtis africana", "Ziziphus mucronata", 
"exomex", "Trachynia_distachya", "Lolium_rigidum", "Trifolium_angustifolium", 
"Plantago_lagopus", "Torilis_nodosa", "Lolium_rigidum", "Trifolium_arvense", 
"bauung", "Senecio latifolius", "mactin", "Aegilops_biuncialis", 
"Geigeria ornativa", "Trachynia_distachya", "Faurea saligna", 
"Lagoecia_cuminoides", "Cynosurus_echinatus", "Phleum_pratense", 
"casser", "Polygala hottentotta", "luespe", "steobo", "chospi", 
"Hordeum_bulbosum", "calcan", "Combretum apiculatum", "Dactylis_glomerata"
), SLA = c(39.35278345, 39.2, 38.7204594, 38.193, 37.16119821, 
36.5482, 36.423, 36.3955172, 35.7273333, 34.58675, 34.236364, 
33.88607569, 33.8744023, 33.8530125, 33.366, 32.9338, 32.7803704, 
32.579, 31.9593878, 31.7509091, 31.3455, 31.3426755, 30.89855185, 
30.2421874, 30.2008955, 29.9203306, 29.7844828, 29.43251529, 
29.36, 29.1293333, 29.09, 29.0699486, 28.72968653, 28.7162493, 
28.5190057, 28.1025365, 27.94175, 27.8320321, 27.29171929, 27.266
), Clim = c("Subtropical", "Meditteranean", "Tropical", "Meditteranean", 
"Subtropical", "Meditteranean", "Meditteranean", "Meditteranean", 
"Meditteranean", "Meditteranean", "Tropical", "Subtropical", 
"Subtropical", "Tropical", "Meditteranean", "Meditteranean", 
"Meditteranean", "Meditteranean", "Meditteranean", "Meditteranean", 
"Meditteranean", "Tropical", "Subtropical", "Tropical", "Meditteranean", 
"Subtropical", "Meditteranean", "Subtropical", "Meditteranean", 
"Meditteranean", "Meditteranean", "Tropical", "Subtropical", 
"Tropical", "Tropical", "Tropical", "Meditteranean", "Tropical", 
"Subtropical", "Meditteranean"), AnnTemp = c(18.9, 15.6, 25.8, 
15.6, 18.9, 15.6, 15.6, 15.1, 15.6, 15.6, 25.8, 18.9, 18.9, 25.8, 
15.6, 15.6, 15.1, 15.6, 15.6, 15.1, 15.6, 25.8, 18.9, 25.8, 15.1, 
18.9, 15.1, 19.3, 15.1, 15.1, 15.6, 25.8, 18.9, 25.8, 25.8, 25.8, 
15.1, 25.8, 18.9, 15.1), AnnPrecip = c(716, 620.5, 1662, 620.5, 
716, 620.5, 620.5, 636.5, 620.5, 620.5, 1662, 716, 716, 1662, 
620.5, 620.5, 636.5, 620.5, 620.5, 636.5, 620.5, 1662, 716, 1662, 
636.5, 716, 636.5, 699, 636.5, 636.5, 620.5, 1662, 716, 1662, 
1662, 1662, 636.5, 1662, 716, 636.5)), row.names = c(NA, 40L), class = "data.frame")

dodge <- position_dodge2(padding = 0.5,
                         preserve = c("total", "single"))
ClimFigP <- ggplot(ClimDF, aes(x = AnnPrecip, y = SLA, fill = Data, pattern = Soil, alpha=0.4)) +
  geom_boxplot_pattern(
                       color = "black", 
                       pattern_fill = "black",
                       pattern_angle = 45,
                       pattern_density = 0.3,
                       pattern_spacing = 0.025,
                       pattern_key_scale_factor = 0.6,
                       width = 75,
                       position = dodge) +
  scale_pattern_manual(values = c(U = "circle", NU = "none")) +
  scale_fill_manual(values = c('lightblue','lightskyblue','palegreen1','lemonchiffon2','lightsalmon')) +
  theme_classic()
ClimFigP

Also if you could help me get rid of the uneccessary alpha key and remove the stripes in the Data key that would be much appreciated!

1 Like

Remember put a reproducible example of data for better help you all the community.

Its easy like that: dput(dataframe[1:40, ]). And try to select all the case in AnnPrecip

Thank you. I have now included reproducible data.

Hi @tjsamo , Im find this solution. Add position = position_dodge2(preserve = "single"). Now try with the all data.

dodge <- position_dodge2(padding = 0.5,
                         preserve = c("total", "single"))
ClimFigP <- ggplot(ClimDF, aes(x = AnnPrecip, y = SLA, fill = Data, pattern = Soil, alpha=0.4)) +
  geom_boxplot_pattern(
                       color = "black", 
                       pattern_fill = "black",
                       pattern_angle = 45,
                       pattern_density = 0.3,
                       pattern_spacing = 0.025,
                       pattern_key_scale_factor = 0.6,
                       width = 75,
                       position = position_dodge2(preserve = "single")) + # Check this 
  scale_pattern_manual(values = c(U = "circle", NU = "none")) +
  scale_fill_manual(values = c('lightblue','lightskyblue','palegreen1',
                               'lemonchiffon2','lightsalmon')) +
  theme_classic()
ClimFigP

Thank you! That looks much better.
Now I am combining two plots using ggarrange. How do I get the boxplot widths to be the same for each? I don't like how the top plot's boxes are wider.

dodge <- position_dodge2(padding = 0.5,
                         preserve = c("total", "single"))
ClimFigT <- ggplot(ClimDF) +
  geom_boxplot_pattern(aes(x = AnnTemp, y = SLA, fill = Data, pattern = Soil, alpha=0.4),
                       color = "black", 
                       pattern_fill = "black",
                       pattern_angle = 45,
                       pattern_density = 0.3,
                       pattern_spacing = 0.025,
                       pattern_key_scale_factor = 0.6,
                       width = 0.1 + 
                       position = position_dodge2(preserve = "single")) +
  scale_pattern_manual(values = c(U = "circle", NU = "none")) +
  scale_fill_manual(values = c('lightblue','lightskyblue','palegreen1','lemonchiffon2','lightsalmon')) +
  labs(y = expression(paste("SLA (", mm^{2},mg^-1,")")),y = "", fill="",
       x = "Mean Annual Temperature (ºC)" ) +
  theme_classic(base_size = 14)
ClimFigT
dev.off()
dev.new()

dodge <- position_dodge2(padding = 0.5,
                         preserve = c("total", "single"))
ClimFigP <- ggplot(ClimDF, aes(x = AnnPrecip, y = SLA, fill = Data, pattern = Soil, alpha=0.4)) +
  geom_boxplot_pattern(
                       color = "black", 
                       pattern_fill = "black",
                       pattern_angle = 45,
                       pattern_density = 0.3,
                       pattern_spacing = 0.025,
                       pattern_key_scale_factor = 0.6,
                       width = 150,
                       position = position_dodge2(preserve = "single")) +
  scale_pattern_manual(values = c(U = "circle", NU = "none")) +
  scale_fill_manual(values = c('lightblue','lightskyblue','palegreen1','lemonchiffon2','lightsalmon')) +
  labs(y = expression(paste("SLA (", mm^{2},mg^-1,")")),y = "", fill="",
       x = "Mean Annual Precipitation (mm)" ) +
  theme_classic(base_size = 14)
ClimFigP

library(ggpubr)
fig1 <- ggarrange(ClimFigT,
                  ClimFigP,
                  #labels = c("A", "B", "C"),
                  ncol = 1, nrow = 2,
                  common.legend = TRUE,
                  label.y = 1,
                  legend="right")
print(fig1)

This topic was automatically closed 21 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.