rggplot2facetggtext

How to wrap facet labels using ggtext element_textbox


I'm using element_textbox_simple() from ggtext to wrap facet labels in my ggplot2 theme. But the strip height doesn't seem to increase with the additional lines. How do I get the textbox to wrap correctly?

I've considered using the labeller = label_wrap_gen() argument in facet_grid(), but I feel element_textbox() is more generalisable to different facet sizes/counts.

library(ggplot2)
library(ggtext)

mpg2 <- mpg
mpg2$drv[mpg2$drv == '4'] <- 'This is a long way of writing Four-weel drive vehicle'
mpg2$cyl[mpg2$cyl == 8] <- 'This is a long way of writing Eight-cylinder vehicle, which is very powerful'

ggplot(mpg2, aes(displ, cty)) + 
  geom_point() +
  facet_grid(vars(drv), vars(cyl)) +
  theme(
    strip.background = element_rect(fill = 'black', colour = 'black'),
    strip.text.x = ggtext::element_textbox_simple(colour = 'red', face = 'bold', size = 10, hjust = 0.5, vjust = 0.5, halign = 0.5, valign = 0.5),
    strip.text.y = ggtext::element_textbox_simple(colour = 'red', face = 'bold',  size = 10, hjust = 0.5, vjust = 0.5, halign = 0.5, valign = 0.5, orientation = "right-rotated")
    )

This is the result


Solution

  • For the strip text, the proposed solution is to use ggtext::element textbox(), which can wrap text depending on the available width. However, we are then confronted with a new issue: the height of the wrapped text cannot be determined automatically.

    Sample code:

    library(ggplot2)
    library(ggtext)
    
    
    ggplot(mpg2, aes(displ, cty)) + 
      geom_point() +
      facet_grid(vars(drv),  vars(cyl))+
      theme(
        strip.background = element_rect(fill = 'black', colour = 'black'),
        strip.text.x = ggtext::element_textbox_simple( width  = unit(1, "npc"),
                                                       height = unit(10 * 0.5, "lines"),
                                                       colour = 'red', 
                                                       face = 'bold', 
                                                       size = 10,
                                                       hjust = 0.5, 
                                                       vjust = 0.5, 
                                                       halign = 0.5,
                                                       valign = 0.5),
        
        strip.text.y = ggtext::element_textbox_simple(width  = unit(1, "npc"),
                                                      height = unit(10 * 0.5, "lines"),
                                                      colour = 'red', face = 'bold',  size = 10, 
                                                      hjust = 0.5, 
                                                      vjust = 0.5, 
                                                      halign = 0.5, 
                                                      valign = 0.5, 
                                                      orientation = "right-rotated"))
    

    Plot:

    enter image description here

    Sample data:

    mpg2 <- mpg
        mpg2$drv[mpg2$drv == '4'] <- 'This is a long way of writing Four-weel drive vehicle'
        mpg2$cyl[mpg2$cyl == 8] <- 'This is a long way of writing Eight-cylinder vehicle, which is very powerful'