Ecosyste.ms: Awesome

An open API service indexing awesome lists of open source software.

Awesome Lists | Featured Topics | Projects

https://github.com/alishinski/lavaanPlot

R package for plotting lavaan path models with DiagrammeR
https://github.com/alishinski/lavaanPlot

Last synced: about 2 months ago
JSON representation

R package for plotting lavaan path models with DiagrammeR

Awesome Lists containing this project

README

        

---
output: github_document
always_allow_html: true
---

```{r setup, include=FALSE}
knitr::opts_chunk$set(echo = TRUE)
```

[![CRAN status](https://www.r-pkg.org/badges/version/lavaanPlot)](https://cran.r-project.org/package=lavaanPlot)
[![](https://cranlogs.r-pkg.org/badges/lavaanPlot)](https://cran.r-project.org/package=lavaanPlot)
[![R-CMD-check](https://github.com/alishinski/lavaanPlot/workflows/R-CMD-check/badge.svg)](https://github.com/alishinski/lavaanPlot/actions)

## News

Version 0.7.0, is introducing a new iteration of the `lavaanPlot` function, called `lavaanPlot2`. Check out a new vignette explaining some of the changes here: [Improvements to lavaanPlot](https://lavaanplot.alexlishinski.com/improvements_to_lavaanplot)

Version 0.8.0, is introducing conditional formatting to the `lavaanPlot` package, using the new `formatting` function. Check out the new vignette explaining the changes here: [Conditional Formatting in LavaanPlot](https://lavaanplot.alexlishinski.com/Conditional_Formatting)

## Background

The lavaan package is an excellent package for structural equation models, and the DiagrammeR package is an excellent package for producing nice looking graph diagrams. As of right now, the lavaan package has no built in plotting functions for models, and the available options from external packages don't look as nice and aren't as easy to use as DiagrammeR, in my opinion. Of course, you can use DiagrammeR to build path diagrams for your models, but it requires you to build the diagram specification manually. This package exists to streamline that process, allowing you to plot your lavaan models directly, without having to translate them into the DOT language specification that DiagrammeR uses.

## Installation

You can install lavaanPlot from CRAN with:

```{r, eval = F}
install.packages("lavaanPlot")
```

You can also install the development version of lavaanPlot from GitHub with:

```{r gh-installation, eval = FALSE}
install.packages("devtools")
devtools::install_github("alishinski/lavaanPlot")
```

## Examples

Here's a quick example using the `mtcars` data set.

First fit your lavaan model. The package supports plotting lavaan regression relationships and latent variable - indicator relationships.

```{r}
library(lavaan)
library(lavaanPlot)

model <- 'mpg ~ cyl + disp + hp
qsec ~ disp + hp + wt'

fit <- sem(model, data = mtcars)
summary(fit)
```

Then using that model fit object, simply call the `lavaanPlot` function, specifying your desired graph parameters.

```{r}
lavaanPlot(model = fit, node_options = list(shape = "box", fontname = "Helvetica"), edge_options = list(color = "grey"), coefs = F)
```

There is also functionality to save these plots as images and embed them in pdf output documents.

```{r, eval = FALSE}
model <- 'mpg ~ cyl + disp + hp
qsec ~ disp + hp + wt'

fit <- sem(model, data = mtcars)

pl <- lavaanPlot(model = fit)

# Example for pdf embed
embed_plot_pdf(pl, "plot2.pdf")

# Example for saving to .png
save_png(pl, "plot.png")
```

# Learning More

To learn more, check out the vignettes.

## Citing lavaanPlot

If you found lavaanPlot useful and used the plots in a publication, I ask that you please cite the package.

## Contributing

I welcome any and all contributions from people using the package, so please share github issues here if there are problems you encounter or features you want to suggest.