In Title(...) : No Font Could Be Found for Family "Arial"
Today'due south guest post comes from Winston Chang, a software developer at RStudio — ed .
When it comes to making figures in R, y'all can use any font you lot similar, as long as it's Helvetica, Times, or Courier. Using other fonts that are installed on your reckoner can seem an impossible chore, particularly if y'all want to save the output to PDF.
Fortunately, the extrafont bundle makes this process much easier. With it, y'all can create beautiful, professional-looking results like this:
When using fonts in PDF files, there are two challenges: Offset you must tell R that the font is available to employ. Adjacent, you must embed the font into the PDF file to make it render properly on another reckoner or printer that doesn't already have the font.
Hither's an example of what a PDF using Garamond might look similar when it's not embedded, and printed or viewed on a device that lacks the font. It will substitute some other font in the place of Garamond:
Using extrafont in three easy steps
The first step is to install extrafont, and and then import the fonts from your system into the extrafont database:
Installation
install.packages("extrafont") library(extrafont) font_import()
You may run across some warnings, merely you should be able to ignore them. Afterward the fonts are imported, you can view the available fonts by running fonts()
or fonttable()
:
fonts() ## [1] "Andale Mono" "AppleMyungjo" ## [3] "Arial Black" "Arial" ## [five] "Arial Narrow" "Arial Rounded MT Bold" ## [7] "Arial Unicode MS" "Bangla Sangam MN" ## [9] "Castor Script MT" "Comic Sans MS" ## [xi] "Courier New" "Georgia" ## [xiii] "Gujarati Sangam MN" "Impact" ## ... # This will testify more detailed data about fonts fonttable()
Creating PDF files with fonts
Once you've imported the fonts from your organization to the extrafont database, they must be registered with R every bit existence available for the PDF output device. This must be run one time in each R session where you want to utilise the fonts:
library(extrafont) loadfonts() # If you want to output to .ps files instead of .pdf, use: # loadfonts(device="postscript")
After the fonts are registered with R's PDF device, you can create figures with them. Here's an example using base graphics:
pdf("plot_garamond.pdf", family unit="Garamond", width=four, height=four.five) plot(mtcars$mpg, mtcars$wt, master = "Fuel Efficiency of 32 Cars", xlab = "Weight (x1000 lb)", ylab = "Miles per Gallon") dev.off()
Again, you lot may come across some warnings, but they shouldn't cause any bug.
And with ggplot2:
library(ggplot2) p <- ggplot(mtcars, aes(10=wt, y=mpg)) + geom_point() + ggtitle("Fuel Efficiency of 32 Cars") + xlab("Weight (x1000 lb)") + ylab("Miles per Gallon") + theme_bw() + theme(text=element_text(family unit="Garamond", size=xiv)) ggsave("ggplot_garamond.pdf", p, width=iii.5, height=3.5)
Embedding fonts
Extrafont uses GhostScript, a free PostScript interpreter, to embed the fonts. Yous'll demand to make sure it's installed on your computer (note that GhostScript is not an R package). If yous're using Windows, you'll as well need to tell R where the Ghostscript executable is:
# For Windows - in each session # Accommodate the path to lucifer your installation of Ghostscript Sys.setenv(R_GSCMD = "C:/Program Files/gs/gs9.05/bin/gswin32c.exe")
To embed the fonts, use embed_fonts()
:
# If you lot don't specify 'outfile', information technology volition overwrite the original file embed_fonts("plot_garamond.pdf", outfile="plot_garamond_embed.pdf") embed_fonts("ggplot_garamond.pdf", outfile="ggplot_garamond_embed.pdf")
If you lot have many PDF figures that are existence put into a single PDF document, it can be more infinite-efficient to embed the fonts in the final document, instead of in each effigy. That mode, there will exist a single copy of the font in the document instead of ane for each figure.
Using Reckoner Modernistic (with math symbols)
If you are creating a document with TeX, at that place'southward a good chance that your certificate uses the default font, Estimator Mod. Some R users use TikZ (with the tikzDevice packet) to create figures that employ Computer Modern, to match the appearance of the certificate text.
With extrafont, y'all can use Computer Mod in a PDF figure without using TikZ. This is washed by installing the font bundle chosen fontcm
. (Extrafont supports special font packages that contain a PostScript type i font stored in a particular way.)
font_install("fontcm")
It will ask yous to download the fontcm package from CRAN, and and then information technology will import the font into the extrafont database. Once that's washed, run loadfonts()
to register the fonts with R's PDF device. If you lot run fonts()
, they volition be listed every bit "CM Roman", "CM Sans", and "CM Typewriter".
Now you tin can create PDF figures with these fonts:
# Base graphics pdf("plot_cm.pdf", family unit="CM Roman", width=5.v, height=5) curve(dnorm, from=-3, to=3, main="Normal Distribution") text(x=0, y=0.ane, cex=1.5, expression(italic(y == frac(1, sqrt(2 * pi)) * e ^ {-frac(x^2, ii)} ))) dev.off() embed_fonts("plot_cm.pdf", outfile="plot_cm_embed.pdf")
And with ggplot2:
# ggplot2 exp_text <- "italic(y == frac(1, sqrt(2 * pi)) * e ^ {-frac(x^ii, 2)} )" p <- ggplot(data.frame(ten=c(-three,3)), aes(x=x)) + ggtitle("Normal Distribution") + stat_function(fun=dnorm, geom="line") + theme_bw() + theme(text=element_text(family="CM Roman")) + annotate("text", x=0, y=0.1, label=exp_text, parse=TRUE, family unit="CM Roman") ggsave("ggplot_cm.pdf", p, width=4, tiptop=three.5) embed_fonts("ggplot_cm.pdf", outfile="ggplot_cm_embed.pdf")
Notation that font packages installed this way can merely exist used for PDF and PostScript figures generated with R. They aren't available for on-screen output, nor are they available for use for other programs.
Output to bitmap files (PNG, TIFF)
If y'all output graphics using Linux or Mac, you are probably using the Cairo device or Quartz output devices. With these output devices, system fonts are more often than not available without having to jump through whatsoever of these hoops.
On Windows, however, the default output device, "windows", requires registering fonts to work with it. Ordinarily this is a bit of a chore, simply extrafont makes it like shooting fish in a barrel. Subsequently the fonts take been imported:
loadfonts(device = "win")
This registers the fonts with R'southward windows output device. Then yous tin can brand bitmap figures with system fonts on screen, or saved to a file.
Experimental back up for OTF and TTC files
The released version of extrafont can simply import TTF (TrueType) fonts. Information technology doesn't piece of work with with OTF (OpenType) and TTC (TrueType font collection) files, which are increasingly common. There is an in-development version supports these file formats. It tin can be installed from GitHub, using the devtools package. This requires that yous have a C compiler installed on your system, and the proper R development environment prepare:
library(devtools) install_github("Rttf2pt1", "wch") install_github("extrafont", "wch")
Once it's installed, import the fonts as before:
library(extrafont) font_import()
Back up for these file types is experimental. If yous notice any problems, please report them on the issues page for extrafont:
https://github.com/wch/extrafont/issues
Learning more
For more data, run into:
- extrafont project page
- fontcm project page
- A writeup past Paul Murrell about using symbol fonts with R. (The fontcm package should have intendance of the issues.)
The current version of extrafont is 0.11. Previous version had bug with character spacing when you utilise hyphens and minus signs, simply it should be fixed in the latest version.
Winston Chang graduated in 2012 from Northwestern Academy with a PhD in Psychology. He is now a software programmer for RStudio, working primarily on developing the ggplot2 package for data visualization. He created and maintains the "Cookbook for R" website, and is the writer of the forthcoming book "R Graphics Cookbook", to be published by O'Reilly Media.
The comments to this entry are closed.
Source: https://blog.revolutionanalytics.com/2012/09/how-to-use-your-favorite-fonts-in-r-charts.html
0 Response to "In Title(...) : No Font Could Be Found for Family "Arial""
Post a Comment