Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
4 changes: 2 additions & 2 deletions DESCRIPTION
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
Package: semptools
Title: Customizing Structural Equation Modelling Plots
Version: 0.3.2.2
Version: 0.3.2.3
Authors@R: c(
person(given = "Shu Fai",
family = "Cheung",
Expand Down Expand Up @@ -33,5 +33,5 @@ Suggests:
rmarkdown,
magrittr
Roxygen: list(markdown = TRUE)
RoxygenNote: 7.3.2
RoxygenNote: 7.3.3
VignetteBuilder: knitr
8 changes: 7 additions & 1 deletion NEWS.md
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
# semptools 0.3.2.2
# semptools 0.3.2.3

## New Features

Expand All @@ -12,6 +12,12 @@
latent variables
(0.3.2.1)

- Updated `set_edge_label_position()`
to handle bidirectional edges specified
in an order different from that
in `lavaan`'s parameter table.
(0.3.2.3)

# semptools 0.3.2

## New Features
Expand Down
12 changes: 12 additions & 0 deletions R/set_edge_label_position.R
Original file line number Diff line number Diff line change
Expand Up @@ -61,6 +61,8 @@
#'@export

set_edge_label_position <- function(semPaths_plot, position_list = NULL) {
# TODO:
# - Update to use set_edge_attribute().
if (is.null(position_list)) {
stop("position_list not specified.")
}
Expand Down Expand Up @@ -90,6 +92,16 @@ set_edge_label_position <- function(semPaths_plot, position_list = NULL) {
edge_index(semPaths_plot, from = x$from, to = x$to)
})
position_new[position_index] <- sapply(position_list, function(x) x$new_position)

# Check bidirectional edges
position_list2 <- position_list[which(semPaths_plot$Edge$bidirectional[position_index])]
if (length(position_list2) > 0) {
position_index2 <- sapply(position_list2, function(x) {
edge_index(semPaths_plot, from = x$to, to = x$from)
})
position_new[position_index2] <- sapply(position_list2, function(x) x$new_position)
}

semPaths_plot$graphAttributes$Edges$edge.label.position <- position_new
semPaths_plot
}
2 changes: 1 addition & 1 deletion README.md
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,7 @@
[![R-CMD-check](https://github.com/sfcheung/semptools/actions/workflows/R-CMD-check.yaml/badge.svg)](https://github.com/sfcheung/semptools/actions/workflows/R-CMD-check.yaml)
<!-- badges: end -->

(Version 0.3.2.2, updated on 2025-08-17, [release history](https://sfcheung.github.io/semptools/news/index.html))
(Version 0.3.2.3, updated on 2026-01-18, [release history](https://sfcheung.github.io/semptools/news/index.html))

# semptools <img src="man/figures/logo.png" align="right" height="150" />

Expand Down
42 changes: 42 additions & 0 deletions tests/testthat/test-set_edge_label_position.R
Original file line number Diff line number Diff line change
@@ -0,0 +1,42 @@
library(lavaan)
library(semPlot)

mod_pa <-
'x1 ~~ x2
x3 ~ x1 + x2
x4 ~ x1 + x2
'
fit_pa <- lavaan::sem(mod_pa, pa_example)

m <- matrix(c("x1", NA, "x3",
"x2", NA, "x4"), byrow = TRUE, 2, 3)
p_pa <- semPaths(fit_pa, whatLabels = "est",
sizeMan = 10,
edge.label.cex = 1.15,
nCharNodes = 0,
nCharEdges = 0,
layout = m,
DoNotPlot = TRUE)

subset(parameterEstimates(fit_pa), op == "~~")

p2 <- set_edge_label_position(
p_pa,
c("x1~~x2" = .70,
"x3~~ x3" = .10))
# plot(p2)
p2b <- set_edge_label_position(
p_pa,
c("x2~~x1" = .70,
"x3~~ x3" = .10))
# plot(p2b)

test_that("set_edge_lable_position", {
expect_true(p2$graphAttributes$Edges$edge.label.position[6] !=
p_pa$graphAttributes$Edges$edge.label.position[6])
expect_true(p2$graphAttributes$Edges$edge.label.position[1] !=
p_pa$graphAttributes$Edges$edge.label.position[1])
expect_true(p2b$graphAttributes$Edges$edge.label.position[1] !=
p_pa$graphAttributes$Edges$edge.label.position[1])
})

Loading