hrbrmstr / hrbrthemes

:lock_with_ink_pen: Opinionated, typographic-centric ggplot2 themes and theme components

Home Page:https://cinc.rud.is/web/packages/hrbrthemes/

Geek Repo:Geek Repo

Github PK Tool:Github PK Tool

Unable to export to pdf for some fonts

andrewdigby opened this issue · comments

I'm unable to save to PDF using anything other than Arial Narrow. This is similar to #2, but the fixes there don't work for me.

I've:

  1. Imported and installed fonts
library(hrbrthemes)
import_public_sans()
import_titillium_web() 
import_econ_sans() 
import_tinyhand() 
import_plex_sans()
import_roboto_condensed()
  1. Installed these imported fonts using FontBook (on Mac)
  2. Checked fonts are loaded:
> fonts()
  [1] "Roboto Condensed"        "Public Sans Black"       "Public Sans"             "Public Sans ExtraBold"   "Public Sans ExtraLight"  "Public Sans Light"      
  [7] "Public Sans Medium"      "Public Sans SemiBold"    "Public Sans Thin"        "Titillium Web"           "Titillium Web Light"     "EconSansCndBol"         
 [13] "EconSansCndBolIta"       "EconSansCndLig"          "EconSansCndLigIta"       "EconSansCndMed"          "EconSansCndMedIta"       "EconSansCndReg"         
 [19] "EconSansCndRegIta"       "BF Tiny Hand"            "IBM Plex Sans"           "IBM Plex Sans Light"     "IBM Plex Sans Medium"    "IBM Plex Sans Text"     
 [25] "IBM Plex Sans Thin" 

But I still get an error when trying to save to pdf when using theme_ipsum_rc():

extrafont::loadfonts(quiet=TRUE, device='pdf')
g <- iris %>% ggplot(aes(Sepal.Length, Sepal.Width)) + geom_point() + theme_ipsum_rc() + labs(caption='Caption')
g
ggsave('temp.pdf', g)

Error in grid.Call.graphics(C_text, as.graphicsAnnot(x$label), x$x, x$y, : invalid font type

Saving to png works fine.

Somehow the PDF device doesn't see the installed fonts, but after much searching I can't find the solution. Any tips greatly appreciated!

Reproducible example (.Rmd):

---
title: "test"
output: 
  rmdformats::readthedown:
     lightbox: true
     gallery: true
     fig_caption: true
     code_folding: "hide"
     code_download: true
     use_bookdown: true
---

```{r setup, include=FALSE}
knitr::opts_chunk$set(dev=c('png', 'pdf'),  comment="", fig.width=12, echo=T, warning=FALSE, cache=F, message=FALSE,  fig.path='figure/theme_test/')
library(hrbrthemes)
library(tidyverse)
extrafont::loadfonts(quiet=TRUE, device='pdf')
library(ggplot2)
 ```  `

```{r }
g <- iris %>% ggplot(aes(Sepal.Length, Sepal.Width)) + geom_point() + theme_ipsum_rc() + labs(caption='Caption')
g
ggsave('temp.pdf', g)
``` .

> sessionInfo()
R version 3.5.1 (2018-07-02)
Platform: x86_64-apple-darwin15.6.0 (64-bit)
Running under: macOS  10.15.6

Matrix products: default
BLAS: /System/Library/Frameworks/Accelerate.framework/Versions/A/Frameworks/vecLib.framework/Versions/A/libBLAS.dylib
LAPACK: /Library/Frameworks/R.framework/Versions/3.5/Resources/lib/libRlapack.dylib

locale:
[1] en_NZ.UTF-8/en_NZ.UTF-8/en_NZ.UTF-8/C/en_NZ.UTF-8/en_NZ.UTF-8

attached base packages:
[1] stats     graphics  grDevices utils     datasets  methods   base     

other attached packages:
 [1] extrafont_0.17   forcats_0.5.0    stringr_1.4.0    dplyr_1.0.2      purrr_0.3.4      readr_1.3.1      tidyr_1.0.2      tibble_3.0.3     ggplot2_3.3.2   
[10] tidyverse_1.3.0  hrbrthemes_0.8.0

loaded via a namespace (and not attached):
  [1] colorspace_1.4-1     pryr_0.1.4           ellipsis_0.3.1       ggridges_0.5.2       brms_2.12.9          rsconnect_0.8.8      markdown_1.1        
  [8] fs_1.5.0             base64enc_0.1-3      rstudioapi_0.11      farver_2.0.3         rstan_2.19.3         DT_0.15              fansi_0.4.1         
 [15] mvtnorm_1.1-1        lubridate_1.7.4      xml2_1.3.2           bridgesampling_1.0-0 codetools_0.2-15     knitr_1.29           shinythemes_1.1.1   
 [22] bayesplot_1.7.1      projpred_2.0.0       jsonlite_1.7.0       packrat_0.4.9-3      broom_0.5.6          Rttf2pt1_1.3.8       dbplyr_1.4.3        
 [29] shiny_1.4.0.2        httr_1.4.2           compiler_3.5.1       backports_1.1.9      assertthat_0.2.1     Matrix_1.2-14        fastmap_1.0.1       
 [36] cli_2.0.2            later_1.1.0.1        htmltools_0.5.0      prettyunits_1.1.1    tools_3.5.1          igraph_1.2.2         coda_0.19-3         
 [43] gtable_0.3.0         glue_1.4.2           reshape2_1.4.3       Rcpp_1.0.5           cellranger_1.1.0     vctrs_0.3.4          nlme_3.1-137        
 [50] extrafontdb_1.0      crosstalk_1.1.0.1    insight_0.9.1.1      xfun_0.16            ps_1.3.4             summarytools_0.9.6   rvest_0.3.6         
 [57] openxlsx_4.1.4       mime_0.9             miniUI_0.1.1.1       lifecycle_0.2.0      gtools_3.8.1         zoo_1.8-7            scales_1.1.1        
 [64] colourpicker_1.0     hms_0.5.3            promises_1.1.1       Brobdingnag_1.2-6    parallel_3.5.1       inline_0.3.15        shinystan_2.5.0     
 [71] yaml_2.2.1           gridExtra_2.3        pander_0.6.3         gdtools_0.2.1        loo_2.2.0            StanHeaders_2.21.0-5 stringi_1.4.6       
 [78] bayestestR_0.7.2.1   dygraphs_1.1.1.6     checkmate_1.8.5      pkgbuild_1.1.0       zip_2.0.4            rlang_0.4.7          pkgconfig_2.0.3     
 [85] systemfonts_0.1.1    matrixStats_0.56.0   evaluate_0.14        lattice_0.20-35      labeling_0.3         rstantools_2.0.0     rapportools_1.0     
 [92] htmlwidgets_1.5.1    processx_3.4.3       tidyselect_1.1.0     plyr_1.8.6           magrittr_1.5         R6_2.4.1             magick_2.3          
 [99] generics_0.0.2       DBI_1.0.0            withr_2.2.0          haven_2.2.0          pillar_1.4.6         xts_0.12-0           abind_1.4-5         
[106] performance_0.4.8.1  modelr_0.1.6         crayon_1.3.4         rmarkdown_2.1        readxl_1.3.1         grid_3.5.1           callr_3.4.3         
[113] threejs_0.3.1        reprex_0.3.0         digest_0.6.25        xtable_1.8-4         httpuv_1.5.2         RcppParallel_5.0.2   stats4_3.5.1        
[120] munsell_0.5.0        tcltk_3.5.1          shinyjs_1.0  

SOLVED! Using Cairo works:


```{r setup, include=FALSE}
knitr::opts_chunk$set(dev=c('png', 'cairo_pdf'),  comment="", echo=T, warning=FALSE, cache=F, message=FALSE,  fig.path='figure/theme_test/')
library(hrbrthemes)
library(tidyverse)
library(extrafont)
library(Cairo)
#extrafont::loadfonts(quiet=TRUE, device='pdf')
library(ggplot2)
theme_set(theme_ipsum_rc())
``` ...

```{r }
g <- iris %>% ggplot(aes(Sepal.Length, Sepal.Width)) + geom_point()  + labs(caption='Caption')
g
ggsave('temp.pdf', g, dev=Cairo)
``` ...