Skip to content

This is a variation on svglite that makes it easy to access the current value as a string.

Usage

svgstring(
  width = 10,
  height = 8,
  bg = "white",
  pointsize = 12,
  standalone = TRUE,
  system_fonts = list(),
  user_fonts = list(),
  web_fonts = list(),
  id = NULL,
  fix_text_size = TRUE,
  scaling = 1
)

Arguments

height, width

Height and width in inches.

bg

Default background color for the plot (defaults to "white").

pointsize

Default point size.

standalone

Produce a standalone svg file? If FALSE, omits xml header and default namespace.

system_fonts

[Superseded] Consider using systemfonts::register_font() instead. Named list of font names to be aliased with fonts installed on your system. If unspecified, the R default families sans, serif, mono and symbol are aliased to the family returned by font_info().

user_fonts

[Superseded] Consider using systemfonts::register_font() instead. Named list of fonts to be aliased with font files provided by the user rather than fonts properly installed on the system. The aliases can be fonts from the fontquiver package, strings containing a path to a font file, or a list containing name and file elements with name indicating the font alias in the SVG output and file the path to a font file.

web_fonts

A list containing web fonts to use in the SVG. The fonts will still need to be available locally on the computer running the code, but viewers of the final SVG will not need the font if specified as a web font. Web fonts can either be specified using font_face() or given as a single string in which case they are taken to be URL's for an @import directive to e.g. Google Fonts.

id

A character vector of ids to assign to the generated SVG's. If creating more SVG files than supplied ids the exceeding SVG's will not have an id tag and a warning will be thrown.

fix_text_size

Should the width of strings be fixed so that it doesn't change between svg renderers depending on their font rendering? Defaults to TRUE. If TRUE each string will have the textLength CSS property set to the width calculated by systemfonts and lengthAdjust='spacingAndGlyphs'. Setting this to FALSE can be beneficial for heavy post-processing that may change content or style of strings, but may lead to inconsistencies between strings and graphic elements that depend on the dimensions of the string (e.g. label borders and background).

scaling

A scaling factor to apply to the rendered line width and text size. Useful for getting the right sizing at the dimension that you need.

Value

A function with no arguments: call the function to get the current value of the string.

Details

See svglite() documentation for information about specifying fonts.

Examples

s <- svgstring()
s()
#> 

plot.new()
s()
#> <?xml version='1.0' encoding='UTF-8' ?>
#> <svg xmlns='http://www.w3.org/2000/svg' xmlns:xlink='http://www.w3.org/1999/xlink' class='svglite' width='720.00pt' height='576.00pt' viewBox='0 0 720.00 576.00'>
#> <defs>
#>   <style type='text/css'><![CDATA[
#>     .svglite line, .svglite polyline, .svglite polygon, .svglite path, .svglite rect, .svglite circle {
#>       fill: none;
#>       stroke: #000000;
#>       stroke-linecap: round;
#>       stroke-linejoin: round;
#>       stroke-miterlimit: 10.00;
#>     }
#>     .svglite text {
#>       white-space: pre;
#>     }
#>   ]]></style>
#> </defs>
#> <rect width='100%' height='100%' style='stroke: none; fill: #FFFFFF;'/>
#> <defs>
#>   <clipPath id='cpMC4wMHw3MjAuMDB8MC4wMHw1NzYuMDA='>
#>     <rect x='0.00' y='0.00' width='720.00' height='576.00' />
#>   </clipPath>
#> </defs>
#> <g clip-path='url(#cpMC4wMHw3MjAuMDB8MC4wMHw1NzYuMDA=)'>
#> </g>
#> </svg>
text(0.5, 0.5, "Hi!")
s()
#> <?xml version='1.0' encoding='UTF-8' ?>
#> <svg xmlns='http://www.w3.org/2000/svg' xmlns:xlink='http://www.w3.org/1999/xlink' class='svglite' width='720.00pt' height='576.00pt' viewBox='0 0 720.00 576.00'>
#> <defs>
#>   <style type='text/css'><![CDATA[
#>     .svglite line, .svglite polyline, .svglite polygon, .svglite path, .svglite rect, .svglite circle {
#>       fill: none;
#>       stroke: #000000;
#>       stroke-linecap: round;
#>       stroke-linejoin: round;
#>       stroke-miterlimit: 10.00;
#>     }
#>     .svglite text {
#>       white-space: pre;
#>     }
#>   ]]></style>
#> </defs>
#> <rect width='100%' height='100%' style='stroke: none; fill: #FFFFFF;'/>
#> <defs>
#>   <clipPath id='cpMC4wMHw3MjAuMDB8MC4wMHw1NzYuMDA='>
#>     <rect x='0.00' y='0.00' width='720.00' height='576.00' />
#>   </clipPath>
#> </defs>
#> <g clip-path='url(#cpMC4wMHw3MjAuMDB8MC4wMHw1NzYuMDA=)'>
#> </g>
#> <defs>
#>   <clipPath id='cpNTkuMDR8Njg5Ljc2fDU5LjA0fDUwMi41Ng=='>
#>     <rect x='59.04' y='59.04' width='630.72' height='443.52' />
#>   </clipPath>
#> </defs>
#> <g clip-path='url(#cpNTkuMDR8Njg5Ljc2fDU5LjA0fDUwMi41Ng==)'>
#> <text x='374.40' y='285.36' text-anchor='middle' style='font-size: 12.00px; font-family: "Liberation Sans";' textLength='17.17px' lengthAdjust='spacingAndGlyphs'>Hi!</text>
#> </g>
#> </svg>
dev.off()
#> agg_png 
#>       2 

s <- svgstring()
plot(rnorm(5), rnorm(5))
s()
#> <?xml version='1.0' encoding='UTF-8' ?>
#> <svg xmlns='http://www.w3.org/2000/svg' xmlns:xlink='http://www.w3.org/1999/xlink' class='svglite' width='720.00pt' height='576.00pt' viewBox='0 0 720.00 576.00'>
#> <defs>
#>   <style type='text/css'><![CDATA[
#>     .svglite line, .svglite polyline, .svglite polygon, .svglite path, .svglite rect, .svglite circle {
#>       fill: none;
#>       stroke: #000000;
#>       stroke-linecap: round;
#>       stroke-linejoin: round;
#>       stroke-miterlimit: 10.00;
#>     }
#>     .svglite text {
#>       white-space: pre;
#>     }
#>   ]]></style>
#> </defs>
#> <rect width='100%' height='100%' style='stroke: none; fill: #FFFFFF;'/>
#> <defs>
#>   <clipPath id='cpMC4wMHw3MjAuMDB8MC4wMHw1NzYuMDA='>
#>     <rect x='0.00' y='0.00' width='720.00' height='576.00' />
#>   </clipPath>
#> </defs>
#> <g clip-path='url(#cpMC4wMHw3MjAuMDB8MC4wMHw1NzYuMDA=)'>
#> </g>
#> <defs>
#>   <clipPath id='cpNTkuMDR8Njg5Ljc2fDU5LjA0fDUwMi41Ng=='>
#>     <rect x='59.04' y='59.04' width='630.72' height='443.52' />
#>   </clipPath>
#> </defs>
#> <g clip-path='url(#cpNTkuMDR8Njg5Ljc2fDU5LjA0fDUwMi41Ng==)'>
#> <circle cx='411.55' cy='75.47' r='2.70' style='stroke-width: 0.75;' />
#> <circle cx='625.50' cy='262.68' r='2.70' style='stroke-width: 0.75;' />
#> <circle cx='440.25' cy='216.20' r='2.70' style='stroke-width: 0.75;' />
#> <circle cx='82.40' cy='486.13' r='2.70' style='stroke-width: 0.75;' />
#> <circle cx='666.40' cy='157.56' r='2.70' style='stroke-width: 0.75;' />
#> </g>
#> <g clip-path='url(#cpMC4wMHw3MjAuMDB8MC4wMHw1NzYuMDA=)'>
#> <line x1='90.74' y1='502.56' x2='632.14' y2='502.56' style='stroke-width: 0.75;' />
#> <line x1='90.74' y1='502.56' x2='90.74' y2='509.76' style='stroke-width: 0.75;' />
#> <line x1='271.21' y1='502.56' x2='271.21' y2='509.76' style='stroke-width: 0.75;' />
#> <line x1='451.67' y1='502.56' x2='451.67' y2='509.76' style='stroke-width: 0.75;' />
#> <line x1='632.14' y1='502.56' x2='632.14' y2='509.76' style='stroke-width: 0.75;' />
#> <text x='90.74' y='528.48' text-anchor='middle' style='font-size: 12.00px; font-family: "Liberation Sans";' textLength='23.43px' lengthAdjust='spacingAndGlyphs'>-1.5</text>
#> <text x='271.21' y='528.48' text-anchor='middle' style='font-size: 12.00px; font-family: "Liberation Sans";' textLength='23.43px' lengthAdjust='spacingAndGlyphs'>-1.0</text>
#> <text x='451.67' y='528.48' text-anchor='middle' style='font-size: 12.00px; font-family: "Liberation Sans";' textLength='23.43px' lengthAdjust='spacingAndGlyphs'>-0.5</text>
#> <text x='632.14' y='528.48' text-anchor='middle' style='font-size: 12.00px; font-family: "Liberation Sans";' textLength='19.09px' lengthAdjust='spacingAndGlyphs'>0.0</text>
#> <line x1='59.04' y1='438.71' x2='59.04' y2='111.71' style='stroke-width: 0.75;' />
#> <line x1='59.04' y1='438.71' x2='51.84' y2='438.71' style='stroke-width: 0.75;' />
#> <line x1='59.04' y1='373.31' x2='51.84' y2='373.31' style='stroke-width: 0.75;' />
#> <line x1='59.04' y1='307.91' x2='51.84' y2='307.91' style='stroke-width: 0.75;' />
#> <line x1='59.04' y1='242.51' x2='51.84' y2='242.51' style='stroke-width: 0.75;' />
#> <line x1='59.04' y1='177.11' x2='51.84' y2='177.11' style='stroke-width: 0.75;' />
#> <line x1='59.04' y1='111.71' x2='51.84' y2='111.71' style='stroke-width: 0.75;' />
#> <text transform='translate(41.76,438.71) rotate(-90)' text-anchor='middle' style='font-size: 12.00px; font-family: "Liberation Sans";' textLength='23.43px' lengthAdjust='spacingAndGlyphs'>-1.0</text>
#> <text transform='translate(41.76,373.31) rotate(-90)' text-anchor='middle' style='font-size: 12.00px; font-family: "Liberation Sans";' textLength='23.43px' lengthAdjust='spacingAndGlyphs'>-0.5</text>
#> <text transform='translate(41.76,307.91) rotate(-90)' text-anchor='middle' style='font-size: 12.00px; font-family: "Liberation Sans";' textLength='19.09px' lengthAdjust='spacingAndGlyphs'>0.0</text>
#> <text transform='translate(41.76,242.51) rotate(-90)' text-anchor='middle' style='font-size: 12.00px; font-family: "Liberation Sans";' textLength='19.09px' lengthAdjust='spacingAndGlyphs'>0.5</text>
#> <text transform='translate(41.76,177.11) rotate(-90)' text-anchor='middle' style='font-size: 12.00px; font-family: "Liberation Sans";' textLength='19.09px' lengthAdjust='spacingAndGlyphs'>1.0</text>
#> <text transform='translate(41.76,111.71) rotate(-90)' text-anchor='middle' style='font-size: 12.00px; font-family: "Liberation Sans";' textLength='19.09px' lengthAdjust='spacingAndGlyphs'>1.5</text>
#> <polygon points='59.04,502.56 689.76,502.56 689.76,59.04 59.04,59.04 ' style='stroke-width: 0.75;' />
#> <text x='374.40' y='557.28' text-anchor='middle' style='font-size: 12.00px; font-family: "Liberation Sans";' textLength='53.09px' lengthAdjust='spacingAndGlyphs'>rnorm(5)</text>
#> <text transform='translate(12.96,280.80) rotate(-90)' text-anchor='middle' style='font-size: 12.00px; font-family: "Liberation Sans";' textLength='53.09px' lengthAdjust='spacingAndGlyphs'>rnorm(5)</text>
#> </g>
#> </svg>
dev.off()
#> agg_png 
#>       2