Ulpoad library
edges <- data.frame(
from = sample(1:12, 8),
to = sample(1:12, 8),
label = paste("interaction type", 1:8),
length = c(100, 500),
width = c(4, 1),
arrows = c("to", "from", "middle", "middle;to"),
dashes = c(TRUE, FALSE),
title = paste("interaction name", 1:8),
smooth = c(FALSE, TRUE),
shadow = c(FALSE, TRUE, FALSE, TRUE)
)
head(edges,3)
## from to label length width arrows dashes title
## 1 1 3 interaction type 1 100 4 to TRUE interaction name 1
## 2 3 4 interaction type 2 500 1 from FALSE interaction name 2
## 3 10 1 interaction type 3 100 4 middle TRUE interaction name 3
## smooth shadow
## 1 FALSE FALSE
## 2 TRUE TRUE
## 3 FALSE FALSE
## id group label position shape
## 1 1 A Node 1 center ellipse
## 2 2 B Node 2 center ellipse
## 3 3 A Node 3 edge ellipse
Save the graph in variable
g <-visNetwork::visNetwork(nodes, edges, height = "500px", width = "100%") %>%
visNetwork::visIgraphLayout(layout = "layout_as_tree")
## Warning in ctrl$objs[[1]](graph = ig, ...): At structural_properties.c:
## 3346 :graph contains a cycle, partial result is returned
# access the x and y co-ordinates to arrange the groups
coords <- g$x$nodes %>%
dplyr::mutate(x = abs(x)) %>%
dplyr::mutate(y = abs(y)) %>%
dplyr::mutate(x = ifelse(group %in% "A", -x, x)) %>%
dplyr::select(x, y) %>%
as.matrix()
coords
## x y
## [1,] -1.0000000 1.0
## [2,] 0.6666667 1.0
## [3,] -1.0000000 0.5
## [4,] 1.0000000 0.0
## [5,] -1.0000000 0.5
## [6,] 0.3333333 1.0
## [7,] 0.0000000 1.0
## [8,] 0.3333333 1.0
## [9,] -0.6666667 1.0
## [10,] 1.0000000 1.0
## [11,] -1.0000000 1.0
## [12,] 0.0000000 0.5
replot the network with the new co-ordinates
visNetwork::visNetwork(nodes, edges, height = "500px", width = "100%") %>%
visNetwork::visIgraphLayout(
layout = "layout.norm",
layoutMatrix = coords,
randomSeed = 1,
smooth = T
)