tgod7258
tgod7258

Reputation: 35

Word spacing too close when using element_textbox with imported fonts

I like to use element_textbox from the ggtext package as it provides automatic line breaks when captions get too long. However, when combined with the font that I'm using - Public Sans https://fonts.google.com/specimen/Public+Sans - the spacing becomes too close together, with some words overlapping each other. It usually looks ok in the preview pane, but not after saving as a .png with ggsave().

library(ggplot2)
library(ggtext)
library(extrafont)

extrafont::font_import(paths = NULL, prompt = FALSE, pattern = "PublicSans")
extrafont::loadfonts(device = "win")

ggplot(mpg, aes(displ, hwy, colour = class)) + 
  geom_point() +
  labs(
    title = "When I use element_text, spacing is fine...",
    caption = "... But this is a really long caption that might span more than the width of the chart in some cases, that's why I use element_textbox which sometimes makes the spacing too close together.") + 
  theme(
    text = element_text(family = "Public Sans"),
    plot.title = element_text(size = 9),
    plot.caption = ggtext::element_textbox_simple(size = 9,
                                           fill = "red")
  )

ggsave("test.png")

caption spacing too close

I've tried different values for the padding and margin arguments, but nothing has given the desired effect.

Extra info: I use extrafont to import fonts for ggplot, and I uninstalled ragg as it interferes.

Upvotes: 1

Views: 109

Answers (0)

Related Questions