Skip to contents

Create 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, Aerial, 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, add code to make a html file with svg in it? If FALSE (default) the outfile - if specified - must have a 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 normal font size to use.

idx.font.size

An integer specifying font size to use for a subscript.

dy

An integer specifying the distance to move the baseline of the subscript.

mlovcolors

Array of two colors for distinguishing ov nodes with same name between the levels in 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

Are labels to be in italic font? If FALSE the labels will be shown in mathrm font.

auto.subscript

Logical, see lav_label_code.

Value

NULL (invisible)

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="900" height="1000" 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 700 L 221.213203435596 600" stroke-width="2" stroke="black" marker-end="url(#arr)" />
#> <path id="L7" d="M 221.213203435596 600 L 270 700" stroke-width="0" stroke="none" fill="none" />
#> <path d="M 270 700 L 221.213203435596 700" stroke-width="2" stroke="black" marker-end="url(#arr)" />
#> <path id="L8" d="M 221.213203435596 700 L 270 700" stroke-width="0" stroke="none" fill="none" />
#> <path d="M 270 700 L 221.213203435596 800" stroke-width="2" stroke="black" marker-end="url(#arr)" />
#> <path id="L9" d="M 221.213203435596 800 L 270 700" stroke-width="0" stroke="none" fill="none" />
#> <path id="L10" d="M 630 500 L 678.786796564404 400" stroke-width="2" stroke="black" marker-end="url(#arr)" />
#> <path id="L11" d="M 630 500 L 678.786796564404 500" stroke-width="2" stroke="black" marker-end="url(#arr)" />
#> <path id="L12" d="M 630 500 L 678.786796564404 600" stroke-width="2" stroke="black" marker-end="url(#arr)" />
#> <path id="L13" d="M 630 500 L 678.786796564404 700" stroke-width="2" stroke="black" marker-end="url(#arr)" />
#> <path id="L14" d="M 421.213203435596 321.213203435596 L 578.786796564404 478.786796564404" stroke-width="2" stroke="black" marker-end="url(#arr)" />
#> <text fill="black" font-size="20" font-family="Latin Modern Math, arial, Aerial, sans" font-style="italic" text-anchor="middle">
#> <textPath xlink:href="#L14" startOffset="50%">v</textPath>
#> </text>
#> <path id="L15" d="M 330 400 L 570 500" stroke-width="2" stroke="black" marker-end="url(#arr)" />
#> <text fill="black" font-size="20" font-family="Latin Modern Math, arial, Aerial, sans" font-style="italic" text-anchor="middle">
#> <textPath xlink:href="#L15" startOffset="50%">t</textPath>
#> </text>
#> <path id="L16" d="M 600 415 L 600 470" stroke-width="2" stroke="black" marker-end="url(#arr)" />
#> <path id="L17" d="M 321.213203435596 300 L 370 300" stroke-width="2" stroke="black" marker-end="url(#arr)" />
#> <text fill="black" font-size="20" font-family="Latin Modern Math, arial, Aerial, 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, Aerial, sans" font-style="italic" text-anchor="middle">
#> <textPath xlink:href="#L18" startOffset="50%">tt</textPath>
#> </text>
#> <path id="L19" d="M 300 670 L 400 330" stroke-width="2" stroke="black" marker-end="url(#arr)" />
#> <text fill="black" font-size="20" font-family="Latin Modern Math, arial, Aerial, 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, Aerial, sans" font-style="italic" dominant-baseline="central" text-anchor="middle">&#945;</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, Aerial, 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, Aerial, 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, Aerial, 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, Aerial, sans" font-style="italic" dominant-baseline="central" text-anchor="middle">&#946;</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, Aerial, 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, Aerial, 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, Aerial, sans" font-style="italic" dominant-baseline="central" text-anchor="middle">x<tspan dy="5" font-size="15">6</tspan></text>
#> <circle cx="300" cy="700" r="30" stroke-width="2" stroke="black" fill="white"/>
#> <text x="300" y="700" fill="black" font-size="20" font-family="Latin Modern Math, arial, Aerial, sans" font-style="italic" dominant-baseline="central" text-anchor="middle">&#947;</text>
#> <rect width="42.4264068711929" height="42.4264068711929" x="178.786796564404" y="578.786796564404" stroke-width="2" stroke="black" fill="white" />
#> <text x="200" y="600" fill="black" font-size="20" font-family="Latin Modern Math, arial, Aerial, 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="678.786796564404" stroke-width="2" stroke="black" fill="white" />
#> <text x="200" y="700" fill="black" font-size="20" font-family="Latin Modern Math, arial, Aerial, 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="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, Aerial, sans" font-style="italic" dominant-baseline="central" text-anchor="middle">x<tspan dy="5" font-size="15">9</tspan></text>
#> <circle cx="600" cy="500" r="30" stroke-width="2" stroke="black" fill="white"/>
#> <text x="600" y="500" fill="black" font-size="20" font-family="Latin Modern Math, arial, Aerial, sans" font-style="italic" dominant-baseline="central" text-anchor="middle">&#926;</text>
#> <rect width="42.4264068711929" height="42.4264068711929" x="678.786796564404" y="378.786796564404" stroke-width="2" stroke="black" fill="white" />
#> <text x="700" y="400" fill="black" font-size="20" font-family="Latin Modern Math, arial, Aerial, 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="678.786796564404" y="478.786796564404" stroke-width="2" stroke="black" fill="white" />
#> <text x="700" y="500" fill="black" font-size="20" font-family="Latin Modern Math, arial, Aerial, 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="678.786796564404" y="578.786796564404" stroke-width="2" stroke="black" fill="white" />
#> <text x="700" y="600" fill="black" font-size="20" font-family="Latin Modern Math, arial, Aerial, 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="678.786796564404" y="678.786796564404" stroke-width="2" stroke="black" fill="white" />
#> <text x="700" y="700" fill="black" font-size="20" font-family="Latin Modern Math, arial, Aerial, sans" font-style="italic" dominant-baseline="central" text-anchor="middle">x<tspan dy="5" font-size="15">13</tspan></text>
#> <polygon points="600,370 625.980762113533,415 574.019237886467,415" stroke-width="2" stroke="black" fill="none" />
#> <text x="600" y="400" fill="black" font-size="20" font-family="Latin Modern Math, arial, Aerial, sans" font-style="italic" dominant-baseline="central" text-anchor="middle">1</text>
#> <rect width="42.4264068711929" height="42.4264068711929" x="278.786796564404" y="278.786796564404" stroke-width="2" stroke="black" fill="white" />
#> <text x="300" y="300" fill="black" font-size="20" font-family="Latin Modern Math, arial, Aerial, sans" font-style="italic" dominant-baseline="central" text-anchor="middle">&#920;<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="700" height="800" 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 400 L 600 400" stroke="black"/>
#> <path d="M 300 270 L 200 221.213203435596" stroke-width="2" stroke="black" marker-end="url(#arr)" />
#> <path id="L1" d="M 200 221.213203435596 L 300 270" stroke-width="0" stroke="none" fill="none" />
#> <text x="250" y="245.606601717798" fill="black" font-size="20" font-family="Latin Modern Math, arial, Aerial, sans" font-style="italic" text-anchor="end">1</text>
#> <path id="L2" d="M 300 270 L 300 221.213203435596" stroke-width="2" stroke="black" marker-end="url(#arr)" />
#> <path id="L3" d="M 300 270 L 400 221.213203435596" stroke-width="2" stroke="black" marker-end="url(#arr)" />
#> <path id="L4" d="M 300 270 L 500 221.213203435596" stroke-width="2" stroke="black" marker-end="url(#arr)" />
#> <path d="M 500 178.786796564404 Q 400 70 300 178.786796564404" stroke-width="2" stroke="black" fill="none" marker-start="url(#sarr)" marker-end="url(#arr)" />
#> <path id="L5" d="M 300 178.786796564404 Q 400 70 500 178.786796564404" stroke-width="0" stroke="none" fill="none" />
#> <text x="400" y="124.393398282202" fill="black" font-size="20" font-family="Latin Modern Math, arial, Aerial, sans" font-style="italic"  >cv24</text>
#> <path d="M 300 570 L 200 521.213203435596" stroke-width="2" stroke="black" marker-end="url(#arr)" />
#> <path id="L6" d="M 200 521.213203435596 L 300 570" stroke-width="0" stroke="none" fill="none" />
#> <text x="250" y="545.606601717798" fill="black" font-size="20" font-family="Latin Modern Math, arial, Aerial, sans" font-style="italic" text-anchor="end">1</text>
#> <path id="L7" d="M 300 570 L 300 521.213203435596" stroke-width="2" stroke="black" marker-end="url(#arr)" />
#> <path id="L8" d="M 300 570 L 400 521.213203435596" stroke-width="2" stroke="black" marker-end="url(#arr)" />
#> <path id="L9" d="M 300 570 L 500 521.213203435596" stroke-width="2" stroke="black" marker-end="url(#arr)" />
#> <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, Aerial, 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, Aerial, 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="278.786796564404" y="178.786796564404" rx="7.07106781186548" ry="7.07106781186548" stroke-width="2" stroke="black" fill="#add8e6" />
#> <text x="300" y="200" fill="black" font-size="20" font-family="Latin Modern Math, arial, Aerial, 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="378.786796564404" y="178.786796564404" rx="7.07106781186548" ry="7.07106781186548" stroke-width="2" stroke="black" fill="#add8e6" />
#> <text x="400" y="200" fill="black" font-size="20" font-family="Latin Modern Math, arial, Aerial, 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="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, Aerial, sans" font-style="italic" dominant-baseline="central" text-anchor="middle">y<tspan dy="5" font-size="15">4</tspan></text>
#> <circle cx="300" cy="600" r="30" stroke-width="2" stroke="black" fill="white"/>
#> <text x="300" y="600" fill="black" font-size="20" font-family="Latin Modern Math, arial, Aerial, sans" font-style="italic" dominant-baseline="central" text-anchor="middle">fw</text>
#> <rect width="42.4264068711929" height="42.4264068711929" x="178.786796564404" y="478.786796564404" rx="7.07106781186548" ry="7.07106781186548" stroke-width="2" stroke="black" fill="#90ee90" />
#> <text x="200" y="500" fill="black" font-size="20" font-family="Latin Modern Math, arial, Aerial, 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="278.786796564404" y="478.786796564404" rx="7.07106781186548" ry="7.07106781186548" stroke-width="2" stroke="black" fill="#90ee90" />
#> <text x="300" y="500" fill="black" font-size="20" font-family="Latin Modern Math, arial, Aerial, 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="378.786796564404" y="478.786796564404" rx="7.07106781186548" ry="7.07106781186548" stroke-width="2" stroke="black" fill="#90ee90" />
#> <text x="400" y="500" fill="black" font-size="20" font-family="Latin Modern Math, arial, Aerial, 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="478.786796564404" y="478.786796564404" stroke-width="2" stroke="black" fill="white" />
#> <text x="500" y="500" fill="black" font-size="20" font-family="Latin Modern Math, arial, Aerial, 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")
} # }