library(magrittr)
topo <- topo(trini_mod)
sankey(topo, debug = TRUE)
sankey(topo, layout = "stress")
sankey(topo(aquarium_mod), layout = "stress", edge_f = 0.5)
m <- new_networkModel() %>%
set_topo(c("subs -> NH3 -> subs",
"NH3 -> Q, E", "E -> Q -> E",
"E -> D, M")) %>%
set_steady("subs") %>%
set_prop_family("normal_sd")
ggtopo(m)
sankey(topo(m), layout = "stress")
# Debug visualization
## Helper functions
flows_from_topo <- function(x) {
x <- unclass(x) # Remove the "topo" class to treat it as a matrix
n_comps <- ncol(x)
links <- which(x > 0)
from <- links %/% n_comps + 1
to <- links %% n_comps
links <- tibble::tibble(from = from, to = to)
for (i in seq_len(nrow(links))) {
if (links$to[i] == 0) {
links$from[i] <- links$from[i] - 1
links$to[i] <- n_comps
}
stopifnot(x[links$to[i], links$from[i]] > 0)
}
flows <- tibble::tibble(from = colnames(x)[links$from],
to = rownames(x)[links$to])
return(flows)
}
nodes_from_topo <- function(x) {
nodes <- tibble::tibble(comp = colnames(x),
label = colnames(x))
return(nodes)
}
t <- topo(trini_mod)
nodes <- nodes_from_topo(t)
nodes$label <- as.list(nodes$label)
nodes$label[[2]] <- latex2exp::TeX("$\\beta$")
nodes$size <- runif(nrow(nodes), 1, 2)
flows <- flows_from_topo(t)
flows$width <- runif(nrow(flows), 0.2, 2)
z <- sankey(t, nodes = nodes, flows = flows, layout = "left2right",
debug = TRUE, node_f = 1, edge_f = 0.9, edge_n = 32,
cex_lab = 1.5)
# Stress layout
y <- new_networkModel() %>%
set_topo(c("subs -> NH3 -> subs",
"NH3 -> Q, E", "E -> Q -> E",
"E -> D, M")) %>%
set_steady("subs") %>%
set_prop_family("normal_sd")
y <- topo(y)
nodes <- nodes_from_topo(y)
nodes$size <- runif(nrow(nodes), 1, 10)
ggtopo(y, edge = "fan")
flows <- flows_from_topo(y)
flows$width <- runif(nrow(flows), 0.2, 5)
z <- sankey(y, nodes = nodes, flows = flows, debug = FALSE, edge_n = 32,
edge_f = 0.4, node_s = "prop")
# Another example
r <- new_networkModel() %>%
set_topo("infusion -> plasma -> body -> plasma") %>%
set_steady(c("infusion", "body"))
r <- topo(r)
ggtopo(r, edge = "fan")
sankey(r, debug = TRUE, edge_f = 0.2)
Run the code above in your browser using DataLab