Can't change fonts in ggplot/geom_text
I would try"
windowsFonts(Times=windowsFont("TT Times New Roman"))
In doing this your specifying explicitly the Windows Font mapping.
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)
You must import the system fonts using the command:
font_import(paths = NULL, recursive = TRUE, prompt = TRUE,pattern = NULL)