Can't change fonts in ggplot/geom_text

Solution 1:

I would try"

windowsFonts(Times=windowsFont("TT Times New Roman"))

In doing this your specifying explicitly the Windows Font mapping.

Solution 2:

The other answers didn't solve my problem (Windows 10).

The key for my system was to call extrafont::loadfonts(device="win") prior to library(ggplot2).

extrafont::loadfonts(device="win")
#extrafont::fonttable()
#extrafont::font_import("C:/Windows/Fonts/", pattern = "RobotoCondensed")
library(ggplot2)

Common problem with font locations:

I had installed the fonts from a random folder with extrafont::font_import() previously. As such extrafont::fonttable() referenced the files in my C:\Windows\Fonts\ folder. To fix this I reset my extrafonts::fonttable() with install.packages("extrafontdb") in order to clear a reference to the fonts in a different location.

Edit regarding saving:

Deeper down the rabbit hole. Saving was an additional challenge. In order to extrafont::loadfonts(device="pdf") I had to make sure no fonts in my extrafont::fonttable() had identical family names and bold/italic status. I edited extrafont:::fonttable_file() to resolve any duplicate bold/italic fonts within my family. Using Roboto Condensed I renamed the light fonts' font family to "Roboto Condensed Light".

Saving with ggsave(device="pdf") then worked. Opening the files in acrobat the fonts did not display correctly. I tried embedding the fonts with ghostscript as well as using the cairo_pdf device. The easiest and most functional solution was to open the .pdf files in Illustrator (the fonts display fine there) and immediately re-save them again as .pdf.

Edit 2 regarding saving:

Saving as .eps was the only way to preserve the file in both illustrator and acrobat. The result is perfect. ggsave(g, file="Figure.eps", fonts=c("FONT FAMILIES USED", "Roboto Condensed", "Roboto Condensed Light"))

Final plotting code:

Here is my final set of calls I use before plotting. Comments are setup commands that need to be run once only.

# Plotting
extrafont::loadfonts(device="pdf")
extrafont::loadfonts(device="postscript")
# extrafont::font_import("C:/Windows/Fonts/", pattern = "RobotoCondensed", prompt = F)
# extrafont::fonttable()
# C:/Program Files/R/R-3.3.1/library/extrafontdb/fontmap/ - Change lights to "Roboto Condensed Light"
# After ggsave(device="pdf") or ggsave(device="eps") open and resave the file in Illustrator
library(hrbrthemes)
library(ggplot2)

Solution 3:

You must import the system fonts using the command:

font_import(paths = NULL, recursive = TRUE, prompt = TRUE,pattern = NULL)

Solution 4:

I tried the different solutions here but none worked for me (win10, R 3.4.3). This is what worked for me:

install.packages("extrafont")
library(extrafont)
loadfonts(device = "win")

It did not matter if I did it before or after library(ggplot2)

Sources:

  1. https://cran.r-project.org/web/packages/extrafont/extrafont.pdf
  2. Changing fonts in ggplot2