Weird fix to "ggsave does not save fonts properly when saving as svg". Anyone knows why this works?

1,708 views
Skip to first unread message

Alexis Solis

unread,
Jan 19, 2022, 10:45:05 PM1/19/22
to ggplot2
I use a custom font for my ggplot2 theme (adhering to my company's policies). Using both `ggplot2 `and `svglite`, I've been saving some plots as `.svg`. However, the fonts don't render correctly in PowerPoint (although they DO render correctly when opening the .svg file in say, a browser). I realized that adding the line `device = svg`  inside the `ggsave()` function WITHOUT the quotations around `svg` fixes the issue. Why would this happen? See reprex below for an example:

```
library(tidyverse)
library(svglite)

# Import ggplot2 theme, which uses the DINPro Font
dinpro_path <- "path/to_custom/dinpro_theme.R"
source(dinpro_path)

# Generic plot
mtcars %>%
  ggplot(aes(mpg, disp)) +
  geom_point() +
  labs(
    title = "plot_title",
    subtitle = "– plot_subtitle"
  ) +
  dinpro_theme()

# This DOES NOT work since fonts don't show in PowerPoint or any other MS Office product.
ggsave(
  filename = "svg_name.svg",
  path = "charts/",
  device = 'svg',
  width = 8,
  height = 11,
  units = 'cm',
  dpi = 300
)


# This works perfectly!
ggsave(
  filename = "svg_name.svg",
  path = "charts/",
  device = svg,
  width = 8,
  height = 11,
  units = 'cm',
  dpi = 300
)
```
I'm using the latest versions of the packages:
ggplot2: ‘3.3.5’
svglite: ‘2.0.0’

Reply all
Reply to author
Forward
0 new messages