Skip to content

ggsave() doesn't render custom fonts when saving (+workaround)Β #4824

@markjrieke

Description

@markjrieke

I've been using a few scripts that save plots with custom fonts loaded via extrafont. This was working fine up until a few days ago, when ggsave stopped saving the plots w/the custom font (I update this plot daily, so I can see from the commit history that on 4/27 it was working & on 4/28 it wasn't).

For example, this renders in the IDE correctly:

library(ggplot2)
#> Warning: package 'ggplot2' was built under R version 4.1.2
library(ggtext)
extrafont::loadfonts(device = "win")

ggplot(mtcars,
       aes(x = hp,
           y = qsec)) +
  geom_point() +
  theme(plot.title = element_markdown(family = "Vivaldi")) +
  labs(title = "Here is a title rendered with **markdown**")

Created on 2022-04-29 by the reprex package (v2.0.1)

but when I save w/ggsave, here's what appears:

tmp

Rendering in the IDE is fine, & saving by exporting from the IDE is fine, it's just when I call ggsave. I found a workaround that seems to do the job - specifying the device in both the filename and device fields, but it feels a bit clunky:

# doesn't work
plot %>%
  ggsave("plot_without_font.png")

# works
plot %>%
  ggsave("plot_with_font.png",
         device = png)

Not sure what the difference is between a few days ago & now (I haven't updated packages in the past few days, so I'm assuming there's some latent issue in my environment or session), but thought I'd highlight in case anyone else is running into the same issue!

Metadata

Metadata

Assignees

No one assigned

    Labels

    bugan unexpected problem or unintended behavior

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions