Joschi
Joschi

Reputation: 3039

Can't change fonts in ggplot/geom_text

I can't set my fonts in geom_text. Here is what I tried:

    labels_test<-data.frame(a=c("a","b","c"),b=c(1:3),c=c(3:1))
    # works
    ggplot () + geom_text(data=labels_test,aes(b,c,label=a),color="blue")
    # does not work: 
    ggplot () + geom_text(data=labels_test,aes(b,c,label=a),color="blue",family="Times")
    # error message:  In grid.Call.graphics(L_text, as.graphicsAnnot(x$label), x$x, x$y,:
    # Font family not found in Windows font database

I already imported all fonts as indicated here. Any ideas what is still going wrong?

Upvotes: 53

Views: 56174

Answers (4)

anakar
anakar

Reputation: 356

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

Upvotes: 3

nate
nate

Reputation: 977

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)

Upvotes: 41

user7190527
user7190527

Reputation: 201

You must import the system fonts using the command:

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

Upvotes: 20

user1317221_G
user1317221_G

Reputation: 15441

I would try"

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

In doing this your specifying explicitly the Windows Font mapping.

Upvotes: 38

Related Questions