| Title: | Insert Graphs, Images and Text in SVG Files |
|---|---|
| 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. |
| Authors: | Daniel Thedie [aut, cre, cph] (ORCID: <https://orcid.org/0000-0002-1352-7245>) |
| Maintainer: | Daniel Thedie <[email protected]> |
| License: | MIT + file LICENSE |
| Version: | 1.0.0 |
| Built: | 2026-05-22 18:48:01 UTC |
| Source: | https://github.com/danielthedie/svgedit |
Replace an svg object by a ggplot2 graph
draw( input_svg, output_svg, plots = NULL, plot_scale = NULL, text = NULL, images = NULL, dpi = 150 )draw( input_svg, output_svg, plots = NULL, plot_scale = NULL, text = NULL, images = NULL, dpi = 150 )
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. |
Invisibly returns NULL. The output svg file is written to output_svg.
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) )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
find_element(doc, label)find_element(doc, label)
doc |
An xml2 SVG document |
label |
The label of the element to find |
The xml2 node corresponding to the element with the given id
Get the unit used in the SVG document
get_doc_unit(doc)get_doc_unit(doc)
doc |
An xml2 SVG document |
The unit used in the SVG document (e.g., "px", "mm", "cm", "in")
Get the dimensions of an SVG element
get_element_dimensions( element, doc_unit, dpi = 150, call = rlang::caller_env() )get_element_dimensions( element, doc_unit, dpi = 150, call = rlang::caller_env() )
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 |
The function expects the element to have 'x', 'y', 'width', and 'height' attributes.
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
insert_image(doc, label, image_file, dpi = 150)insert_image(doc, label, image_file, dpi = 150)
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) |
The modified SVG document (doc) with the image added and the target removed
Add an element to an SVG document, replacing a target element
insert_svg(doc, label, insert_file, dpi = 150)insert_svg(doc, label, insert_file, dpi = 150)
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 |
The modified xml2 SVG document (doc) with the svg file added and the target removed
Replace "" in a text element by provided values
insert_text(doc, label, values)insert_text(doc, label, values)
doc |
An SVG document |
label |
The label of the text element to edit |
values |
A character vector to replace each "" in order |
The modified SVG document (doc) with the text inserted
Convert a measurement to inches based on the unit
unit_to_inch(val, unit, dpi = 150)unit_to_inch(val, unit, dpi = 150)
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 |
The measurement converted to inches