Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Fix path plot layout for moderator on a and c paths #43

Merged
merged 2 commits into from
Oct 27, 2023
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
92 changes: 52 additions & 40 deletions R/classicProcess.R
Original file line number Diff line number Diff line change
Expand Up @@ -1752,52 +1752,64 @@ ClassicProcess <- function(jaspResults, dataset = NULL, options) {
for (i in 1:length(igraph::V(graph)[isHigherOrderInt])) {
# Get vars of interaction
modIntVars <- igraph::V(graph)[isHigherOrderInt][i]$intVars[[1]]

nMods <- length(modIntVars)-1
# Get target var of interaction
# Get target vars of interaction
target <- igraph::E(graph)[.from(igraph::V(graph)[isHigherOrderInt][i]$name)]$target

for (l in 1:length(target)) {
# Delete edges from moderators to target variable
modIntVarHasEdgeToTarget <- sapply(modIntVars[-1], function(v) igraph::are_adjacent(graph, v, target[l]))

if (any(modIntVarHasEdgeToTarget)) {
graph <- igraph::delete_edges(graph,
paste(modIntVars[-1][modIntVarHasEdgeToTarget], target[l], sep = "|")
)
}

# Delete edges from moderators to target variable
modIntVarHasEdgeToTarget <- sapply(modIntVars[-1], function(v) igraph::are_adjacent(graph, v, target))

if (any(modIntVarHasEdgeToTarget)) {
graph <- igraph::delete_edges(graph,
paste(modIntVars[-1][modIntVarHasEdgeToTarget], target, sep = "|")
# Add helper nodes ("i"lj)
helperNodeNames <- paste0("i", i, l, 1:nMods)
graph <- igraph::add_vertices(graph,
nMods,
name = helperNodeNames,
posX = NA, posY = NA, isHigherOrderInt = FALSE
)
}

# Add helper nodes ("i"ij)
helperNodeNames <- paste0("i", i, 1:nMods)
graph <- igraph::add_vertices(graph,
nMods,
name = helperNodeNames,
posX = NA, posY = NA, isHigherOrderInt = FALSE
)

# Add edges from moderators to helper nodes
graph <- igraph::add_edges(graph,
edges = c(rbind(modIntVars[-1], helperNodeNames)),
source = modIntVars[-1],
target = helperNodeNames
)

for (j in 2:length(modIntVars)) {
# Pos of helper nodes is mean of source and target nodes
helperNodePosX <- mean(c(igraph::V(graph)[modIntVars[j-1]]$posX, igraph::V(graph)[target]$posX))
helperNodePosY <- mean(c(igraph::V(graph)[modIntVars[j-1]]$posY, igraph::V(graph)[target]$posY))
igraph::V(graph)[paste0("i", i, j-1)]$posX <- helperNodePosX
igraph::V(graph)[paste0("i", i, j-1)]$posY <- helperNodePosY
# Pos of moderator depends on index
if (j %% 2 == 0) {
# Place first moderator above source and target
igraph::V(graph)[modIntVars[j]]$posX <- helperNodePosX
igraph::V(graph)[modIntVars[j]]$posY <- .minMaxSubAddOne(igraph::V(graph)$posY)
} else {
# Place second moderator to the right of source and target
igraph::V(graph)[modIntVars[j]]$posX <- .minMaxSubAddOne(helperNodePosX)
igraph::V(graph)[modIntVars[j]]$posY <- helperNodePosY
# Add edges from moderators to helper nodes
graph <- igraph::add_edges(graph,
edges = c(rbind(modIntVars[-1], helperNodeNames)),
source = modIntVars[-1],
target = helperNodeNames
)

# Create new target variable to modify later
helperTarget <- target[l]

for (j in 2:length(modIntVars)) {
helperNodeName <- paste0("i", i, l, j-1)
# Pos of helper nodes is mean of source and target nodes
helperNodePosX <- mean(c(igraph::V(graph)[modIntVars[j-1]]$posX, igraph::V(graph)[helperTarget]$posX))
helperNodePosY <- mean(c(igraph::V(graph)[modIntVars[j-1]]$posY, igraph::V(graph)[helperTarget]$posY))

igraph::V(graph)[helperNodeName]$posX <- helperNodePosX
igraph::V(graph)[helperNodeName]$posY <- helperNodePosY

# Only assign pos of nodes that do not have pos yet
if (is.na(igraph::V(graph)[modIntVars[j]]$posX) && is.na(igraph::V(graph)[modIntVars[j]]$posY)) {
# Pos of moderator depends on index
if (j %% 2 == 0) {
# Place first moderator above source and target
igraph::V(graph)[modIntVars[j]]$posX <- helperNodePosX
igraph::V(graph)[modIntVars[j]]$posY <- .minMaxSubAddOne(igraph::V(graph)$posY)
} else {
# Place second moderator to the right of source and target
igraph::V(graph)[modIntVars[j]]$posX <- .minMaxSubAddOne(helperNodePosX)
igraph::V(graph)[modIntVars[j]]$posY <- helperNodePosY
}
}
# Set helper node as next target
helperTarget <- helperNodeName
}
# Set helper node as next target
target <- paste0("i", i, j-1)
}
}
}
Expand Down
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Loading
Loading