svglite is a graphics device that produces clean svg output, suitable
for use on the web, or hand editing. Compared to the built-in svg()
,
svglite produces smaller files, and leaves text as is.
svglite is available on CRAN using install.packages("svglite")
. You
can install the development version from github with:
# install.packages("devtools")
devtools::install_github("r-lib/svglite")
While the performance of svglite()
and svg()
is comparable (with a
slight upper hand to svglite)
library(svglite)
x <- runif(1e3)
y <- runif(1e3)
tmp1 <- tempfile()
tmp2 <- tempfile()
svglite_test <- function() {
svglite(tmp1)
plot(x, y)
dev.off()
}
svg_test <- function() {
svg(tmp2, onefile = TRUE)
plot(x, y)
dev.off()
}
plot(bench::mark(svglite_test(), svg_test()), type = 'ridge')
It produces considerably smaller files:
file.size(c(tmp1, tmp2)) / 1024
#> [1] 74.54492 321.01172
In both cases, compressing to make .svgz
is worthwhile:
gz <- function(in_path, out_path = tempfile()) {
out <- gzfile(out_path, "w")
writeLines(readLines(in_path), out)
close(out)
invisible(out_path)
}
file.size(gz(tmp1)) / 1024
#> [1] 9.416992
file.size(gz(tmp2)) / 1024
#> [1] 38.55762