{"id":719,"date":"2015-02-03T16:27:48","date_gmt":"2015-02-03T23:27:48","guid":{"rendered":"http:\/\/homepages.uc.edu\/~yaozo\/wordpress\/?p=719"},"modified":"2015-02-03T16:27:48","modified_gmt":"2015-02-03T23:27:48","slug":"tired-of-using-helvetica-in-your-r-graphics-heres-how-to-use-the-fonts-you-like","status":"publish","type":"post","link":"https:\/\/zhuoyao.net\/index.php\/2015\/02\/03\/tired-of-using-helvetica-in-your-r-graphics-heres-how-to-use-the-fonts-you-like\/","title":{"rendered":"Tired of using Helvetica in your R graphics? Here\u2019s how to use the fonts you like"},"content":{"rendered":"<p>Although R has vast graphical functionality I&#8217;ve lamented the lack of support for additional fonts. You can spend an incredible amount of time fine-tuning a <code>ggplot2<\/code>graphic, fiddling with the length of the tick marks, getting the legend just right but then the Helvetica text detracts from the beauty of what you&#8217;ve created.<\/p>\n<p>My understanding is that support for &#8216;non-standard&#8217; fonts is extremely difficult given the large number of computing setups, graphics devices etc. Nevertheless we often need to prepare Postscript and PDF plots for scientific papers and reports and the standard fonts often don\u2019t cut it. Luckily there is support for additional fonts particularly if you\u2019re creating PDFs or postscript files. I&#8217;m going to show an example using the package <code>extrafont<\/code>. There is also a relatively new package called <code>showtext<\/code> that I got to work for me, but I didn&#8217;t find that it offered any functionality beyond <code>extrafont<\/code> (and it crashed my R session twice) so I won&#8217;t cover that package.<\/p>\n<h2>1. Import fonts (and some quick data setup)<\/h2>\n<p>For the <code>extrafont<\/code> package you&#8217;ll need to make sure that you have GhostScript on your system in order to embed the fonts (you will also need to tell R where it&#8217;s located \u2013 see below). In addition, you will need to import the fonts you need. Luckily the package comes with a function that does this for you without much fuss \u2013 it takes a couple of minutes, depending on how many fonts you have. Here is an example of the code to import and then review the fonts:<\/p>\n<pre><code class=\"r hljs \"><span class=\"hljs-keyword\">library<\/span>(extrafont)\nfont_import() <span class=\"hljs-comment\"># import all your fonts<\/span>\nfonts() <span class=\"hljs-comment\">#get a list of fonts<\/span>\nfonttable()\nfonttable()[<span class=\"hljs-number\">40<\/span>:<span class=\"hljs-number\">45<\/span>,] <span class=\"hljs-comment\"># very useful table listing the family name, font name etc<\/span>\n<\/code><\/pre>\n<pre><code class=\"r hljs \"><span class=\"hljs-comment\">##   package         afmfile                         fontfile       FullName<\/span>\n<span class=\"hljs-comment\">## 1      NA  AGENCYB.afm.gz  C:\\\\Windows\\\\Fonts\\\\AGENCYB.TTF Agency FB Bold<\/span>\n<span class=\"hljs-comment\">## 2      NA  AGENCYR.afm.gz  C:\\\\Windows\\\\Fonts\\\\AGENCYR.TTF      Agency FB<\/span>\n<span class=\"hljs-comment\">## 3      NA  ahronbd.afm.gz  C:\\\\Windows\\\\Fonts\\\\ahronbd.ttf   Aharoni Bold<\/span>\n<span class=\"hljs-comment\">## 4      NA    ALGER.afm.gz    C:\\\\Windows\\\\Fonts\\\\ALGER.TTF       Algerian<\/span>\n<span class=\"hljs-comment\">## 5      NA Aller_Rg.afm.gz C:\\\\Windows\\\\Fonts\\\\Aller_Rg.ttf          Aller<\/span>\n<span class=\"hljs-comment\">## 6      NA    Aller.afm.gz    C:\\\\Windows\\\\Fonts\\\\Aller.ttf     Aller Bold<\/span>\n<span class=\"hljs-comment\">##   FamilyName      FontName  Bold Italic Symbol afmsymfile<\/span>\n<span class=\"hljs-comment\">## 1  Agency FB AgencyFB-Bold  TRUE  FALSE  FALSE         NA<\/span>\n<span class=\"hljs-comment\">## 2  Agency FB  AgencyFB-Reg FALSE  FALSE  FALSE         NA<\/span>\n<span class=\"hljs-comment\">## 3    Aharoni  Aharoni-Bold  TRUE  FALSE  FALSE         NA<\/span>\n<span class=\"hljs-comment\">## 4   Algerian      Algerian FALSE  FALSE  FALSE         NA<\/span>\n<span class=\"hljs-comment\">## 5      Aller         Aller FALSE  FALSE  FALSE         NA<\/span>\n<span class=\"hljs-comment\">## 6      Aller    Aller-Bold  TRUE  FALSE  FALSE         NA<\/span>\n<\/code><\/pre>\n<p>Now we&#8217;re ready to use the fonts in an actual plot. Based on my review of Winston Chang&#8217;s <a href=\"https:\/\/github.com\/wch\/extrafont\">GitHub repository<\/a> for the package all computer systems can use <code>extrafont<\/code> to embed fonts in PDF\/PS files but extra fonts are only available in bitmap output on Windows machines.<\/p>\n<pre><code class=\"r hljs \"><span class=\"hljs-comment\"># a little setup, I'm using data from the amazing NMMAPS air pollution study<\/span>\n<span class=\"hljs-comment\"># background here http:\/\/bit.ly\/1pqboBG<\/span>\n<span class=\"hljs-keyword\">library<\/span>(ggplot2)\nnmmaps&lt;-read.csv(<span class=\"hljs-string\">\"chicago-nmmaps.csv\"<\/span>, as.is=<span class=\"hljs-literal\">T<\/span>)\nnmmaps$date&lt;-as.Date(nmmaps$date)\nnmmaps&lt;-nmmaps[nmmaps$date&gt;as.Date(<span class=\"hljs-string\">\"1996-01-01\"<\/span>),]\n<\/code><\/pre>\n<h2>2. Use your new fonts \u2013 Bauhaus 93 TrueType font anyone?<\/h2>\n<p>Below I&#8217;m saving directly to an image and the non-default font shows up both on a screen device and in the saved PNG file. But on a Mac you may be limited to saving to PDF (see next setp)<\/p>\n<p>.<\/p>\n<pre><code class=\"r hljs \"><span class=\"hljs-comment\"># default font<\/span>\nggplot(nmmaps, aes(x = date, y = temp)) + geom_point(color=<span class=\"hljs-string\">\"red\"<\/span>)+\n  ggtitle(<span class=\"hljs-string\">\"This is a default font\"<\/span>)+\n  theme(plot.title = element_text(size=<span class=\"hljs-number\">30<\/span>, face=<span class=\"hljs-string\">\"bold\"<\/span>, vjust=<span class=\"hljs-number\">1<\/span>))\n<\/code><\/pre>\n<p>&nbsp;<\/p>\n<pre><code class=\"r hljs \"><span class=\"hljs-comment\"># new font (note the family argument)<\/span>\nggplot(nmmaps, aes(x = date, y = temp)) + geom_point(color=<span class=\"hljs-string\">\"red\"<\/span>)+\n  ggtitle(<span class=\"hljs-string\">\"This is a non-default font\"<\/span>)+\n  theme(plot.title = element_text(size=<span class=\"hljs-number\">30<\/span>, \n                                  vjust=<span class=\"hljs-number\">1<\/span>, family=<span class=\"hljs-string\">\"Bauhaus 93\"<\/span>))\n<\/code><\/pre>\n<p>&nbsp;<\/p>\n<h2>3. Create a PDF (and don&#8217;t forget to embed your fonts)<\/h2>\n<p>You can use <code>ggplot2<\/code>&#8216;s great <code>ggsave<\/code> function to save the plot to PDF.<\/p>\n<pre><code class=\"r hljs \"><span class=\"hljs-comment\"># note that this step will get warnings that you can ignore<\/span>\nggsave(<span class=\"hljs-string\">\"newfont.pdf\"<\/span>)\n<\/code><\/pre>\n<p>If you open the PDF now, though, you will be sorely disappointed! The new font will not appear in the PDF because we have not embedded the font in the PDF yet. In order to do this you have two last steps. Tell R where GhostScript is located and then embed the fonts.<\/p>\n<pre><code class=\"r hljs \">Sys.setenv(R_GSCMD = <span class=\"hljs-string\">\"C:\/Program Files (x86)\/gs\/gs9.02\/bin\/gswin32.exe\"<\/span>)\nembed_fonts(<span class=\"hljs-string\">\"newfont.pdf\"<\/span>)\n<\/code><\/pre>\n<p>And you&#8217;re ready to send that PDF directly to your journal of choice.<\/p>\n<h2>4. Last comment<\/h2>\n<p>I found that most of the fonts worked perfectly both on my image devices as well as PDF but in a few cases something went wrong with the PDF. For example, I played around with the Algerian TrueType font and got output like this:<\/p>\n<p><a href=\"http:\/\/zevross.com\/blog\/wp-content\/uploads\/2014\/07\/NO.png\"><img loading=\"lazy\" decoding=\"async\" class=\"aligncenter size-full wp-image-639\" src=\"http:\/\/zevross.com\/blog\/wp-content\/uploads\/2014\/07\/NO.png\" alt=\"NO\" width=\"968\" height=\"214\" \/><\/a><\/p>\n<p>when it should look like this:<\/p>\n<p><a href=\"http:\/\/zevross.com\/blog\/wp-content\/uploads\/2014\/07\/YES.png\"><img loading=\"lazy\" decoding=\"async\" class=\"aligncenter size-full wp-image-640\" src=\"http:\/\/zevross.com\/blog\/wp-content\/uploads\/2014\/07\/YES.png\" alt=\"YES\" width=\"847\" height=\"214\" \/><\/a><\/p>\n<p>Some digging would be required to track down the issue and, fortunately, Algerian TrueType is not high on my list of fonts to use.<\/p>\n","protected":false},"excerpt":{"rendered":"<p>Although R has vast graphical functionality I&#8217;ve lamented the lack of support for additional fonts. You can spend an incredible amount of time fine-tuning a&hellip; <\/p>\n","protected":false},"author":1,"featured_media":0,"comment_status":"closed","ping_status":"closed","sticky":false,"template":"","format":"standard","meta":{"footnotes":""},"categories":[20],"tags":[],"class_list":["post-719","post","type-post","status-publish","format-standard","hentry","category-r"],"_links":{"self":[{"href":"https:\/\/zhuoyao.net\/index.php\/wp-json\/wp\/v2\/posts\/719","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/zhuoyao.net\/index.php\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/zhuoyao.net\/index.php\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/zhuoyao.net\/index.php\/wp-json\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"https:\/\/zhuoyao.net\/index.php\/wp-json\/wp\/v2\/comments?post=719"}],"version-history":[{"count":0,"href":"https:\/\/zhuoyao.net\/index.php\/wp-json\/wp\/v2\/posts\/719\/revisions"}],"wp:attachment":[{"href":"https:\/\/zhuoyao.net\/index.php\/wp-json\/wp\/v2\/media?parent=719"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/zhuoyao.net\/index.php\/wp-json\/wp\/v2\/categories?post=719"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/zhuoyao.net\/index.php\/wp-json\/wp\/v2\/tags?post=719"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}