14.2 Outputting to SVG Vector Files

14.2.1 Problem

You want to create a scalable vector graphics (SVG) image of your plot.

14.2.2 Solution

Use svglite() from the svglite package:

library(svglite)
svglite("myplot.svg", width = 4, height = 4)
plot(...)
dev.off()

# With ggsave()
ggsave("myplot.svg", width = 8, height = 8, units = "cm")

14.2.3 Discussion

Although R has a built-in svg() function that can generate SVG output, the svglite package provides more standards-compliant output.

When it comes to importing images, some programs may handle SVG files better than PDFs, and vice versa. For example, web browsers tend to have better SVG support, while document-creation programs like LaTeX tend to have better PDF support.