svgparser
loads vector-based SVG images as R vector objects.
The vector objects which are created in R are the standard graphics
object (‘grob’) in the base R {grid}
graphics system.
SVG images can also be imported as data.frames. Each shape, line and point is represented in the data.frame with copious helpings of metadata to relate individual coordinates to the original SVG structure.
This example loads an SVG image into a base R grid
graphics object,
and then draws the object using grid.draw
.
Note that this is not a fixed size raster image! This is a vector image which can be resized without loss of precision.
tiger_filename <- system.file("tiger.svg", package = "svgparser")
tiger_grob <- svgparser::read_svg(tiger_filename)
grid::grid.draw(tiger_grob)
This example loads an SVG as a data.frame and then plots the coordinates using ggplot. The element type from the original SVG is used as the aesthetic for colouring the lines in the plot.
tiger_df <- svgparser::read_svg(tiger_filename, obj_type = 'data.frame')
nrow(tiger_df)
#> [1] 57117
knitr::kable(head(tiger_df, 3))
x
y
name
idx
path\_idx
inst\_idx
path
col
fill
alpha
lwd
lineend
linejoin
linemitre
fontsize
cex
fontface
fontfamily
font
xpath
tag
elem\_idx
-122.3000
84.2850
move
1
1
1
m -122.3 84.285
\#000000FF
\#FFFFFFFF
1
0.172
butt
mitre
4
12
1
plain
serif
1
/svg/g/g\[1\]/path
path
1
-122.3000
84.2850
bezier3\_reflect
1
2
2
s 0.1 1.894 -0.73 1.875
\#000000FF
\#FFFFFFFF
1
0.172
butt
mitre
4
12
1
plain
serif
1
/svg/g/g\[1\]/path
path
1
-122.2997
84.2916
bezier3\_reflect
1
2
2
s 0.1 1.894 -0.73 1.875
\#000000FF
\#FFFFFFFF
1
0.172
butt
mitre
4
12
1
plain
serif
1
/svg/g/g\[1\]/path
path
1
There’s enough information in the data.frame to manually recreate most
of the SVG using whatever drawing mechanism you’d like. Here, I’ve used
ggplot2
to draw the outlines from the data.
ggplot(tiger_df) +
geom_path(aes(x, y, colour = name, group = interaction(elem_idx, path_idx))) +
scale_y_reverse()
read_svg()
read SVG into R as a {grid}
graphics object (grob) or
data.frameload_supertinyicon()
to load an SVG icon from the Super Tiny
Icons icon pack included
with this package. See LICENSE-supertinycons.txt
(MIT License)This package is available on GitHub.
# install.package('remotes')
remotes::install_github('coolbutuseless/cssparser') # Handles CSS styling
remotes::install_github('coolbutuseless/svgparser')
With R > v4.1.0:
With R < v4.1.0:
| Description | |
|-----------------------------------------------------------------------------------------------------------------------------|------------------------------------------------------------------------------------------------------------------------------------------------|
| SVG in ggplot using gggrid | |
| Creating tileable patterns for R graphics |
|
| Add SVG to ggplot2 plots |
|
| Debugging Issues by Using False Colouring |
|
| Customising SVG with CSS |
|
Add the following code to your website.
For more information on customizing the embed code, read Embedding Snippets.