library(svglite)
library(ggplot2)
library(rsvg)
library(png)
library(jpeg)
library(webp)
##
## DESCRIPTION
##
## The new rsvg package renders (vector based) SVG images into
## high-quality bitmap arrays. The resulting image is an array
## of 3 dimensions: height * width * 4 (RGBA) and can be written
## to png, jpeg or web
##
# create an svg image
svglite("plot.svg", width = 10, height = 7)
qplot(mpg, wt, data = mtcars, colour = factor(cyl))
dev.off()
# render it into a bitmap array
bitmap <- rsvg("plot.svg")
dim(bitmap)
# write to format
png::writePNG(bitmap, "bitmap.png")
jpeg::writeJPEG(bitmap, "bitmap.jpg", quality = 1)
webp::write_webp(bitmap, "bitmap.webp", quality = 100)
##
## The advantage of storing your plots in svg format is they
## can be rendered later into an arbitrary resolution and
## format without loss of quality
##
## For example suppose we need to render the plot into ultra HD
## so that it is crisp as toast when printed a conference poster:
##
# render it into a bitmap array
bitmap <- rsvg("plot.svg", width = 3840)
png::writePNG(bitmap, "plot_4k.png", dpi = 144)
browseURL("plot_4k.png")
##
## Rather than actually dealing with the bitmap array in R, rsvg
## also allows you to directly render the image to various output
## formats, which is slighly faster
##
rsvg_pdf("plot.svg", "out.pdf")
rsvg_ps("plot.svg", "out.ps")
rsvg_svg("plot.svg", "out.svg")
rsvg_png("plot.svg", "out.png")
rsvg_webp("plot.svg", "out.webp")
# download.file("http://dev.w3.org/SVG/tools/svgweb/samples/svg-files/tiger.svg", "tiger.svg")
# bin <- rsvg_raw("tiger.svg")
# h2w <- 1
# pct_v <- 1
# pct_h <- 1
# orient <- c("letter", "landscape")
#
#
# n <- 300
# w <- n * pct_v
# h <- h2w*n * pct_h
#
# rstudioapi::savePlotAsImage(format = "png", width = w, height = h)
#
# rstudioapi::insertText()
Add the following code to your website.
For more information on customizing the embed code, read Embedding Snippets.