Ggplot Embedded Fonts in PDF

ggplot embedded fonts in pdf

Try adding device=cairo_pdf to the ggsave() call. This appears to solve the problem for me. This way, it's no longer necessary to use embed_fonts().

See mgaudet's comment here: https://github.com/wch/extrafont/issues/8

Saving ggplot graph to PDF with fonts embedded in r

There are a couple issues at play here: (1) loading fonts into R and (2) using a PDF-writing library that works correctly with custom embedded fonts.

First, as others have mentioned, on Windows you generally need to run extrafont::font_import() to register many of your system fonts with R, but it can take a while and can miss TTF and other types of fonts. One way around this is to load fonts into R on the fly, without loading the full database, using windowsFonts(name_of_font_inside_r = windowsFont("Name of actual font")), like so:

windowsFonts(Calibri = windowsFont("Calibri"))

This makes just that one font accessible in R. You can check with windowsFonts(). You have to run this line each time the script is run—the font loading doesn't persist across sessions. Once the font has been loaded, you can use it normally:

library(tidyverse)
df <- data_frame(x = 1:10, y = 2:11)

p <- ggplot(df, aes(x = x, y = y)) +
geom_point() +
labs(title = "Yay Calibri") +
theme_light(base_family = "Calibri")
p

Calibrified

Second, R's built-in PDF-writing device on both Windows and macOS doesn't handle font embedding very well. However, R now includes the Cairo graphics library, which can embed fonts just fine. You can specify the Cairo device in ggsave() to use it, which is easier than dealing with GhostScript:

ggsave(p, filename = "whatever.pdf", device = cairo_pdf, 
width = 4, height = 3, units = "in")

ggplot2 embed all used fonts in pdf, R

This succeeded:

embedFonts( path.expand("~/Rplots.pdf") )

This did not, (so by experiment I have determined that embedFonts requires a full path and will not do "tilde expansion"):

embedFonts("~/Rplots.pdf")
... snipped a bunch of inscrutable error messages
GPL Ghostscript 9.16: Unrecoverable error, exit code 1
Error in sprintf(gettext(fmt, domain = domain), ...) :
object 'cmd' not found

although it did demonstrate that I had a functional installation of Ghostscript which is a requirement for embedFonts. (My copy of Ghostscript was probably from my installation of Tex.)

ggplot2 ggsave: keep embedded fonts in exported .png

Definitely not a full answer, but I've been able to fix this issue by restarting R, then loading only the ggplot2 and scales libraries I need for my plot, plotting, and saving. So there is some other package that I'm using that is interfering with ggsave.

Problems with PDF fonts generated with ggsave under windows when linking in Illustrator

After further research I could solve the problem with the embedFonts function. The the problem seems to be that the fonts are not embedded by default. I wrote a small function to use instead of ggsave to automatically embed the fonts into the same PDF file:

ggsave_embed<-function(fileN, ...){
ggsave(fileN, ...)
embedFonts(file=fileN, outfile = fileN)
}
# example usage:
ggsave_embed("myfile.pdf", myPlot)

R + ggplot + pdf device + LaTeX: is it possible to embed fonts one time

I've tried to do embed_fonts over the PDF, generated on step 3 instead of calling it over each of graph generated on step 1 and that seem to be OK

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

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.

Embedding fonts in ggplot2 charts in rmarkdown documents

If you set the graphics device to "cairo_pdf" the fonts will be embedded. You can do this for individual chunks or for the whole document using knitr::opts_chunk$set

I used a really obviously different font below so that it was clear the fonts were really being set.

The package is called "extrafont" not "extrafonts"

---
title: "Untitled"
output: beamer_presentation
---

```{r, echo=FALSE, message = FALSE}
knitr::opts_chunk$set(warning=FALSE, message=FALSE, echo = FALSE, dev = "cairo_pdf")
```

```{r}
library(extrafont)
library(ggplot2)
loadfonts()
```

##

```{r, fig.width = 5}
qplot(iris$Sepal.Length) + theme_light(base_family = "Vladimir Script")
```

Embedded pdf-font in R-plot is not recognized by InDesign although available

It seems if the font was not properly embedded into the pdf.
By running embed_fonts() after saving the plot, the according font got embedded and now works in InDesign.

I just needed:

library(extrafont)
embed_fonts(file="plot.pdf", outfile="plot.pdf")


Related Topics



Leave a reply



Submit