Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to embed Ubuntu Condensed in Rmarkdown PDF files?

Tags:

r

fonts

ggplot2

I'm struggling with a persistent issue while trying to embed the Ubuntu Condensed font in ggplot2 plots in R Markdown PDF outputs.

  • I've installed both Ubuntu and Ubuntu Condensed fonts for all users on my Windows machine

  • I've verified both fonts are listed under systemfonts::system_fonts() and extrafont::fonttable()

  • I've registered both fonts with Windows bitmap devices

  • I'm using AGG as the backend graphics device

  • Within RStudio's viewer pane, the plot is displayed properly with both fonts working:

plot inside RStudio's viewer pane

enter image description here

  • When generating a PDF from an R Markdown document Ubuntu works fine, but Ubuntu Condensed completely fails to embed:

plot rendered in PDF

enter image description here

  • I managed to get it working with showtext but I would like to avoid using showtext if possible

Here's my current setup:

knitr::opts_chunk$set(
  dev = "cairo_pdf",
    echo = FALSE,
    message = FALSE,
    warning = FALSE
)

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

loadfonts()

grDevices::windowsFont("Ubuntu")
grDevices::windowsFont("Ubuntu Condensed")
ggplot(mtcars, aes(x=wt, y=mpg)) + 
  geom_point() +
  labs(title = "This is Ubuntu", x = "This should be Ubuntu Condensed", 
       y = "This should be Ubuntu Condensed") +
  theme(plot.title=element_textbox_simple(family="Ubuntu"),
        axis.title = element_text(family = "Ubuntu Condensed"),
        axis.text = element_text(family = "Ubuntu Condensed"))

And here's my session info:

R version 4.4.2 (2024-10-31 ucrt)
Platform: x86_64-w64-mingw32/x64
Running under: Windows 11 x64 (build 22631)

Matrix products: default


locale:
[1] LC_COLLATE=Czech_Czechia.utf8  LC_CTYPE=Czech_Czechia.utf8    LC_MONETARY=Czech_Czechia.utf8
[4] LC_NUMERIC=C                   LC_TIME=Czech_Czechia.utf8    

time zone: Europe/Prague
tzcode source: internal

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

other attached packages:
[1] systemfonts_1.2.1 extrafont_0.19    ggtext_0.1.2      ggplot2_3.5.1    

loaded via a namespace (and not attached):
 [1] vctrs_0.6.5       cli_3.6.3         knitr_1.49        rlang_1.1.5       xfun_0.50        
 [6] stringi_1.8.4     Rttf2pt1_1.3.8    generics_0.1.3    textshaping_1.0.0 labeling_0.4.3   
[11] glue_1.8.0        colorspace_2.1-1  markdown_1.13     extrafontdb_1.0   ragg_1.3.3.9000  
[16] gridtext_0.1.5    scales_1.3.0      grid_4.4.2        munsell_0.5.1     evaluate_1.0.3   
[21] tibble_3.2.1      lifecycle_1.0.4   stringr_1.5.1     compiler_4.4.2    dplyr_1.1.4      
[26] Rcpp_1.0.14       pkgconfig_2.0.3   rstudioapi_0.17.1 farver_2.1.2      R6_2.5.1         
[31] tidyselect_1.2.1  pillar_1.10.1     commonmark_1.9.2  magrittr_2.0.3    tools_4.4.2      
[36] withr_3.0.2       gtable_0.3.6      xml2_1.3.6       

UPDATE Both fonts are embedded correctly with R version 4.3.3, so I assume the problem is caused by some change from R 4.3.3 to R 4.4.0.

like image 790
Kateřina Hudáčová Avatar asked Nov 21 '25 21:11

Kateřina Hudáčová


1 Answers

I downloaded the two Ubuntu fonts here. Double click both files and then click "Install" to register them properly on windows.

With registered fonts

Instead of manually, you can also register fonts with

font_files <- list.files("/path/to/your/fonts", pattern = "\\.ttf$", full.names = TRUE)

for (file in font_files) {
  font_name <- tools::file_path_sans_ext(basename(file))  # Extract font name from file name
  register_font(name = font_name, plain = file)  # Register each font
}

But always make sure, that the fonts are listed in font_families().

Be sure, to run your ggplot in a chunk that is NOT setup. Also set dev to "ragg_png" which is a great hint I found here.

out

The viewport when put to AGG displays both fonts the same as the PDF:

viewport

Code

---
title: "Font Embedding Example with dev = 'ragg_png' and registered Fonts"
output:
  pdf_document: default
  html_document:
    df_print: paged
---

```{r setup, echo=F, warning=F}
knitr::opts_chunk$set(
  dev = "ragg_png", 
  echo = FALSE,
  message = FALSE,
  warning = FALSE,
  dpi = 400
)

```    

```{r extraFont, echo=F, warning=F}
library(ggplot2); library(ggtext)

ggplot(mtcars, aes(x=wt, y=mpg)) + 
  geom_point() +
  labs(title = "This is Ubuntu", x = "This should be Ubuntu Condensed", 
       y = "This should be Ubuntu Condensed") +
  theme(plot.title=element_textbox_simple(family="Ubuntu"),
        axis.title = element_text(family = "Ubuntu Condensed"),
        axis.text = element_text(family = "Ubuntu Condensed"))
```

With showtext_auto

Another way is using showtext_auto() out

Code

---
title: "Font Embedding Example"
output: 
  pdf_document
---

```{r showtext, echo=F, warning=F}

library(showtext);library(ggplot2);library(ggtext)
    
font_add("Ubuntu", regular = "Ubuntu-Regular.ttf")
font_add("Ubuntu Condensed", regular = "UbuntuCondensed-Regular.ttf")

# Enable showtext for this specific plot only
showtext_auto()

ggplot(mtcars, aes(x=wt, y=mpg)) + 
  geom_point() +
  labs(title = "This is Ubuntu", x = "This should be Ubuntu Condensed", 
       y = "This should be Ubuntu Condensed") +
  theme(plot.title=element_textbox_simple(family="Ubuntu"),
        axis.title = element_text(family = "Ubuntu Condensed"),
        axis.text = element_text(family = "Ubuntu Condensed"))

showtext_auto(FALSE)
```
like image 51
Tim G Avatar answered Nov 24 '25 10:11

Tim G



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!