| Title: | Insert Graphs, Images and Text in SVG Files |
| Version: | 1.0.0 |
| Description: | Edit SVG files created in 'Inkscape' by replacing placeholders (e.g. a rectangle element or {} in a text box) by 'ggplot2' objects, images or text. This helps automate the creation of figures with complex layouts. |
| License: | MIT + file LICENSE |
| Encoding: | UTF-8 |
| RoxygenNote: | 7.3.3 |
| Imports: | cli, rlang, xml2, ggplot2, base64enc |
| Suggests: | palmerpenguins, testthat (≥ 3.0.0), svglite, knitr, rmarkdown |
| Config/testthat/edition: | 3 |
| VignetteBuilder: | knitr |
| URL: | https://github.com/DanielThedie/svgedit |
| BugReports: | https://github.com/DanielThedie/svgedit/issues |
| NeedsCompilation: | no |
| Packaged: | 2025-12-06 17:09:41 UTC; daniel |
| Author: | Daniel Thedie |
| Maintainer: | Daniel Thedie <daniel.thedie@ed.ac.uk> |
| Repository: | CRAN |
| Date/Publication: | 2025-12-11 13:50:02 UTC |
svgedit: Insert Graphs, Images and Text in SVG Files
Description
Edit SVG (Scalable Vector Graphics) files created in 'Inkscape' by replacing placeholders (e.g. a rectangle element or in a text box) by 'ggplot2' objects, images or text. This helps automate the creation of figures with complex layouts.
Author(s)
Maintainer: Daniel Thedie daniel.thedie@ed.ac.uk (ORCID) [copyright holder]
See Also
Useful links:
Report bugs at https://github.com/DanielThedie/svgedit/issues
Replace an svg object by a ggplot2 graph
Description
Replace an svg object by a ggplot2 graph
Usage
draw(
input_svg,
output_svg,
plots = NULL,
plot_scale = NULL,
text = NULL,
images = NULL,
dpi = 150
)
Arguments
input_svg |
Path to the input svg file |
output_svg |
Path to the output svg file |
plots |
A named list of ggplot2 objects. The list names should correspond to the labels of the svg elements to be replaced. |
plot_scale |
A named list of numeric values to scale the inserted plots. The names should correspond to the labels of the svg elements to be replaced. |
text |
A named list of character vectors. The list names should correspond to the labels of the svg text elements to be modified. Each character vector will be used to replace "" placeholders in the text element in order. |
images |
A named list of paths to image files. The list names should correspond to the labels of the svg image elements to be replaced. |
dpi |
The resolution to use when rendering the ggplot2 objects. |
Value
Invisibly returns NULL. The output svg file is written to output_svg.
Examples
library(ggplot2)
# Create a simple plot
p <- ggplot(mtcars, aes(x = mpg, y = wt)) + geom_point()
# Use draw() to insert the plot into an SVG template
input_svg <- system.file("examples", "Template.svg", package = "svgedit")
draw(
input_svg = input_svg,
output_svg = tempfile(fileext = ".svg"),
plots = list(panel_A = p)
)
Find an element by label in an Inkscape SVG document
Description
Find an element by label in an Inkscape SVG document
Usage
find_element(doc, label)
Arguments
doc |
An xml2 SVG document |
label |
The label of the element to find |
Value
The xml2 node corresponding to the element with the given id
Get the unit used in the SVG document
Description
Get the unit used in the SVG document
Usage
get_doc_unit(doc)
Arguments
doc |
An xml2 SVG document |
Value
The unit used in the SVG document (e.g., "px", "mm", "cm", "in")
Get the dimensions of an SVG element
Description
Get the dimensions of an SVG element
Usage
get_element_dimensions(
element,
doc_unit,
dpi = 150,
call = rlang::caller_env()
)
Arguments
element |
An xml2 node corresponding to an SVG element |
doc_unit |
The unit used in the SVG document (e.g., "px", "mm", "cm", "in") |
dpi |
The resolution to use when interpreting pixel units |
call |
The calling environment for error reporting |
Details
The function expects the element to have 'x', 'y', 'width', and 'height' attributes.
Value
A list with x, y, width, and height of the element
Insert a raster image (PNG/JPG) into an SVG document, replacing a target element
Description
Insert a raster image (PNG/JPG) into an SVG document, replacing a target element
Usage
insert_image(doc, label, image_file, dpi = 150)
Arguments
doc |
An xml2 SVG document |
label |
The label of the target element to be replaced |
image_file |
Path to the PNG or JPG image to be inserted |
dpi |
The resolution to use when interpreting pixel units (in the template svg) |
Value
The modified SVG document (doc) with the image added and the target removed
Add an element to an SVG document, replacing a target element
Description
Add an element to an SVG document, replacing a target element
Usage
insert_svg(doc, label, insert_file, dpi = 150)
Arguments
doc |
An xml2 SVG document |
label |
The label of the target element to be replaced |
insert_file |
Path to the SVG file to be inserted |
dpi |
The resolution to use when interpreting pixel units |
Value
The modified xml2 SVG document (doc) with the svg file added and the target removed
Replace "" in a text element by provided values
Description
Replace "" in a text element by provided values
Usage
insert_text(doc, label, values)
Arguments
doc |
An SVG document |
label |
The label of the text element to edit |
values |
A character vector to replace each "" in order |
Value
The modified SVG document (doc) with the text inserted
Convert a measurement to inches based on the unit
Description
Convert a measurement to inches based on the unit
Usage
unit_to_inch(val, unit, dpi = 150)
Arguments
val |
The measurement value |
unit |
The unit of the measurement (e.g., "px", "mm", "cm", "in") |
dpi |
The resolution to use when interpreting pixel units |
Value
The measurement converted to inches