plot_svg() compiles any vellumplot object (a PlotSpec, vtable(), or a
composition) to a stand-alone <svg> string rather than a file — the way
to embed a chart inline in HTML: a vsparkline() in a gt / reactable /
DT cell, a chart in a Quarto callout, or an inline glyph in a report. The
string carries a viewBox, so it scales.
Usage
plot_svg(
plot,
width = NULL,
height = NULL,
scaling = c("fixed", "fit"),
inline = FALSE,
recolor = NULL,
text = "native",
manifest = FALSE
)Arguments
- plot
- width, height
Optional page-size override in inches (defaults to the object's own size).
- scaling
"fixed"(default) keeps the pixel size;"fit"sets the root<svg>width/heightto100%so it fills its container (theviewBoxpreserves the aspect).- inline
TRUEstrips the leading<?xml …?>prolog so the<svg>is a valid fragment to drop straight into an HTML paragraph / cell / Quarto inline expression.FALSE(default) keeps the stand-alone-file prolog.- recolor
Optional named character vector mapping a source colour to a replacement written verbatim into the SVG — e.g.
c(grey30 = "currentColor")makes agrey30sparkline follow the surrounding text colour (dark-mode-adaptive inline). Each name is resolved to its hex and swapped for the value; use for CSS keywords ("currentColor") orvar(--x)that R's colour engine can't emit itself.- text
How glyphs are emitted:
"native"(default) or"outline"(paths, for maximum portability).- manifest
If
TRUE, embed a reproducibility manifest (seeplot_manifest()) as an XML comment in the SVG, so the figure carries its own data fingerprint;plot_verify()reads it back. DefaultFALSE.
Examples
svg <- plot_svg(vsparkline(cumsum(rnorm(20))))
substr(svg, 1, 40)
#> [1] "<?xml version=\"1.0\" encoding=\"UTF-8\"?>\n<"
# a transparent, prolog-free, dark-mode-adaptive inline sparkline:
plot_svg(vsparkline(1:9, color = "grey30"),
inline = TRUE, recolor = c(grey30 = "currentColor")
)
#> [1] "<svg xmlns=\"http://www.w3.org/2000/svg\" width=\"76\" height=\"23\" viewBox=\"0 0 76 23\" role=\"img\" aria-labelledby=\"vl893aa516-d\"><desc id=\"vl893aa516-d\">A sparkline plot. It plots .v (vertical axis) against .i (horizontal axis). Based on 9 observations.</desc><defs></defs><g data-vellum-panel=\"plot\"><g data-vellum-panel=\"panel-area\"><g data-vellum-panel=\"panel-1-1\"><g data-vellum-pan=\"panel-1-1\"><g data-vellum-id=\"layer-1-sparkline-g1\" role=\"presentation\"><path d=\"M4.16 19.78 L12.62 17.71 L21.08 15.64 L29.54 13.57 L38 11.5 L46.46 9.43 L54.92 7.36 L63.38 5.29 L71.84 3.22\" fill=\"none\" stroke=\"currentColor\" stroke-width=\"1\" stroke-linecap=\"round\" stroke-linejoin=\"round\"/></g><g data-vellum-id=\"layer-1-sparkline-g2\" role=\"presentation\"><circle cx=\"4.16\" cy=\"19.78\" r=\"2.65\" fill=\"#b22222\" stroke=\"#b22222\" stroke-width=\"1\"/><circle cx=\"71.84\" cy=\"3.22\" r=\"2.65\" fill=\"#b22222\" stroke=\"#b22222\" stroke-width=\"1\"/></g></g></g></g></g></svg>\n"
