52

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?

Joschi
  • 2,941
  • 9
  • 28
  • 36
  • 6
    does it work if you try `windowsFonts(Times=windowsFont("TT Times New Roman"))` first? – user1317221_G Feb 06 '13 at 16:24
  • 1
    yes it does. thank you. you should post it as an answer. Can you explain it? And do I have to do this in all scripts when I want to use diffrent fonts? – Joschi Feb 06 '13 at 21:35
  • I'm on mac just now so I can't really fiddle around but [this](https://groups.google.com/forum/?fromgroups=#!topic/ggplot2/781cT5yMqCg) might help but mainly the idea of this is once you have installed the fonts you give them to R to use explicitly – user1317221_G Feb 06 '13 at 22:29
  • You can bulk import your fonts with package {extrafont} as explained in this article: http://blog.revolutionanalytics.com/2012/09/how-to-use-your-favorite-fonts-in-r-charts.html – Brandon Loudermilk Mar 02 '17 at 16:10

4 Answers4

40

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)
nate
  • 927
  • 1
  • 7
  • 19
  • 2
    It may seem obvious, but it's worth adding: you'll want to make sure you have fonts loaded by executing `extrafont::font_import()` before attempting to load any fonts. – Jared Wilber May 04 '18 at 00:29
38

I would try"

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

In doing this your specifying explicitly the Windows Font mapping.

user1317221_G
  • 15,087
  • 3
  • 52
  • 78
20

You must import the system fonts using the command:

font_import(paths = NULL, recursive = TRUE, prompt = TRUE,pattern = NULL)
MSU_Bulldog
  • 3,501
  • 5
  • 37
  • 73
user7190527
  • 201
  • 2
  • 2
  • 1
    This is actually the real _answer_ to the question. – skoh Jan 26 '17 at 15:24
  • 7
    If you have a lot of fonts, this solution will take a long time. Use `pattern="Times"` or something to reduce the number of fonts loaded – C8H10N4O2 Mar 09 '17 at 01:59
  • Is this a one time operation or something you must run every time? Looks like it might be one time to me – Akhil Nair Jan 07 '21 at 16:22
3

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
anakar
  • 316
  • 2
  • 13