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

width

Height and width in inches.

height

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 lifecycle 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 lifecycle 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='133.99' cy='160.45' r='2.70' style='stroke-width: 0.75;' />
#> <circle cx='227.49' cy='75.47' r='2.70' style='stroke-width: 0.75;' />
#> <circle cx='630.48' cy='134.26' r='2.70' style='stroke-width: 0.75;' />
#> <circle cx='82.40' cy='398.77' r='2.70' style='stroke-width: 0.75;' />
#> <circle cx='666.40' cy='486.13' r='2.70' style='stroke-width: 0.75;' />
#> </g>
#> <g clip-path='url(#cpMC4wMHw3MjAuMDB8MC4wMHw1NzYuMDA=)'>
#> <line x1='147.90' y1='502.56' x2='585.35' y2='502.56' style='stroke-width: 0.75;' />
#> <line x1='147.90' y1='502.56' x2='147.90' y2='509.76' style='stroke-width: 0.75;' />
#> <line x1='257.26' y1='502.56' x2='257.26' y2='509.76' style='stroke-width: 0.75;' />
#> <line x1='366.63' y1='502.56' x2='366.63' y2='509.76' style='stroke-width: 0.75;' />
#> <line x1='475.99' y1='502.56' x2='475.99' y2='509.76' style='stroke-width: 0.75;' />
#> <line x1='585.35' y1='502.56' x2='585.35' y2='509.76' style='stroke-width: 0.75;' />
#> <text x='147.90' 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='257.26' y='528.48' text-anchor='middle' style='font-size: 12.00px; font-family: "Liberation Sans";' textLength='19.09px' lengthAdjust='spacingAndGlyphs'>0.0</text>
#> <text x='366.63' y='528.48' text-anchor='middle' style='font-size: 12.00px; font-family: "Liberation Sans";' textLength='19.09px' lengthAdjust='spacingAndGlyphs'>0.5</text>
#> <text x='475.99' y='528.48' text-anchor='middle' style='font-size: 12.00px; font-family: "Liberation Sans";' textLength='19.09px' lengthAdjust='spacingAndGlyphs'>1.0</text>
#> <text x='585.35' y='528.48' text-anchor='middle' style='font-size: 12.00px; font-family: "Liberation Sans";' textLength='19.09px' lengthAdjust='spacingAndGlyphs'>1.5</text>
#> <line x1='59.04' y1='446.80' x2='59.04' y2='118.21' style='stroke-width: 0.75;' />
#> <line x1='59.04' y1='446.80' x2='51.84' y2='446.80' style='stroke-width: 0.75;' />
#> <line x1='59.04' y1='381.08' x2='51.84' y2='381.08' style='stroke-width: 0.75;' />
#> <line x1='59.04' y1='315.36' x2='51.84' y2='315.36' style='stroke-width: 0.75;' />
#> <line x1='59.04' y1='249.64' x2='51.84' y2='249.64' style='stroke-width: 0.75;' />
#> <line x1='59.04' y1='183.93' x2='51.84' y2='183.93' style='stroke-width: 0.75;' />
#> <line x1='59.04' y1='118.21' x2='51.84' y2='118.21' style='stroke-width: 0.75;' />
#> <text transform='translate(41.76,446.80) rotate(-90)' text-anchor='middle' style='font-size: 12.00px; font-family: "Liberation Sans";' textLength='23.43px' lengthAdjust='spacingAndGlyphs'>-0.6</text>
#> <text transform='translate(41.76,381.08) rotate(-90)' text-anchor='middle' style='font-size: 12.00px; font-family: "Liberation Sans";' textLength='23.43px' lengthAdjust='spacingAndGlyphs'>-0.4</text>
#> <text transform='translate(41.76,315.36) rotate(-90)' text-anchor='middle' style='font-size: 12.00px; font-family: "Liberation Sans";' textLength='23.43px' lengthAdjust='spacingAndGlyphs'>-0.2</text>
#> <text transform='translate(41.76,249.64) 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,183.93) rotate(-90)' text-anchor='middle' style='font-size: 12.00px; font-family: "Liberation Sans";' textLength='19.09px' lengthAdjust='spacingAndGlyphs'>0.2</text>
#> <text transform='translate(41.76,118.21) rotate(-90)' text-anchor='middle' style='font-size: 12.00px; font-family: "Liberation Sans";' textLength='19.09px' lengthAdjust='spacingAndGlyphs'>0.4</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