ProductPromotion
Logo

R Programming

made by https://0x3d.site

GitHub - wch/extrafont: Tools for using fonts in R graphics
Tools for using fonts in R graphics. Contribute to wch/extrafont development by creating an account on GitHub.
Visit Site

GitHub - wch/extrafont: Tools for using fonts in R graphics

GitHub - wch/extrafont: Tools for using fonts in R graphics

extrafont

The extrafont package makes it easier to use fonts other than the basic PostScript fonts that R uses. Fonts that are imported into extrafont can be used with PDF or PostScript output files. On Windows, extrafont will also make system fonts available for bitmap output.

There are two hurdles for using fonts in PDF (or Postscript) output files:

  • Making R aware of the font and the dimensions of the characters.
  • Embedding the fonts in the PDF file so that the PDF can be displayed properly on a device that doesn't have the font. This is usually needed if you want to print the PDF file or share it with others.

The extrafont package makes both of these things easier.

Presently it allows the use of TrueType fonts with R, and installation of special font packages. Support for other kinds of fonts will be added in the future. It has been tested on Mac OS X 10.7 and Ubuntu Linux 12.04 and Windows XP.

The instructions below are written for PDF files, although the information also applies to PostScript files.

If you want to use the TeX Computer Modern fonts in PDF files, also see the fontcm package.

Using extrafont

Requirements

You must have Ghostscript installed on your system for embedding fonts into PDF files.

Extrafont requires the extrafontdb package to be installed. extrafontdb contains the font database, while this package contains the code to install fonts and register them in the database.

It also requires the Rttf2pt1 package to be installed. Rttf2pt1 contains the ttf2pt1 program which is used to read and manipulate TrueType fonts. It is in a separate package for licensing reasons.

Install extrafont from CRAN will automatically install extrafontdb and Rttf2pt1:

install.packages('extrafont')
library(extrafont)

To use extrafont in making graphs, you'll need to do the following:

  • Import fonts into the extrafont database. (Needs to be done once)
  • Register the fonts from the extrafont database with R's PDF (or PostScript) output device. (Needs to be done once per R session)
  • Create the graphics that use the fonts.
  • Embed the fonts into the PDF file. (Needs to be done for each file)

Import fonts into the extrafont database

First, import the fonts installed on the system. (This only works with TrueType fonts right now.)

font_import()
# This tries to autodetect the directory containing the TrueType fonts.
# If it fails on your system, please let me know.

This does the following:

  • Finds the fonts on your system.
  • Extracts the FontName (like ArialNarrow-BoldItalic).
  • Extracts/converts a PostScript .afm file for each font. This file contains the font metrics, which are the rectangular dimensions of each character that are needed for placement of the characters. These are not the glyphs, which the curves defining the visual shape of each character. The glyphs are only in the .ttf file.
  • Scan all the resulting .afm files, and save a table with information about them. This table will be used when making plots with R.
  • Creates a file Fontmap, which contains the mapping from FontName to the .ttf file. This is required by Ghostscript for embedding fonts.

You can view the resulting table of font information with:

# Vector of font family names
fonts()

# Show entire table
fonttable()

If you install new fonts on your computer, you'll have to run font_import() again.

Register the fonts with the PDF output device

The next step is to register the fonts in the afm table with R's PDF (or PostScript) output device. This is needed to create PDF files with the fonts. As of extrafont version 0.13, this must be run only in the first session when you import your fonts. In sessions started after the fonts have been imported, simply loading the package with library(extrafont) this step isn't necessary, since it will automatically register the fonts with R.

# Only necessary in session where you ran font_import()
loadfonts()
# For PostScript output, use loadfonts(device="postscript")
# Suppress output with loadfonts(quiet=TRUE)

Create figures with the fonts

Here's an example of PDFs made with base graphics and with ggplot2. These examples use the font Impact, which should be available on Windows and Mac. (Use fonts() to see what fonts are available on your system)

pdf("font_plot.pdf", family="Impact", width=4, height=4)
plot(mtcars$mpg, mtcars$wt, 
     main = "Fuel Efficiency of 32 Cars",
     xlab = "Weight (x1000 lb)",
     ylab = "Miles per Gallon")
dev.off()


library(ggplot2)
p <- ggplot(mtcars, aes(x=wt, y=mpg)) + geom_point() +
  ggtitle("Fuel Efficiency of 32 Cars") +
  xlab("Weight (x1000 lb)") + ylab("Miles per Gallon") +
  theme(text=element_text(size=16, family="Impact"))

ggsave("font_ggplot.pdf", plot=p,  width=4, height=4)

The first time you use a font, it may throw some warnings about unknown characters. This should be harmless, but if it causes any problems, please report them.

Embed the fonts

After you create a PDF output file, you should embed the fonts into the file. There are 14 PostScript base fonts never need to be embedded, because they are included with every PDF/PostScript renderer. All other fonts should be embedded into the PDF files.

First, if you are running Windows, you may need to tell it where the Ghostscript program is, for embedding fonts. (See Windows installation notes below.)

# Needed only on Windows - run once per R session
# Adjust the path to match your installation of Ghostscript
Sys.setenv(R_GSCMD = "C:/Program Files/gs/gs9.05/bin/gswin32c.exe")

As the name suggests, embed_fonts() will embed the fonts:

embed_fonts("font_plot.pdf", outfile="font_plot_embed.pdf")
embed_fonts("font_ggplot.pdf", outfile="font_ggplot_embed.pdf")
# If outfile is not specified, it will overwrite the original file

To check if the fonts have been properly embedded, open each of the PDF files with Adobe Reader, and go to File->Properties->Fonts. If a font is embedded, it will say "Embedded Subset" by the font's name; otherwise it will say nothing next to the name.

With Adobe Reader, if a font is not embedded, it will be substituted by another font. This provides a way to see what your PDF will look like on printer or computer that doesn't have the font installed. Other PDF viewers may behave differently. For example, the Preview application on Mac OS X will automatically use system fonts to display non-embedded fonts -- this makes it impossible to tell whether the font is embedded in the PDF.

On Linux you can also use evince (the default PDF viewer) to view embedded fonts. Open the file and go to File->Properties->Fonts. If a font is embedded, it will say "Embedded subset"; otherwise it will say "Not embedded".

If you are putting multiple PDF figures into a single document, it is more space-efficient to not embed fonts in each figure, but instead embed the font in the final PDF document.

Windows bitmap output

extrafont also makes it easier to use fonts in Windows for on-screen or bitmap output.

# Register fonts for Windows bitmap output
loadfonts(device="win")

ggplot(mtcars, aes(x=wt, y=mpg)) + geom_point() +
  ggtitle("Title text goes here") +
  theme(plot.title = element_text(size = 16, family="Georgia", face="italic"))

ggsave("fonttest-win.png")

Since the output is a bitmap file, there's no need to embed the fonts.

Font packages

Extrafont supports font packages, which contain fonts that are packaged in a particular way so that they can be imported into extrafont. These fonts are installed as R packages; they are not installed for the computer operating system. Fonts that are installed this way will be available only for PDF or PostScript output. They will not be available for on-screen or bitmap output, which requires that the font be installed for operating system, not just with R and extrafont.

Presently extrafont supports only font packages with PostScript Type 1 fonts.

See the fontcm package containing Computer Modern fonts for an example.


Installation notes

Rttf2pt1

The source code for the utility program ttf2pt1 is in the package Rttf2pt1. CRAN has pre-compiled Windows and Mac OS X binaries. For other platforms, and when installing from source, it will be compiled on installation, so you need a build environment on your system.

Windows installation notes

In Windows, you need to make sure that Ghostscript is installed.

In each R session where you embed fonts, you will need to tell R where Ghostscript is installed. For example, when Ghostscript 9.05 is installed to the default location, running this command will do it (adjust the path for your installation):

Sys.setenv(R_GSCMD="C:/Program Files/gs/gs9.05/bin/gswin32c.exe")

Resetting the font database

To reset the extrafont database, reinstall the extrafontdb package:

install.packages("extrafontdb")

More Resources
to explore the angular.

mail [email protected] to add your project or resources here ๐Ÿ”ฅ.

Related Articles
to learn about angular.

FAQ's
to learn more about Angular JS.

mail [email protected] to add more queries here ๐Ÿ”.

More Sites
to check out once you're finished browsing here.

0x3d
https://www.0x3d.site/
0x3d is designed for aggregating information.
NodeJS
https://nodejs.0x3d.site/
NodeJS Online Directory
Cross Platform
https://cross-platform.0x3d.site/
Cross Platform Online Directory
Open Source
https://open-source.0x3d.site/
Open Source Online Directory
Analytics
https://analytics.0x3d.site/
Analytics Online Directory
JavaScript
https://javascript.0x3d.site/
JavaScript Online Directory
GoLang
https://golang.0x3d.site/
GoLang Online Directory
Python
https://python.0x3d.site/
Python Online Directory
Swift
https://swift.0x3d.site/
Swift Online Directory
Rust
https://rust.0x3d.site/
Rust Online Directory
Scala
https://scala.0x3d.site/
Scala Online Directory
Ruby
https://ruby.0x3d.site/
Ruby Online Directory
Clojure
https://clojure.0x3d.site/
Clojure Online Directory
Elixir
https://elixir.0x3d.site/
Elixir Online Directory
Elm
https://elm.0x3d.site/
Elm Online Directory
Lua
https://lua.0x3d.site/
Lua Online Directory
C Programming
https://c-programming.0x3d.site/
C Programming Online Directory
C++ Programming
https://cpp-programming.0x3d.site/
C++ Programming Online Directory
R Programming
https://r-programming.0x3d.site/
R Programming Online Directory
Perl
https://perl.0x3d.site/
Perl Online Directory
Java
https://java.0x3d.site/
Java Online Directory
Kotlin
https://kotlin.0x3d.site/
Kotlin Online Directory
PHP
https://php.0x3d.site/
PHP Online Directory
React JS
https://react.0x3d.site/
React JS Online Directory
Angular
https://angular.0x3d.site/
Angular JS Online Directory