Description
I'm trying to use element_textbox_simple() to automatically wrap an axis title. This works as expected (and well -- thank you!) to yield a textbox of appropriate height and width containing appropriately-wrapped text, as in the following:
tbl <- tibble(x = c(1,2,3,4), y = c( 1,2,3,4))
ggplot(tbl, mapping = aes(x=x, y=y)) + geom_point() + xlab( "A really really really really long, just truly amazingly long and incredibly dull x-axis title") + theme( axis.title.x = element_textbox_simple( linetype=1, halign=0.5, margin=margin(0,0,0,0)))
With this sample code, I get an x-axis title textbox that's 2 lines high, and it's positioned immediately below (b/c margin=0, just for demo purposes) the x-axis tick labels; all as expected.
However, a problem arises when the aspect ratio of the panel is changed (by including aspect.ratio = 2/1
within the theme()
call): While the x-axis title textbox itself is still constructed correctly (the text width matches the aspect-ratio-adjusted x-axis length, and the text is thus appropriately wrapped onto 4 lines instead of 2), this textbox is no longer positioned correctly; instead it's positioned on the plot as if it were only 2 lines high. Depending on the valign setting, it extends too far down, off the bottom edge of the plot; and/or it overlaps the x-axis tick labels instead of being immediately below them.
So it appears that when an aspect ratio adjustment is present, element_textbox_simple() is positioning the textbox based on a textbox height computed before considering the aspect-ratio adjustment, whereas the textbox itself is constructed based on the (correct) width and height after the aspect-ratio adjustment.
The same problem occurs with y-axis titles (when including an aspect ratio sufficiently less than 1 to increase the number of lines in the y-axis title). Haven't tested anything other than axis titles; haven't tried to test what happens if an aspect ratio adjustment decreases textbox height; and not sure whether element_textbox() has exactly the same problem, but a quick try suggested at least something similar happens there.