Using google fonts in markdown (and distill) ggplots and showtext

I regularly use rmarkdown (and now distill) to do data investigations to show to clients.

I have in previous jobs 'branded' my output in various ways, particularly using fonts stored locally to my machine to generate PDFs. I am in a new company, where the corporate 'font' is in fact a google font. I am also not restricted (and happily empowered) to generate HTML output instead of PDF, however this has disrupted my workflow slightly.

I had previously used showtext to apply my fonts to ggplot2 in PDF outputs, however I am starting to expect this is not supported for all HTML outputs.

I can successfully generate an html_notebook output type, I believe because the plot assets are generated in the same session as I'm working in in the IDE, and the notebook output is updated form that environment on a save operation.

However, I am not successfully generating HTML documents that are html_document, or distill::distill_article. I believe this is because these documents get generated in a separate (child?) session of R, which seems not to behave with showtext?

Has anyone successfully generated ggplots with showtext in either of these cases? Has anyone found an alternative approach to formatting ggplots in these cases? Has anyone got an alternative approach (even not ggplot! :exploding_head:) to formatting chart fonts in these cases?

---
title: "Showtext example"
output: html_document
---

Change html_document to html_notebook and distill::distill_article 

```{r setup, include=FALSE}
knitr::opts_chunk$set(echo = TRUE)

library(showtext)
library(ggplot2)

font_add_google(name = "Quicksand", family = "Quicksand", regular.wt = 400, bold.wt = 700)
showtext.auto()

theme_set(theme_minimal() + theme(text = element_text(family = "Quicksand")))

df <- data.frame(
  gp = factor(rep(letters[1:3], each = 10)),
  y = rnorm(30)
)
ggplot(data = df, aes(x = gp, y = y)) +
  geom_point()

My error in non-html_notebook files includes no font could be found for family "Quicksand"

If the font is installed in your system you can use extrafont package

library(extrafont)
font_import() # You just have to run this once to import all the fonts present in your system
loadfonts() # If you are on a windows system add the device = "win" parameter
# Then you could add your font to your ggplots
theme(text=element_text(family="Quicksand"))
2 Likes

I had in fact missed that download button on the google fonts site :face_with_hand_over_mouth:, that said, it makes my work pretty unportable as the the font needs to exist on their system too.

I'll absolutely start doing this, though I am still very interested if there is a way to reference the font source via code? (though admittedly, the direct business solution is solved!)

This is what web fonts are. You can include a font as part of a website (either hosted or in the site itself), and then reference it. So, if you have a google webfont, you can include that in your header and, if client side allows it, the font will be part of the site itself.

@mara Didn't @andresrcs suggest downloading and using locally though? this bypasses the part where it continues to be a webfont, and becomes a local font, therefore removing this benefit?

Yeah, I thought you were talking about this for the purposes of displaying as HTML. If you have the font locally, it will work either way, but if you wanted to have a specific font embedded, you could do it with a web font.

1 Like

This topic was automatically closed 7 days after the last reply. New replies are no longer allowed.

If you have a query related to it or one of the replies, start a new topic and refer back with a link.