Creates code to show a diagram in svg format
lav_plotinfo_svgcode.RdCreates svg code to show a diagram of the model.
Usage
lav_plotinfo_svgcode(plotinfo,
outfile = "",
sloped_labels = TRUE,
standalone = FALSE,
stroke_width = 2L,
font_size = 20L,
idx_font_size = 15L,
dy = 5L,
mlovcolors = c("lightgreen", "lightblue"),
lightness= 1,
font_family = "Latin Modern Math, arial, Arial, sans",
italic = TRUE,
auto_subscript = TRUE
)Arguments
- plotinfo
A plotinfo structure as returned from
lav_plotinfo_positions.- outfile
A connection or a character string, the file to store the code.
- sloped_labels
logical, sloped labels for the edges.
- standalone
logical, should code be added to produce an html file with the svg embedded in it? If FALSE (the default), the outfile (if specified) must have the file extension 'svg'; if TRUE, the file extension must be 'htm' or 'html'.
- stroke_width
Value for stroke-width parameter in svg.
- font_size
An integer specifying the normal font size to use.
- idx_font_size
An integer specifying the font size to use for a subscript.
- dy
An integer specifying the distance by which to move the baseline of the subscript.
- mlovcolors
Array of two colors for distinguishing ov nodes with the same name across the levels of a multilevel model.
- lightness
A scalar factor to modify the distances between nodes.
- font_family
Fonts to be tried for rendering the labels. The first one, Latin Modern Math, is close to the default font used in tikz for mathematical expressions and can be downloaded from CTAN.
- italic
Should labels be in an italic font? If FALSE, the labels are shown in mathrm font.
- auto_subscript
Logical, see
lav_label_code.
Examples
model <- 'alpha =~ x1 + x2 + x3 # latent variable
beta <~ x4 + x5 + x6 # composite
gamma =~ x7 + x8 + x9 # latent variable
Xi =~ x10 + x11 + x12 + x13 # latent variable
# regressions
Xi ~ v * alpha + t * beta + 1
alpha ~ yy * Theta1 + tt * beta + ss * gamma
'
test <- lav_model_plotinfo(model)
test <- lav_plotinfo_positions(test)
lav_plotinfo_svgcode(test) # no file given, so output to R console
#> <svg width="800" height="1200" version="1.1" xmlns="http://www.w3.org/2000/svg" xmlns:xlink="http://www.w3.org/1999/xlink">
#> <rect width="100%" height="100%" fill="white" />
#> <defs>
#> <marker id="arr" markerWidth="6" markerHeight="6"
#> refX="6" refY="2.5" orient="auto">
#> <path d="M 0 0 L 6 2.5 L 0 5 L 2 2.5 z" fill="black" />
#> </marker>
#> <marker id="sarr" markerWidth="6" markerHeight="6"
#> refX="0" refY="2.5" orient="auto">
#> <path d="M 0 2.5 L 6 0 L 4 2.5 L 6 5 z" fill="black" />
#> </marker>
#> </defs>
#> <path d="M 400 270 L 300 221.213203435596" stroke-width="2" stroke="black" marker-end="url(#arr)" />
#> <path id="L1" d="M 300 221.213203435596 L 400 270" stroke-width="0" stroke="none" fill="none" />
#> <path id="L2" d="M 400 270 L 400 221.213203435596" stroke-width="2" stroke="black" marker-end="url(#arr)" />
#> <path id="L3" d="M 400 270 L 500 221.213203435596" stroke-width="2" stroke="black" marker-end="url(#arr)" />
#> <path id="L4" d="M 221.213203435596 300 L 270 400" stroke-width="2" stroke="black" marker-end="url(#arr)" />
#> <path id="L5" d="M 221.213203435596 400 L 270 400" stroke-width="2" stroke="black" marker-end="url(#arr)" />
#> <path id="L6" d="M 221.213203435596 500 L 270 400" stroke-width="2" stroke="black" marker-end="url(#arr)" />
#> <path d="M 270 900 L 221.213203435596 800" stroke-width="2" stroke="black" marker-end="url(#arr)" />
#> <path id="L7" d="M 221.213203435596 800 L 270 900" stroke-width="0" stroke="none" fill="none" />
#> <path d="M 270 900 L 221.213203435596 900" stroke-width="2" stroke="black" marker-end="url(#arr)" />
#> <path id="L8" d="M 221.213203435596 900 L 270 900" stroke-width="0" stroke="none" fill="none" />
#> <path d="M 270 900 L 221.213203435596 1000" stroke-width="2" stroke="black" marker-end="url(#arr)" />
#> <path id="L9" d="M 221.213203435596 1000 L 270 900" stroke-width="0" stroke="none" fill="none" />
#> <path id="L10" d="M 530 600 L 578.786796564404 500" stroke-width="2" stroke="black" marker-end="url(#arr)" />
#> <path id="L11" d="M 530 600 L 578.786796564404 600" stroke-width="2" stroke="black" marker-end="url(#arr)" />
#> <path id="L12" d="M 530 600 L 578.786796564404 700" stroke-width="2" stroke="black" marker-end="url(#arr)" />
#> <path id="L13" d="M 530 600 L 578.786796564404 800" stroke-width="2" stroke="black" marker-end="url(#arr)" />
#> <path id="L14" d="M 400 330 L 500 570" stroke-width="2" stroke="black" marker-end="url(#arr)" />
#> <text fill="black" font-size="20" font-family="Latin Modern Math, arial, Arial, sans" font-style="italic" text-anchor="middle">
#> <textPath xlink:href="#L14" startOffset="50%">v</textPath>
#> </text>
#> <path id="L15" d="M 315 425.980762113533 L 478.786796564404 578.786796564404" stroke-width="2" stroke="black" marker-end="url(#arr)" />
#> <text fill="black" font-size="20" font-family="Latin Modern Math, arial, Arial, sans" font-style="italic" text-anchor="middle">
#> <textPath xlink:href="#L15" startOffset="50%">t</textPath>
#> </text>
#> <path id="L16" d="M 500 315 L 500 570" stroke-width="2" stroke="black" marker-end="url(#arr)" />
#> <path id="L17" d="M 321.213203435596 478.786796564404 L 378.786796564404 321.213203435596" stroke-width="2" stroke="black" marker-end="url(#arr)" />
#> <text fill="black" font-size="20" font-family="Latin Modern Math, arial, Arial, sans" font-style="italic" text-anchor="middle">
#> <textPath xlink:href="#L17" startOffset="50%">yy</textPath>
#> </text>
#> <path id="L18" d="M 315 374.019237886467 L 378.786796564404 321.213203435596" stroke-width="2" stroke="black" marker-end="url(#arr)" />
#> <text fill="black" font-size="20" font-family="Latin Modern Math, arial, Arial, sans" font-style="italic" text-anchor="middle">
#> <textPath xlink:href="#L18" startOffset="50%">tt</textPath>
#> </text>
#> <path id="L19" d="M 300 870 L 400 330" stroke-width="2" stroke="black" marker-end="url(#arr)" />
#> <text fill="black" font-size="20" font-family="Latin Modern Math, arial, Arial, sans" font-style="italic" text-anchor="middle">
#> <textPath xlink:href="#L19" startOffset="50%">ss</textPath>
#> </text>
#> <circle cx="400" cy="300" r="30" stroke-width="2" stroke="black" fill="white"/>
#> <text x="400" y="300" fill="black" font-size="20" font-family="Latin Modern Math, arial, Arial, sans" font-style="italic" dominant-baseline="central" text-anchor="middle">α</text>
#> <rect width="42.4264068711929" height="42.4264068711929" x="278.786796564404" y="178.786796564404" stroke-width="2" stroke="black" fill="white" />
#> <text x="300" y="200" fill="black" font-size="20" font-family="Latin Modern Math, arial, Arial, sans" font-style="italic" dominant-baseline="central" text-anchor="middle">x<tspan dy="5" font-size="15">1</tspan></text>
#> <rect width="42.4264068711929" height="42.4264068711929" x="378.786796564404" y="178.786796564404" stroke-width="2" stroke="black" fill="white" />
#> <text x="400" y="200" fill="black" font-size="20" font-family="Latin Modern Math, arial, Arial, sans" font-style="italic" dominant-baseline="central" text-anchor="middle">x<tspan dy="5" font-size="15">2</tspan></text>
#> <rect width="42.4264068711929" height="42.4264068711929" x="478.786796564404" y="178.786796564404" stroke-width="2" stroke="black" fill="white" />
#> <text x="500" y="200" fill="black" font-size="20" font-family="Latin Modern Math, arial, Arial, sans" font-style="italic" dominant-baseline="central" text-anchor="middle">x<tspan dy="5" font-size="15">3</tspan></text>
#> <polygon points="285,374.019237886467 315,374.019237886467 330,400 315,425.980762113533 285,425.980762113533 270,400" stroke-width="2" stroke="black" fill="none" />
#> <text x="300" y="400" fill="black" font-size="20" font-family="Latin Modern Math, arial, Arial, sans" font-style="italic" dominant-baseline="central" text-anchor="middle">β</text>
#> <rect width="42.4264068711929" height="42.4264068711929" x="178.786796564404" y="278.786796564404" stroke-width="2" stroke="black" fill="white" />
#> <text x="200" y="300" fill="black" font-size="20" font-family="Latin Modern Math, arial, Arial, sans" font-style="italic" dominant-baseline="central" text-anchor="middle">x<tspan dy="5" font-size="15">4</tspan></text>
#> <rect width="42.4264068711929" height="42.4264068711929" x="178.786796564404" y="378.786796564404" stroke-width="2" stroke="black" fill="white" />
#> <text x="200" y="400" fill="black" font-size="20" font-family="Latin Modern Math, arial, Arial, sans" font-style="italic" dominant-baseline="central" text-anchor="middle">x<tspan dy="5" font-size="15">5</tspan></text>
#> <rect width="42.4264068711929" height="42.4264068711929" x="178.786796564404" y="478.786796564404" stroke-width="2" stroke="black" fill="white" />
#> <text x="200" y="500" fill="black" font-size="20" font-family="Latin Modern Math, arial, Arial, sans" font-style="italic" dominant-baseline="central" text-anchor="middle">x<tspan dy="5" font-size="15">6</tspan></text>
#> <circle cx="300" cy="900" r="30" stroke-width="2" stroke="black" fill="white"/>
#> <text x="300" y="900" fill="black" font-size="20" font-family="Latin Modern Math, arial, Arial, sans" font-style="italic" dominant-baseline="central" text-anchor="middle">γ</text>
#> <rect width="42.4264068711929" height="42.4264068711929" x="178.786796564404" y="778.786796564404" stroke-width="2" stroke="black" fill="white" />
#> <text x="200" y="800" fill="black" font-size="20" font-family="Latin Modern Math, arial, Arial, sans" font-style="italic" dominant-baseline="central" text-anchor="middle">x<tspan dy="5" font-size="15">7</tspan></text>
#> <rect width="42.4264068711929" height="42.4264068711929" x="178.786796564404" y="878.786796564404" stroke-width="2" stroke="black" fill="white" />
#> <text x="200" y="900" fill="black" font-size="20" font-family="Latin Modern Math, arial, Arial, sans" font-style="italic" dominant-baseline="central" text-anchor="middle">x<tspan dy="5" font-size="15">8</tspan></text>
#> <rect width="42.4264068711929" height="42.4264068711929" x="178.786796564404" y="978.786796564404" stroke-width="2" stroke="black" fill="white" />
#> <text x="200" y="1000" fill="black" font-size="20" font-family="Latin Modern Math, arial, Arial, sans" font-style="italic" dominant-baseline="central" text-anchor="middle">x<tspan dy="5" font-size="15">9</tspan></text>
#> <circle cx="500" cy="600" r="30" stroke-width="2" stroke="black" fill="white"/>
#> <text x="500" y="600" fill="black" font-size="20" font-family="Latin Modern Math, arial, Arial, sans" font-style="italic" dominant-baseline="central" text-anchor="middle">Ξ</text>
#> <rect width="42.4264068711929" height="42.4264068711929" x="578.786796564404" y="478.786796564404" stroke-width="2" stroke="black" fill="white" />
#> <text x="600" y="500" fill="black" font-size="20" font-family="Latin Modern Math, arial, Arial, sans" font-style="italic" dominant-baseline="central" text-anchor="middle">x<tspan dy="5" font-size="15">10</tspan></text>
#> <rect width="42.4264068711929" height="42.4264068711929" x="578.786796564404" y="578.786796564404" stroke-width="2" stroke="black" fill="white" />
#> <text x="600" y="600" fill="black" font-size="20" font-family="Latin Modern Math, arial, Arial, sans" font-style="italic" dominant-baseline="central" text-anchor="middle">x<tspan dy="5" font-size="15">11</tspan></text>
#> <rect width="42.4264068711929" height="42.4264068711929" x="578.786796564404" y="678.786796564404" stroke-width="2" stroke="black" fill="white" />
#> <text x="600" y="700" fill="black" font-size="20" font-family="Latin Modern Math, arial, Arial, sans" font-style="italic" dominant-baseline="central" text-anchor="middle">x<tspan dy="5" font-size="15">12</tspan></text>
#> <rect width="42.4264068711929" height="42.4264068711929" x="578.786796564404" y="778.786796564404" stroke-width="2" stroke="black" fill="white" />
#> <text x="600" y="800" fill="black" font-size="20" font-family="Latin Modern Math, arial, Arial, sans" font-style="italic" dominant-baseline="central" text-anchor="middle">x<tspan dy="5" font-size="15">13</tspan></text>
#> <polygon points="500,270 525.980762113533,315 474.019237886467,315" stroke-width="2" stroke="black" fill="none" />
#> <text x="500" y="300" fill="black" font-size="20" font-family="Latin Modern Math, arial, Arial, sans" font-style="italic" dominant-baseline="central" text-anchor="middle">1</text>
#> <rect width="42.4264068711929" height="42.4264068711929" x="278.786796564404" y="478.786796564404" stroke-width="2" stroke="black" fill="white" />
#> <text x="300" y="500" fill="black" font-size="20" font-family="Latin Modern Math, arial, Arial, sans" font-style="italic" dominant-baseline="central" text-anchor="middle">Θ<tspan dy="5" font-size="15">1</tspan></text>
#> </svg>
modelml <- '
level: 1
fw =~ 1*y_1 + y_2 + y_3 + y_5
level: 2
fb =~ 1*y_1 + y_2 + y_3 + y_4
y_2 ~~ cv24 * y_4
'
testml <- lav_model_plotinfo(modelml)
testml <- lav_plotinfo_positions(testml)
# in the line hereunder no file is given, so output to R console
lav_plotinfo_svgcode(testml, sloped_labels = FALSE, standalone = TRUE,
auto_subscript = FALSE)
#> <!DOCTYPE html>
#> <html>
#> <body>
#> <h2>SVG diagram created by lav_plot R package</h2>
#> <svg width="500" height="1200" version="1.1" xmlns="http://www.w3.org/2000/svg" xmlns:xlink="http://www.w3.org/1999/xlink">
#> <rect width="100%" height="100%" fill="white" />
#> <defs>
#> <marker id="arr" markerWidth="6" markerHeight="6"
#> refX="6" refY="2.5" orient="auto">
#> <path d="M 0 0 L 6 2.5 L 0 5 L 2 2.5 z" fill="black" />
#> </marker>
#> <marker id="sarr" markerWidth="6" markerHeight="6"
#> refX="0" refY="2.5" orient="auto">
#> <path d="M 0 2.5 L 6 0 L 4 2.5 L 6 5 z" fill="black" />
#> </marker>
#> </defs>
#> <path d="M 1 600 L 400 600" stroke="black"/>
#> <path d="M 270 300 L 221.213203435596 200" stroke-width="2" stroke="black" marker-end="url(#arr)" />
#> <path id="L1" d="M 221.213203435596 200 L 270 300" stroke-width="0" stroke="none" fill="none" />
#> <text x="245.606601717798" y="250" fill="black" font-size="20" font-family="Latin Modern Math, arial, Arial, sans" font-style="italic" text-anchor="end">1</text>
#> <path d="M 270 300 L 221.213203435596 300" stroke-width="2" stroke="black" marker-end="url(#arr)" />
#> <path id="L2" d="M 221.213203435596 300 L 270 300" stroke-width="0" stroke="none" fill="none" />
#> <path d="M 270 300 L 221.213203435596 400" stroke-width="2" stroke="black" marker-end="url(#arr)" />
#> <path id="L3" d="M 221.213203435596 400 L 270 300" stroke-width="0" stroke="none" fill="none" />
#> <path d="M 270 300 L 221.213203435596 500" stroke-width="2" stroke="black" marker-end="url(#arr)" />
#> <path id="L4" d="M 221.213203435596 500 L 270 300" stroke-width="0" stroke="none" fill="none" />
#> <path id="L5" d="M 221.213203435596 500 Q 86.7544467966324 400 221.213203435596 300" stroke-width="2" stroke="black" fill="none" marker-start="url(#sarr)" marker-end="url(#arr)" />
#> <text x="153.983825116114" y="400" fill="black" font-size="20" font-family="Latin Modern Math, arial, Arial, sans" font-style="italic" >cv24</text>
#> <path d="M 270 800 L 221.213203435596 700" stroke-width="2" stroke="black" marker-end="url(#arr)" />
#> <path id="L6" d="M 221.213203435596 700 L 270 800" stroke-width="0" stroke="none" fill="none" />
#> <text x="245.606601717798" y="750" fill="black" font-size="20" font-family="Latin Modern Math, arial, Arial, sans" font-style="italic" text-anchor="end">1</text>
#> <path d="M 270 800 L 221.213203435596 800" stroke-width="2" stroke="black" marker-end="url(#arr)" />
#> <path id="L7" d="M 221.213203435596 800 L 270 800" stroke-width="0" stroke="none" fill="none" />
#> <path d="M 270 800 L 221.213203435596 900" stroke-width="2" stroke="black" marker-end="url(#arr)" />
#> <path id="L8" d="M 221.213203435596 900 L 270 800" stroke-width="0" stroke="none" fill="none" />
#> <path d="M 270 800 L 221.213203435596 1000" stroke-width="2" stroke="black" marker-end="url(#arr)" />
#> <path id="L9" d="M 221.213203435596 1000 L 270 800" stroke-width="0" stroke="none" fill="none" />
#> <circle cx="300" cy="300" r="30" stroke-width="2" stroke="black" fill="white"/>
#> <text x="300" y="300" fill="black" font-size="20" font-family="Latin Modern Math, arial, Arial, sans" font-style="italic" dominant-baseline="central" text-anchor="middle">fb</text>
#> <rect width="42.4264068711929" height="42.4264068711929" x="178.786796564404" y="178.786796564404" rx="7.07106781186548" ry="7.07106781186548" stroke-width="2" stroke="black" fill="#add8e6" />
#> <text x="200" y="200" fill="black" font-size="20" font-family="Latin Modern Math, arial, Arial, sans" font-style="italic" dominant-baseline="central" text-anchor="middle">y<tspan dy="5" font-size="15">1</tspan></text>
#> <rect width="42.4264068711929" height="42.4264068711929" x="178.786796564404" y="278.786796564404" rx="7.07106781186548" ry="7.07106781186548" stroke-width="2" stroke="black" fill="#add8e6" />
#> <text x="200" y="300" fill="black" font-size="20" font-family="Latin Modern Math, arial, Arial, sans" font-style="italic" dominant-baseline="central" text-anchor="middle">y<tspan dy="5" font-size="15">2</tspan></text>
#> <rect width="42.4264068711929" height="42.4264068711929" x="178.786796564404" y="378.786796564404" rx="7.07106781186548" ry="7.07106781186548" stroke-width="2" stroke="black" fill="#add8e6" />
#> <text x="200" y="400" fill="black" font-size="20" font-family="Latin Modern Math, arial, Arial, sans" font-style="italic" dominant-baseline="central" text-anchor="middle">y<tspan dy="5" font-size="15">3</tspan></text>
#> <rect width="42.4264068711929" height="42.4264068711929" x="178.786796564404" y="478.786796564404" stroke-width="2" stroke="black" fill="white" />
#> <text x="200" y="500" fill="black" font-size="20" font-family="Latin Modern Math, arial, Arial, sans" font-style="italic" dominant-baseline="central" text-anchor="middle">y<tspan dy="5" font-size="15">4</tspan></text>
#> <circle cx="300" cy="800" r="30" stroke-width="2" stroke="black" fill="white"/>
#> <text x="300" y="800" fill="black" font-size="20" font-family="Latin Modern Math, arial, Arial, sans" font-style="italic" dominant-baseline="central" text-anchor="middle">fw</text>
#> <rect width="42.4264068711929" height="42.4264068711929" x="178.786796564404" y="678.786796564404" rx="7.07106781186548" ry="7.07106781186548" stroke-width="2" stroke="black" fill="#90ee90" />
#> <text x="200" y="700" fill="black" font-size="20" font-family="Latin Modern Math, arial, Arial, sans" font-style="italic" dominant-baseline="central" text-anchor="middle">y<tspan dy="5" font-size="15">1</tspan></text>
#> <rect width="42.4264068711929" height="42.4264068711929" x="178.786796564404" y="778.786796564404" rx="7.07106781186548" ry="7.07106781186548" stroke-width="2" stroke="black" fill="#90ee90" />
#> <text x="200" y="800" fill="black" font-size="20" font-family="Latin Modern Math, arial, Arial, sans" font-style="italic" dominant-baseline="central" text-anchor="middle">y<tspan dy="5" font-size="15">2</tspan></text>
#> <rect width="42.4264068711929" height="42.4264068711929" x="178.786796564404" y="878.786796564404" rx="7.07106781186548" ry="7.07106781186548" stroke-width="2" stroke="black" fill="#90ee90" />
#> <text x="200" y="900" fill="black" font-size="20" font-family="Latin Modern Math, arial, Arial, sans" font-style="italic" dominant-baseline="central" text-anchor="middle">y<tspan dy="5" font-size="15">3</tspan></text>
#> <rect width="42.4264068711929" height="42.4264068711929" x="178.786796564404" y="978.786796564404" stroke-width="2" stroke="black" fill="white" />
#> <text x="200" y="1000" fill="black" font-size="20" font-family="Latin Modern Math, arial, Arial, sans" font-style="italic" dominant-baseline="central" text-anchor="middle">y<tspan dy="5" font-size="15">5</tspan></text>
#> </svg>
#> </body>
#> </html>
if (FALSE) { # \dontrun{
# example creating html file with the above diagrams
zz <- file("demosvg.html", open="w")
writeLines(c(
'<!DOCTYPE html>',
'<html>',
'<body>',
'<h2>SVG diagrams created by lav_plot R package</h2>'),
zz)
lav_plotinfo_svgcode(test, outfile = "temp.svg")
tmp <- readLines("tmp.svg")
writeLines(tmp, zz)
writeLines("<br />", zz)
lav_plotinfo_svgcode(testml, outfile = "temp.svg", sloped_labels = FALSE,
standalone = TRUE)
tmp <- readLines("tmp.svg")
writeLines(tmp, zz)
writeLines(c("</body>", "</html>"), zz)
close(zz)
browseURL("demosvg.html")
} # }