cjvanlissa / tidySEM

54 stars 7 forks source link

Arrow head settings and formating #94

Closed dumaskvn closed 1 month ago

dumaskvn commented 1 month ago

Hi ! Thanks a lot for this amazing and efficient package. This is not so much of an issue, but rather a request for future devs ? Although I'm very satisfied by the results of SEM plot using tidySEM, I am a bit sad with the appearance of arrow heads, especially when correlating edges size with standardized effects (see exemple below). It would be nice to have a bit more control on arrow head settings (arrow function from ggplot is amazing for that!). From what I understood so far, I would have to plot all edges almost "by hand" to be able to clean the arrow design.

Maybe I shouldn't be playing with edge size in this way, and it is why tidySEM handles it badly ? A bit of exemple below to show this arrow head issue.

Anyway, sorry for bothering you with this tiny details, and thanks for the huge work on tidySEM !

Standard settings :

library(tidySEM)
library(lavaan)
library(dplyr)
library(piecewiseSEM)
library(magrittr)
# raw plot
semdata <- data.frame(y1 = rnorm(100, 0, 1)) %>%
  mutate(x1 = rnorm(100, 0, 1),
         x2 = y1*10 + rnorm(100, 0, 1),
         x3 = y1*50 + rnorm(100, 0, 1))
model <- 'y1  ~ x1
          y1  ~ x2
          y1  ~ x3'
fit <- cfa(model, data=semdata)
layout <- matrix(ncol = 3, nrow = 2, c("x1", "", "x2", "y1", "x3", ""))
graph_sem(model = fit, layout = layout)

With edge size as function of standardized effect. I added transparency to the nodes for convenience.

# size
prepare_graph(fit, , layout = layout) %>%
  edit_graph({ size = as.numeric(est_std) * 5}, element = "edges") %>%
  edit_graph({ alpha = 0.2}, element = "nodes") %>%
  plot

Created on 2024-05-31 with reprex v2.1.0

cjvanlissa commented 1 month ago

Not sure what you're requesting... the graphs are ggplot objects, so they can be further customized using additional geoms. You should be able to just add something like + geom_line(arrow = arrow(length=unit(0.7,"cm"), ends="last", type = "closed")).