align elements in ggplot


I'm trying to build a ggplot2 theme that will conform to my company's graphics standards. (Efforts to engineer the rules rather than the plots have been mostly unsuccessful. Alas.) The image below illustrates the main challenge I'm having: there are a number of requirements about the alignment of axis labels relative to the y-axis numbers, and the spacing of certain elements.

I can mess around with vjust and hjust for individual plots but if there's some easy way to grab the position of each of the relevant elements and snap them to the same line that would be helpful to know.


I'm not sure I understand all of your annotations in terms of ggplot2 elements. It'd be helpful to have a reprex (a small, reproducible example) to make it easier to discuss more concretely.

Not sure if this is what you mean for one of them, but you can adjust the distance between the legend symbol and the text (see the PR where it was implemented below)

Again, I could be off with what you're looking for, but in hrbrthemes, there's a flush_ticks() function that could be handy:

1 Like

I also highly recommend cowplot for composing figures like these! Claus is currently doing work on text for R graphics more generally, too, but cowplot great for it,

#> Registered S3 methods overwritten by 'ggplot2':
#>   method         from 
#>   [.quosures     rlang
#>   c.quosures     rlang
#>   print.quosures rlang
theme_acme <- list(
    plot.caption = element_text(hjust = 0, size = 6),
    legend.position = "bottom",
    legend.justification = "left",
    legend.title = element_text(size = 7, face = "bold"),
    plot.title = element_text(size = 7, face = "bold"),
    axis.title.x = element_text(hjust = 0, size = 7, face = "bold"),
    axis.text = element_text(size = 7, face = "bold")

ggplot(iris, aes(Petal.Width, Petal.Length, color = Species)) +
  geom_point() +
    y = "",
    title = "Petal.Length",
    caption = "Source: iris data."
  ) +
  guides(color = guide_legend(ncol = 1)) +

Created on 2019-07-25 by the reprex package (v0.3.0)

To try state the goal more clearly: the left edges of legend.title, x.axis.title, and y.axis.title all need to be flush with the leftmost digit appearing on the y-axis.

I've been using the plot.title rather than the y.axis.title because it gets closer to the desired effect.

I will take a look at flush_ticks(), thanks for the suggestion.

This topic was automatically closed 21 days after the last reply. New replies are no longer allowed.