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()
andextrafont::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
- When generating a PDF from an R Markdown document Ubuntu works fine, but Ubuntu Condensed completely fails to embed:
plot rendered in PDF
- I managed to get it working with
showtext
but I would like to avoid usingshowtext
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.
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()
andextrafont::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
- When generating a PDF from an R Markdown document Ubuntu works fine, but Ubuntu Condensed completely fails to embed:
plot rendered in PDF
- I managed to get it working with
showtext
but I would like to avoid usingshowtext
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.
Share Improve this question edited 18 hours ago Kateřina Hudáčová asked 2 days ago Kateřina HudáčováKateřina Hudáčová 411 silver badge3 bronze badges New contributor Kateřina Hudáčová is a new contributor to this site. Take care in asking for clarification, commenting, and answering. Check out our Code of Conduct. 3 |1 Answer
Reset to default 1I 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.
The viewport when put to AGG displays both fonts the same as the PDF:
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()
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)
```
dev = "ragg_png"
and make sure, your fonts are properly registered by runningfont_families()
. May I ask, why you want to avoidshowtext
? It's a great package! – dog Commented yesterdayragg_png
does work indeed, but I prefer a vector graphics solution. I'm using showtext_auto provisionally—it is a great package, but it doesn't fit into the company workflow. So what puzzles me is that with my original workflow Ubuntu (regular) works fine, and Ubuntu Condensed used to work in previous R versions. I just tested it in R 4.0.0, and it worked perfectly, so I'm trying to figure out what exactly changed in recent versions. – Kateřina Hudáčová Commented 22 hours ago