Alignment cheatsheet

[This article was first published on R tips – NHS-R Community, and kindly contributed to R-bloggers]. (You can report issue about the content on this page here)
Want to share your content on R-bloggers? click here if you have a blog, or here if you don't.

I’ve used {ggtext}’s geom_textbox() a lot in my recent data visualisations, but, every time, I end up resorting to a bit of trial and error to get the alignment just right. Time to create a cheatsheet that will be useful to my future self, and hopefully to a few others also.

First, why geom_textbox()?

I like it for its versatility. It allows for fun things with markdown and a limited number of html tags, which makes it easy to format the text on the fly. I’ll demo a few tricks in the code below but cover them in more detail elsewhere. It also allows for easy placement and manipulation of the box itself, tweaking the radius of the corners, the colour, how thick the border is, and many more things.

What are the alignment parameters?

  • vjust
  • valign
  • hjust
  • halign

These can be set per textbox using values within aes() to allow for different alignments for different textboxes, or across the board outside of aes() so that the same alignment settings are applied to all the textboxes. In this demo, I’ll set different values for each box.

OK, let’s go!

First, we need to load the necessary libraries:


Next, let’s put a tibble together to provide data for our textboxes.

alignments <- tibble("h_align" = sort(rep(c(0, 0.5, 1), 3)),
                     "h_just" = rep(c(0, 0.5, 1), 3), 
                     "v_align" = rep(c(0, 0.5, 1), 3),
                     "v_just" = sort(rep(c(0, 0.5, 1), 3))) %>%
  mutate("content" = 
           paste0("<span style=\"font-size:36px\">↕</span> **vjust = ", v_just,
                  ", valign = ", v_align, "**<br>",
                  "<span style=\"font-size:36px\">↔</span> **hjust = ", h_just,
                  ", halign = ", h_align, "**<br><br>",
                  "Here's some text in a box, and *this* is how everything aligns!"))

And finally, let’s create a plot to see how everything aligns.

ggplot(alignments) +
  geom_textbox(aes(x = h_just, y = v_just, label = content,
                   halign = h_align, hjust = h_just,
                   valign = v_align, vjust = v_just),
               size = 6.4,
               colour = "#232a27",
               box.colour = "#705c70", 
               fill = "#f1f4f3",
               family = "Lato",
               width = unit(16, "lines"),
               height = unit(15, "lines"),
               lineheight = 1.3,
               show.legend = F) +
  geom_point(aes(x = h_just, y = v_just), 
             size = 10, alpha = 0.5, colour = "#232a27") +
  labs(title = "\ngeom_textbox() alignment cheatsheet",
       subtitle = "\nThe dots indicate the x/y coordinates of each box",
       caption = "Graphic: @cararthompson |\n") +
  xlim(c(-0.1, 1.1)) +
  ylim(c(-0.1, 1.1)) +
  theme(plot.title = element_text(colour = "#232a27", 
                                  size = 60, family = "Abel"),
        plot.subtitle = element_text(colour = "#232a27", 
                                     size = 40, family = "Lato"),
        plot.caption = element_text(colour = "#232a27", size = 20, 
                                    family = "Abel", hjust = 0.95))
Plot demonstrating how each alignment parameter affects the position of the box and the position of the text therein
Plot demonstrating how each alignment parameter affects the position of the box and the position of the text therein

So, what does what?

  • h/vjust determines the alignment of the box against its coordinates
  • h/valign determines the alignment of the text within the box
  • for h/valign 0 to 1 moves the text from bottom to top and from left to right inside the boxes (huh, that’s the same direction as 0 to 1 on the axes – that should make it easier to remember!)
  • for h/vjust 0 to 1 goes… well I’m not too sure!

I think my confusion around how to verbalise the direction lies in what we use as our reference point. Are we aligning the top of the box to the coordinate, or are we aligning the box to the bottom of the coordinate? If we keep our focus on the bottom/middle top of the box, vjust = 1 means “make the top of this box line up with the y-coordinate” and the 0 to 1 direction stays consistent with the direction of the y axis. Happy days!

But I think I’ll probably still end up referring back to this plot.

The post Alignment cheatsheet appeared first on NHS-R Community.

To leave a comment for the author, please follow the link and comment on their blog: R tips – NHS-R Community. offers daily e-mail updates about R news and tutorials about learning R and many other topics. Click here if you're looking to post or find an R/data-science job.
Want to share your content on R-bloggers? click here if you have a blog, or here if you don't.

Never miss an update!
Subscribe to R-bloggers to receive
e-mails with the latest R posts.
(You will not see this message again.)

Click here to close (This popup will not appear again)