Skip to content

element_text() ignores left and right margins for some plot elements #5533

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Closed
ccsarapas opened this issue Nov 22, 2023 · 1 comment · Fixed by #5545
Closed

element_text() ignores left and right margins for some plot elements #5533

ccsarapas opened this issue Nov 22, 2023 · 1 comment · Fixed by #5545

Comments

@ccsarapas
Copy link

ccsarapas commented Nov 22, 2023

Left and right margins get ignored for some elements (including plot titles and captions), but work fine for other elements (such as strip and axis labels.

Given this plot:

library(ggplot2)

p <- ggplot(mpg, aes(cty, hwy)) +
  geom_point() +
  facet_wrap(vars(cyl)) +
  labs(
    title = "City vs. Highway Mileage by Cylinders", 
    caption = "Values are in MPG."
  ) +
  theme(
    plot.background = element_rect(color = "black", linewidth = 1),
    axis.title.y = element_text(angle = 0, vjust = 0.5),
    strip.text = element_text(hjust = 0)
  )

p

You can change the top and bottom margins for the title and caption, but the left and right margins don't budge:

p + 
  theme(
    plot.title = element_text(margin = margin(20, 20, 20, 20)),
    plot.caption = element_text(margin = margin(20, 20, 20, 20))
  )

But all 4 margins seem to work for axis titles and strip labels:

p +
  theme(
    strip.text = element_text(margin = margin(20, 20, 20, 20)),
    axis.title.y = element_text(margin = margin(20, 20, 20, 20))
  )

Created on 2023-11-22 with reprex v2.0.2

@teunbrand
Copy link
Collaborator

For some theme elements, only one margin is used purposefully, see for example the (sub)title/caption declarations here:

ggplot2/R/plot-build.R

Lines 267 to 277 in ca47270

# Title
title <- element_render(theme, "plot.title", plot$labels$title, margin_y = TRUE)
title_height <- grobHeight(title)
# Subtitle
subtitle <- element_render(theme, "plot.subtitle", plot$labels$subtitle, margin_y = TRUE)
subtitle_height <- grobHeight(subtitle)
# whole plot annotation
caption <- element_render(theme, "plot.caption", plot$labels$caption, margin_y = TRUE)
caption_height <- grobHeight(caption)

For some elements, say x-axis text, the horizontal margins are ignored due to the labels being linked to a single point that varies in the x-direction. This makes sense to me.

Other elements are linked to a cell in the layout, like the titles. It makes less sense to me to ignore part of the margins there.
The default theme's margins are 0 for left/right (sub)titles and captions, so I don't see any explicit harm in enabling them, but I'd like to wait out to hear what the others think.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
None yet
Development

Successfully merging a pull request may close this issue.

2 participants