Using system and web fonts in R plots

[This article was first published on mages' blog, and kindly contributed to R-bloggers]. (You can report issue about the content on this page here)
Want to share your content on R-bloggers? click here if you have a blog, or here if you don't.

The forthcoming R Journal has an interesting article on the showtext package by Yixuan Qiu. The package allows me to use system and web fonts directly in R plots, reminding me a little of the approach taken by XeLaTeX. But “unlike other methods to embed fonts into graphics, showtext converts text into raster images or polygons, and then adds them to the plot canvas. This method produces platform-independent image files that do not rely on the fonts that create them.” [1]

Here is an example with fonts from my local system:

library(showtext)<br />png("System-Fonts.png", width=550, height=350);<br />par(mfrow=c(2,2))<br />plot(1 ~ 1, main="Lucida Bright", family = "Lucida Bright")<br />plot(1 ~ 1, main="Courier", family = "Courier")<br />plot(1 ~ 1, main="Helvetica Neue Light", family = "Helvetica Neue Light") <br />plot(1 ~ 1, main="Lucida Handwriting Italic", family = "Lucida Handwriting Italic")<br />dev.off()
Additionally showtext allows me to use fonts hosted online, e.g. Google web fonts:

font.add.google("Alegreya Sans", "aleg");<br />font.add.google("Permanent Marker", "marker")<br />font.add.google("Gruppo", "gruppo")<br />font.add.google("Lobster", "lobster")<br />png("Google-Fonts.png", width=550, height=350)<br />showtext.begin()<br />par(mfrow=c(2,2))<br />plot(1 ~ 1, main="Alegreya Sans", family = "aleg")<br />plot(1 ~ 1, main="Permanent Marker", family = "marker")<br />plot(1 ~ 1, main="Gruppo", family = "gruppo") <br />plot(1 ~ 1, main="Lobster", family = "lobster") <br />showtext.end()<br />dev.off()

For more information read the article and/or visit the project site.

References

[1] Yixuan Qiu. showtext: Using System Fonts in R Graphics. The R Journal, 7(1), 2015.

Session Info

R version 3.2.0 (2015-04-16)<br />Platform: x86_64-apple-darwin13.4.0 (64-bit)<br />Running under: OS X 10.10.3 (Yosemite)<br /><br />locale:<br />[1] en_GB.UTF-8/en_GB.UTF-8/en_GB.UTF-8/C/en_GB.UTF-8/en_GB.UTF-8<br /><br />attached base packages:<br />[1] stats graphics grDevices utils datasets methods base     <br /><br />other attached packages:<br />[1] showtext_0.4-2 sysfonts_0.5  <br /><br />loaded via a namespace (and not attached):<br />[1] RCurl_1.95-4.6 showtextdb_1.0 jsonlite_0.9.16 bitops_1.0-6

To leave a comment for the author, please follow the link and comment on their blog: mages' blog.

R-bloggers.com offers daily e-mail updates about R news and tutorials about learning R and many other topics. Click here if you're looking to post or find an R/data-science job.
Want to share your content on R-bloggers? click here if you have a blog, or here if you don't.

Never miss an update!
Subscribe to R-bloggers to receive
e-mails with the latest R posts.
(You will not see this message again.)

Click here to close (This popup will not appear again)