1

I have spent my entire day today trying to sort this out. I have found tons of old posts with no actual solutions to this problem.

I have a custom font - it's loaded, it's installed, it appears when I look under font_families().

The font appears correctly when I generate the plot, but then completely disappears when I use ggsave to export it.

MWE Code:


a <- ggplot(mtcars, aes(x=wt, y=mpg)) + geom_point() +
  ggtitle("Fuel Efficiency of 32 Cars") +
  xlab("Weight (x1000 lb)") + ylab("Miles per Gallon") +
  theme(text=element_text(size=16,  family="Larsseit-BoldItalic"))

a

ggsave(filename = "~/Desktop/test_plot.png", 
       plot=a, 
       width= 5.64*2, 
       height=3.58*2, 
       #height=26/(10/3.125), 
       dpi=300)

Intended output:

enter image description here

Actual output:

enter image description here

Session info:

sessioninfo::session_info()
─ Session info ─────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────
 setting  value
 version  R version 4.2.1 (2022-06-23)
 os       macOS Monterey 12.6.1
 system   x86_64, darwin17.0
 ui       RStudio
 language (EN)
 collate  en_US.UTF-8
 ctype    en_US.UTF-8
 tz       America/Chicago
 date     2022-11-11
 rstudio  2022.07.0+548 Spotted Wakerobin (desktop)
 pandoc   NA

─ Packages ─────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────
 package     * version date (UTC) lib source
 assertthat    0.2.1   2019-03-21 [1] CRAN (R 4.2.0)
 cli           3.3.0   2022-04-25 [1] CRAN (R 4.2.0)
 colorspace    2.0-3   2022-02-21 [1] CRAN (R 4.2.0)
 DBI           1.1.3   2022-06-18 [1] CRAN (R 4.2.0)
 digest        0.6.29  2021-12-01 [1] CRAN (R 4.2.0)
 dplyr         1.0.9   2022-04-28 [1] CRAN (R 4.2.0)
 fansi         1.0.3   2022-03-24 [1] CRAN (R 4.2.0)
 farver        2.1.1   2022-07-06 [1] CRAN (R 4.2.0)
 generics      0.1.3   2022-07-05 [1] CRAN (R 4.2.0)
 ggplot2     * 3.3.6   2022-05-03 [1] CRAN (R 4.2.0)
 glue          1.6.2   2022-02-24 [1] CRAN (R 4.2.0)
 gtable        0.3.0   2019-03-25 [1] CRAN (R 4.2.0)
 labeling      0.4.2   2020-10-20 [1] CRAN (R 4.2.0)
 lifecycle     1.0.1   2021-09-24 [1] CRAN (R 4.2.0)
 magrittr      2.0.3   2022-03-30 [1] CRAN (R 4.2.0)
 munsell       0.5.0   2018-06-12 [1] CRAN (R 4.2.0)
 pillar        1.8.0   2022-07-18 [1] CRAN (R 4.2.1)
 pkgconfig     2.0.3   2019-09-22 [1] CRAN (R 4.2.0)
 purrr         0.3.4   2020-04-17 [1] CRAN (R 4.2.0)
 R6            2.5.1   2021-08-19 [1] CRAN (R 4.2.0)
 rlang         1.0.4   2022-07-12 [1] CRAN (R 4.2.0)
 rstudioapi    0.13    2020-11-12 [1] CRAN (R 4.2.0)
 scales        1.2.0   2022-04-13 [1] CRAN (R 4.2.0)
 sessioninfo   1.2.2   2021-12-06 [1] CRAN (R 4.2.0)
 tibble        3.1.8   2022-07-22 [1] CRAN (R 4.2.0)
 tidyselect    1.1.2   2022-02-21 [1] CRAN (R 4.2.0)
 utf8          1.2.2   2021-07-24 [1] CRAN (R 4.2.0)
 vctrs         0.4.1   2022-04-13 [1] CRAN (R 4.2.0)
 withr         2.5.0   2022-03-03 [1] CRAN (R 4.2.0)

 [1] /Library/Frameworks/R.framework/Versions/4.2/Resources/library
lucretiuss
  • 75
  • 1
  • 7
  • 1
    `ragg` for rendering (with `dev = "ragg_png"`) and `systemfonts` (for registering fonts might help. – Jon Spring Nov 11 '22 at 22:06
  • 1
    I use the `showtext` package on my mac and have never had any issues. It'll also install the `sysfonts` package which is used to load the fonts. I always use `font_add_google()` since I used google fonts but the standard `font_add()` would be of use here. Just call `showtext_auto()` before the graph. – Abigail Nov 12 '22 at 01:07
  • Thanks, @Abigail! I've tried all of these things except showtext_auto() right before the graph so I'll try that. I used sysfonts and font_add() to load the fonts. @JonSpring I've tried using ragg, and that didn't work. I'm really perplexed because it was working completely fine until it wasn't. – lucretiuss Nov 12 '22 at 13:40
  • 1
    @Abigail That seems to work, but then the font sizes shrink a lot. hm. – lucretiuss Nov 12 '22 at 15:15
  • @Abigail - full credit to you! I have posted an answer now that follows your solution, then solves the final line spacing problem. THANK YOU! – lucretiuss Nov 12 '22 at 15:26

1 Answers1

1

This answer gives full credit to @Abigail. First, I added showtext_auto() right before the plot, which made ggsave work correctly, but then the issue was there was extra spacing between the lines of text.

I found a discussion of this issue on the showtext github here. The issue was the the ggsave and showtext dpi defaults were different (300 and 96, respectively). So adding showtext_opts(dpi = 300) before the plot also solved that problem.

Final code, with solution, below:

library(ggplot2)

showtext::showtext_auto()
showtext::showtext_opts(dpi = 300)

a <- ggplot(mtcars, aes(x=wt, y=mpg)) + geom_point() +
  ggtitle("Fuel Efficiency of 32 Cars") +
  xlab("Weight (x1000 lb)") + ylab("Miles per Gallon") +
  theme(text=element_text(size=16,  family="Larsseit-BoldItalic"))

a

ggsave(filename = "~/Desktop/test_plot.png", 
       plot=a, 
       width= 5.64*2, 
       height=3.58*2, 
       dpi=300)
lucretiuss
  • 75
  • 1
  • 7
  • Huh, interesting. I haven't run into that issue before. Glad you were able to fix your problem though! I remember it took me way too long to figure out I needed to add the `showtext_auto` function to make everything look right. – Abigail Nov 12 '22 at 19:59