Specifying Fonts in R (2024)

Paul Murrell
Department of Statistics
The University of Auckland
paul@stat.auckland.ac.nz

This document discusses how R allows fonts to be specified. This is within the context of R's graphics engine -- graphicssystems, such as base graphics and grid can obviously implementtheir own interfaces, but the engine capabilities will limit whatthey are able to achieve.

Currently (R 1.5.0), the following font characteristics may be set:

ps The font pointsize
cex The "character expansion" (text size = ps*cex)
font The font "face" (1=plain, 2=bold, 3=italic, 4=bold-italic)

The base graphics interface provides some extensions on this, basically allowing for multiple cex and font settings to exist for differentpurposes (e.g., axis labels and titles).

One problem with this font specification is that the graphics enginedoes not provide an easy way to specify a font "family". The windows device gets around this by allowing higher values of the font "face" to be used to map into atext file of different fonts, but this is not a terribly clean orconvenient mechanism.

Another problem is that there is no way to specify a change ininter-line spacing of text (for drawing a single piece of multi-linetext, e.g., "one\ntwo\nthree lines")

Specifications in other systems

Unfortunately, there exists no well-defined and universally accepted taxonomy for classifying fonts based on their names, and terms that apply to one font family name may not be appropriate for others.
(from W3C CSS2 specification)

In the W3C CSS2 specification, the following font characteristics are allowed: Font family, Font style (normal or italic), Font variant (normalor smallcaps), Font weight (normal or bold), Font stretch (sort of like cex?),and Font size.

In the GNU plotting utilities fonts are specified by family and face, whereface is a number basically like R's "font", with values higher than 4 indicating othernonstandard variations on a font family (only used for Hershey fonts).

In the Java 2 SDK version 1.4 the TextAttribute class has fields for family,weight (bold), width (like the Font stretch in CSS2), posture (oblique),and size.

Proposal

What I want to do is add family and lineheight characteristicsto R's font specification.

I can take care of the changes to base graphics, grid, and R's graphics engine,but some general issues are: impact on users (R-level API and C-level API), and impact on device maintainers. The device maintainers would eitherhave to make some changes or trust me to make them.

R-level changes

This can be quite minimal.

For lineheight, this can just be ignored bybase graphics for now. Even if added, it would just be an additionalpar() value with no impact on existing code.

For family, allow the current font=number to remain,but allow as an alternative font=list(family=string, face=numberorstring). Again, no impact onexisting user code.

The functions strwidth and strheightneed to have arguments added to allow the specification of the fontfor the text being sized. (This doesn't need to happen in grid because "strwidth" and "strheight"units are always relative to the current font settings.)

C-level changes

The new lineheight and font family information has to be passed from graphics systems to the graphics engine and from the graphics engine to the graphics devices.In other words, there need to be changes to GraphicsEngine.h and GraphicsDevice.hHere are two options for doing this:

  1. Simply add additional lineheight and family arguments (and possiblyrename font argument to "face") to relevant graphical primitives (e.g., GEText and dev_text).

    For devices this would only require modifying theargument list for a couple of functions and possibly renaming"font" to "face". i.e., treat the new face argument exactly likethe old font argument. Lineheight would only be used in the graphicsengine. Family could just be ignored until you feel like doing anythingabout it (although maybe could add a warning if given a non-empty family value).

  2. Change the way graphical arguments are passed to graphics engine andgraphics devices more substantially; create a new gpar structure(an SEXP?) within which all graphical arguments can be passed andreplace primitive-specific argument lists with a single gpar ptr argument. All graphics primitives get all graphical arguments andjust use the ones they are interested in.

    For devices, this would meana change in argument list and extra code to extract graphical parameters from the generic gpar structure. i.e., more work.

    The advantage would be that any future additions to the listof graphical parameters would not affect theAPI. Also, passing a single gpar ptr would be more efficient thanthe current practice of passing multiple graphical arguments.

Examples

Here's some concrete examples to show what the changes might look liketo the user:
text(1, 1, "Works just like it does now", font=2)text(1, 1, "Same effect as above", font=list(face="italic"))text(1, 1, "Same effect as above", font=list(face=2))text(1, 1, "Change the font just for this text", font=list(family="Helvetica", face="bold-italic"))text(1:4, 1:4, paste("Font face", 1:4, "for this text"), font=list(family="Courier New", face=1:4))

Advantages

  • There would be potential for devices to support changes in font family on-the-fly.
  • Hershey vector fonts could be handled within the same mechanism as"normal" fonts. This could lead todeprecation of vfont arguments in text() et al, andno need for introduction of extra vfont arguments for grid.Could also lead to use of Hershey vector fonts in mathematicalannotation, which would allow mathematical annotation onALL devices.

Downsides

  • Possibly some work for device maintainers (at least for gtkDevice).
  • External packages which depend on new graphics API or device APIwould have to be recompiled and versions which run on 1.5.0 would not run on 1.6.0.This includes grid (=> lattice) and gtkDevice to my knowledge. On theother hand, this is already the case due to other changes.
Specifying Fonts in R (2024)

FAQs

How do I use custom fonts in R? ›

Save Custom Fonts in R package
  1. Install Internal Package.
  2. Run Function called: setup_font() ( Or whatever name we assign)
  3. This function now goes through the files inside the R Package, look for the fonts, then install the library extrafont if necessary.
Dec 18, 2021

How do you code a font style? ›

To change font type purely with HTML, use the CSS font-family property. Set it to the value you want and place it inside a style attribute. Then add this style attribute to an HTML element, like a paragraph, heading, button, or span tag. This approach to changing font type in HTML uses the style language CSS.

Where does R look for fonts? ›

Default search paths will be assigned when package is loaded: For Windows, it is %windir%\Fonts , usually expanded into C:\Windows\Fonts. For Mac OS, default paths are /Library/Fonts and ~/Library/Fonts and their subdirectories. For Linux and other Unix-like OS, /usr/share/fonts , /usr/local/share/fonts , ~/.

How do you write bold in R? ›

To make the formatted text into bold type, you can simply use a pair of ** around the marked up text with no space.

How do I add a font to RStudio editor? ›

Install fonts
  1. Open up RStudio, if you haven't already.
  2. Navigate to Tools > Global Options > Appearance.
  3. Use the Editor Font box to select Fira Code iScript.
Dec 18, 2018

How to install system fonts in R? ›

Table of contents
  1. Choose a font.
  2. Check {systemfonts} is installed.
  3. Check the font you want to use is installed.
  4. Set your graphics device to AGG.
  5. Check everything works when you ggsave()
  6. In . Rmd and . qmd files, also set the graphics device within the file!
  7. Register font variants.
  8. Additional resources.
Jan 12, 2024

What is code font type? ›

The font typically used for software development is called a monospace font. In this font, every character takes up the same amount of space. This includes any letters and numbers including i and 1 , and even blank spaces made with the spacebar.

Can you change font in R? ›

showtext and ragg are two R packages that help to work with custom fonts in R and ggplot2 . ragg provides graphic devices based on the AGG library, which gives direct access to all system fonts, making the usage of custom fonts painless and easy.

What is the system font package in R? ›

The 'systemfonts' package provides bindings to the native libraries on Windows, macOS and Linux for finding font files that can then be used further by e.g. graphic devices. The main use is intended to be from compiled code but 'systemfonts' also provides access from R.

What are the default font families in R? ›

Default mappings are provided for three device-independent family names: "sans" for a sans-serif font (to "Helvetica" ), "serif" for a serif font (to "Times" ) and "mono" for a monospaced font (to "Courier" ).

How do you bold and italicize in R? ›

Formatting Text

To write text in italic font, use a single underscore or asterix before and after the text. To write text in bold font, use a double asterix or underscores before and after the text.

How do you add italics in R? ›

If we want to apply italics to many annotations in our plot, bquote() and as. expression() are the way to go. For example, this is useful when the row labels of a heatmap show gene names, which we can italicize easily with just a bit of code. Our function basically wrapped the gene names, the strings, with italic().

How to format code in R? ›

Reformat R scripts

Use the following shortcuts in RStudio to reformat your code: Ctrl + I - Fixes line indentations. Ctrl + Shift + A - Does a complete reformat of the selected part of a code.

How do I import my own font? ›

Add a font
  1. Download the font files. ...
  2. If the font files are zipped, unzip them by right-clicking the .zip folder and then clicking Extract. ...
  3. Right-click the fonts you want, and click Install.
  4. If you're prompted to allow the program to make changes to your computer, and if you trust the source of the font, click Yes.

How do I change the font style in R? ›

In this approach to change the font of the given plot, the user needs to call the windowsFont() which is one of the in-build function of the R programming language, with the name of the font as its parameter, this function is used to specify the font family as per requirement and with this, the user also need to use ...

References

Top Articles
Latest Posts
Article information

Author: Errol Quitzon

Last Updated:

Views: 6423

Rating: 4.9 / 5 (79 voted)

Reviews: 86% of readers found this page helpful

Author information

Name: Errol Quitzon

Birthday: 1993-04-02

Address: 70604 Haley Lane, Port Weldonside, TN 99233-0942

Phone: +9665282866296

Job: Product Retail Agent

Hobby: Computer programming, Horseback riding, Hooping, Dance, Ice skating, Backpacking, Rafting

Introduction: My name is Errol Quitzon, I am a fair, cute, fancy, clean, attractive, sparkling, kind person who loves writing and wants to share my knowledge and understanding with you.