Skip to content

Plot boundary calculated incorrectly with multi-line axis title (when built by ggtext::element_textbox_simple() or marquee::element_marquee()) #6554

@npearlmu

Description

@npearlmu

When building a plot with an automatically-wrapped axis title (using either ggtext::element_textbox_simple() or marquee::element_marquee()), the title text is correctly wrapped and displayed in a box with appropriate height (# of lines of text), and the box is aligned (seemingly) appropriately given the vjust setting.

However, in building the overall plot with these elements, ggplot2 appears to be treating the title text box's height as just 1 line of text, even when the actual height (after wrapping the text) is > 1. As a result, the axis title text box overlaps the axis text (tick labels) and/or extends beyond the edge of the plot itself (depending on the vjust setting). E.g.:

library( ggplot2 )

plt <- ggplot( data.frame( x=c(1:4), y=c(1:4) ), 
               mapping = aes(x=x, y=y) ) + 
    geom_point()

print( plt +
        xlab( "A really really really really really really long,\njust truly amazingly long and crazy incredibly dull x-axis title" ) + 
        theme( plot.background = element_rect( color = "blue" ),
               axis.title.x = element_text( vjust=0.5,
                                            margin=margin(0,0,0,0) ) ) )

print( plt + 
        xlab( "A really really really really really really long, just truly amazingly long and crazy incredibly dull x-axis title" ) + 
        theme( plot.background = element_rect( color = "blue" ),
               axis.title.x = 
                   ggtext::element_textbox_simple( vjust=0.5,
                                                   margin=margin(0,0,0,0) ) ) )

print( plt + 
        xlab( "A really really really really really really long, just truly amazingly long and crazy incredibly dull x-axis title" ) + 
        theme( plot.background = element_rect( color = "blue" ),
               axis.title.x = 
                   marquee::element_marquee( vjust=0.5, 
                                             margin=margin(0,0,0,0),
                                             width = 1 ) ) )

Created on 2025-07-15 with reprex v2.1.1

In the first plot above, the 2-line x-axis title (created by manually inserting a line break (\n) in the title text) is handled correctly and as expected. But in the other two (using either element_textbox_simple() or element_marquee() to wrap the text automatically), ggplot2 appears to be allocating just a single text line's worth of height for the axis title text, even though it's actually 2 lines high, just as in the first plot.

Expected behavior would be for ggplot2 to allocate space for the title box based on its final (post-wrapping) height, resulting in a correctly-computed overall plot size and no clipping of the title text.

Two additional notes:

  1. I haven't tested this in detail, but it looks like the same problem arises when using these element_ calls to auto-wrap the y-axis.
  2. I originally encountered this issue when setting an aspect.ratio for the plot panel within the theme() call, and I would want the fix here to work in such cases as well (viz., when the new aspect ratio affects the word-wrapping and results in a different textbox height). Seems like this should all still be able to work correctly, but I have no idea about the underlying algorithm here.

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions