diff --git a/NEWS.md b/NEWS.md index 992d5cc395..cabdc6940b 100644 --- a/NEWS.md +++ b/NEWS.md @@ -1,5 +1,8 @@ # ggplot2 (development version) +* The plot's title, subtitle and caption now obey horizontal text margins + (#5533). + * New `guide_axis_stack()` to combine other axis guides on top of one another. * New `guide_custom()` function for drawing custom graphical objects (grobs) diff --git a/R/plot-build.R b/R/plot-build.R index 7fa0a89be3..51208d20dd 100644 --- a/R/plot-build.R +++ b/R/plot-build.R @@ -265,15 +265,24 @@ ggplot_gtable.ggplot_built <- function(data) { } # Title - title <- element_render(theme, "plot.title", plot$labels$title, margin_y = TRUE) + title <- element_render( + theme, "plot.title", plot$labels$title, + margin_y = TRUE, margin_x = TRUE + ) title_height <- grobHeight(title) # Subtitle - subtitle <- element_render(theme, "plot.subtitle", plot$labels$subtitle, margin_y = TRUE) + subtitle <- element_render( + theme, "plot.subtitle", plot$labels$subtitle, + margin_y = TRUE, margin_x = TRUE + ) subtitle_height <- grobHeight(subtitle) # whole plot annotation - caption <- element_render(theme, "plot.caption", plot$labels$caption, margin_y = TRUE) + caption <- element_render( + theme, "plot.caption", plot$labels$caption, + margin_y = TRUE, margin_x = TRUE + ) caption_height <- grobHeight(caption) # positioning of title and subtitle is governed by plot.title.position