How to fix "failed to load cairo DLL" in R?

I only have a Windows machine to offer, but the following worked for both saving a .svg and .eps of your cowplot (after installing the package svglite):

# install.packages(svglite)
library(tidyverse)
library(cowplot)

d <- sample_n(diamonds, 50)
g <- ggplot(d, aes(carat, price)) + geom_point()
gg <- list(g,g,g)

plot_grid(plotlist = gg, nrow=1)
ggsave(filename = "testing.svg")
ggsave(filename = "testing.eps")

You can of course modify the filename to something like "~/Desktop/testing.svg", depending on where you want to safe your plot. Note that ggsave per default saves the last active plot, and the ending of the filename you specify should automatically trigger the right device.


I had same problem and I use MacOS. I found it is XQuartz related "since it is no longer part of OS X", according to CRAN. I installed XQuartz and basically all Cairo related problems are addressed.


The Homebrew version of R no longer supports cairo (more info here), so graphical devices like svg(), cairo_pdf(), and cairo_ps() in base R won't work if you install R via brew install r. You have to use other graphical devices (e.g., png, jpeg, pdf). If you need SVG output, ggsave() requires the svglite package and uses svglite::svglite() to save your plot to an SVG file.

If you do want to use Homebrew to install R, I recommend brew cask install r.

Tags:

R

Cairo