Exporting Plots
Overview
webforest supports exporting plots to static image formats for publications, presentations, and reports.
| Method | Best For |
|---|---|
save_plot() |
Batch export, scripts, reproducible workflows |
| Browser download | Interactive exploration, quick exports |
Both methods use the same SVG rendering engine, ensuring consistent output.
Using save_plot()
Export a forest plot to SVG, PNG, or PDF:
# Create a spec
spec <- web_spec(data,
point = "hr", lower = "lower", upper = "upper",
label = "study", null_value = 1
)
# Save as SVG (vector)
save_plot(spec, "forest_plot.svg")
# Save as PNG (raster)
save_plot(spec, "forest_plot.png", width = 1200)
# Save as PDF
save_plot(spec, "forest_plot.pdf", width = 800)From htmlwidget Output
You can also save directly from forest_plot() output:
p <- forest_plot(data, point = "hr", lower = "lower", upper = "upper", label = "study")
save_plot(p, "output.svg")Output Formats
| Format | Extension | Use Case | Dependencies |
|---|---|---|---|
| SVG | .svg |
Vector graphics, web | None |
| PNG | .png |
Raster images, documents | rsvg package |
.pdf |
Print, publications | rsvg package |
- SVG: Best for web, editable in Illustrator/Inkscape, infinitely scalable
- PNG: Best for documents and slides, set
scale = 2for retina displays - PDF: Best for print, preserves vectors, requires
rsvg
SVG (Scalable Vector Graphics)
- Ideal for web display and further editing
- Infinitely scalable without quality loss
- Editable in Illustrator, Inkscape, etc.
PNG (Portable Network Graphics)
- Best for documents, slides, reports
- Set
scale = 2(default) for retina/HiDPI displays - Requires the
rsvgpackage
PDF (Portable Document Format)
- Best for print publications
- Vector format, scales perfectly
- Requires the
rsvgpackage
Options
save_plot(spec, file,
width = 800, # Width in pixels
height = NULL, # Auto-calculated if NULL
scale = 2 # PNG scaling factor
)Width and Height
widthsets the SVG/canvas width in pixelsheightis auto-calculated based on content ifNULL- Specify
heightto force a specific aspect ratio
Scale Factor (PNG only)
The scale parameter multiplies the resolution for PNG output:
# Standard resolution (800px wide)
save_plot(spec, "plot.png", width = 800, scale = 1)
# High resolution for retina displays (1600px wide)
save_plot(spec, "plot.png", width = 800, scale = 2)
# Very high resolution for print (2400px wide)
save_plot(spec, "plot.png", width = 800, scale = 3)Dependencies
V8 (Required for save_plot)
The V8 package provides JavaScript execution for SVG generation:
install.packages("V8")rsvg (Required for PNG/PDF)
The rsvg package converts SVG to raster formats:
install.packages("rsvg")On macOS, you may also need librsvg:
brew install librsvgPublication-Quality Figures
Most journals require 300 DPI figures. To calculate the right settings:
Width in pixels = Width in inches × 300 DPI × scale
For an 8-inch figure at 300 DPI with scale = 3: - width = 800 (base canvas width) - scale = 3 (multiplier for PNG output) - Result: 800 × 3 = 2400 pixels = 8 inches at 300 DPI
For Journal Submission
# High-resolution PNG at 300 DPI equivalent
# 800px * 3 = 2400px for ~8 inch width at 300 DPI
save_plot(spec, "figure1.png", width = 800, scale = 3)
# Or use PDF for vector output
save_plot(spec, "figure1.pdf", width = 800)For Presentations
# Wide format for slides
save_plot(spec, "slide_figure.png", width = 1200, scale = 2)For Web
# SVG for crisp display at any zoom level
save_plot(spec, "web_figure.svg", width = 800)Browser Export
Interactive plots include a download button (visible on hover):
- Hover over the plot to reveal the download button (top-right)
- Click to open the format menu
- Choose SVG or PNG
The browser export uses the same rendering engine as save_plot(), so output is consistent.
Troubleshooting
“SVG generator JavaScript file not found”
The V8 bundle needs to be built. This usually means the package wasn’t installed correctly:
# Reinstall the package
pak::pak("kaskarn/webforest")PNG export fails
Ensure rsvg is installed and working:
install.packages("rsvg")
# Test it works
rsvg::rsvg_png(charToRaw('<svg width="100" height="100"><circle cx="50" cy="50" r="40"/></svg>'))Colors look different
The SVG generator resolves theme colors at export time. Ensure you’re using the same theme for both interactive display and export:
my_theme <- web_theme_jama()
# Interactive
forest_plot(spec, theme = my_theme)
# Export
save_plot(web_spec(..., theme = my_theme), "output.svg")