Follow

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use
Contact

X labels gets cut out for faceted ggplot in R

I formatted facet_wrap so that its lables will be on the at the plot bottom instead of on top of the plot (see pic attached).
When the faceted label is too long, it get’s cut out of the facet borders.
How can I override this to allow longer strings to be in front of the facet graph?

In the attached plot the second facet label (which is ‘really_really_really_long_lable_here’) gets cut.

I can change figure dimensions and text size but the optimal solution for me is to arrange the facet strip text to be in front of the figure itself and therefore to allow long strings to appear in full length.

MEDevel.com: Open-source for Healthcare and Education

Collecting and validating open-source software for healthcare, education, enterprise, development, medical imaging, medical records, and digital pathology.

Visit Medevel

Dataframe to recreate this example:

df <- data.frame(a = c(seq(1:10)), b = seq(1:10), group = c(rep('group1',5), rep('really_really_really_long_lable_here',5)), 
                 sex = c(rep(c('M','F'),5)))


ggplot(df, aes(sex,b, group = sex)) +
              geom_boxplot() +
              facet_wrap(~group, strip.position = "bottom") +
              theme(strip.placement = "outside",
                    strip.background = element_blank(),
                    text = element_text(size = 16)) +
              xlab(NULL)

example figure

>Solution :

You have a few options. I’ll save your plot as p to demonstrate:

library(ggplot2)

df <- data.frame(
  a = seq(1:10), 
  b = seq(1:10), 
  group = c(rep('group1', 5), rep('really_really_really_long_lable_here', 5)), 
  sex = c(rep(c('M', 'F'), 5))
)


p <- ggplot(df, aes(sex, b, group = sex)) +
  geom_boxplot() +
  facet_wrap(~group, strip.position = "bottom") +
  theme(strip.placement = "outside",
        strip.background = element_blank(),
        text = element_text(size = 16)) +
  xlab(NULL)

1. Change your facet layout:

p +
  facet_wrap(~group, ncol = 1, strip.position = "bottom")

enter image description here

2. Change the font size:

p +
  theme(strip.text = element_text(size = 6))

enter image description here

3. Use text wrapping:

# Wrapping function that replaces underscores with spaces
wrap_text <- function(x, chars = 10) {
  x <- gsub("_", " ", x)
  stringr::str_wrap(x, chars)
}

# Alternative wrapping function that just drops a linebreak in every 10
# chars
wrap_text2 <- function(x, chars = 10) {
  regex <- sprintf("(.{%s})", chars)
  gsub(regex, "\\1\n", x)
}

p +
  facet_wrap(
    ~group, strip.position = "bottom",
    labeller = as_labeller(wrap_text)
  )

enter image description here

My favourite approach is #3 – or to just use shorter labels!

Add a comment

Leave a Reply

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use

Discover more from Dev solutions

Subscribe now to keep reading and get access to the full archive.

Continue reading