Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Embedding new fonts to plot in ggplot2, R (mac)

Tags:

r

pdf

fonts

ggplot2

I've been trying to export a ggplot2 plot to PDF. The problem is that I've been adding exotic font to my plots, and -as a consequence- the exported PDF don't show any text.

I made sure to import my font as such:

library(extrafont)
font_import(pattern = 'Arch')
loadfonts()

and I export to a PDF after my ggplot2 plot is put in a variable "p" :

ggsave("myPlot.pdf", plot=p,  width=4, height=6)
embed_fonts("myPlot.pdf", outfile="myPlot_embed.pdf")

I then get an error saying:

GhostScript was not found

This page, however, seems to suggest no other steps should be needed on Mac OS (there's an extra step on Windows): https://github.com/wch/extrafont

Any idea on what am I doing wrong?

like image 746
Lucien S. Avatar asked May 07 '15 19:05

Lucien S.


1 Answers

The instruction guidelines for the extrafont package you quoted (https://github.com/wch/extrafont/blob/master/README.md) clearly state:

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

So as a start, please check if you have Ghostscript installed at all:

  1. If yes, make sure it is in the $PATH, or that its installation location is included in the $PATH variable.

  2. If no, install Ghostscript first. You can use the method described below.


1. Installing MacPorts

First, install the MacPorts framework. MacPorts provides a package management system and ready-made packages which allow you to install a log of GNU and other Free Software packages.

Installation instructions are different, depending on your version of OS X:

  • https://www.macports.org/install.php

After you have MacPorts, run this command in a Terminal.app window:

sudo port selfupdate

2. Installing Ghostscript

MacPorts has a Ghostscript package. You can install it like this, via commands in a Terminal.app window:

sudo port install ghostscript

This command will draw in and install more packages which are required by Ghostscript as "dependencies".

Please note:

  1. After this installation, you'll have a Ghostscript executable placed as /opt/local/bin/gs. (There will be even more helper programs in /opt/local/bin/.)

  2. This requires that you need to put that directory into your $PATH. Therefore, put this line into your ~/.bashrc:

    export PATH=/opt/local/bin:$PATH
    

There are other options which you could try to install Ghostscript. One is HomeBrew -- but I have no personal experience with this.

like image 51
Kurt Pfeifle Avatar answered Oct 13 '22 19:10

Kurt Pfeifle