From 889d3001a173459067f3279f5031fcf4464f8960 Mon Sep 17 00:00:00 2001 From: noriakis <31095487+noriakis@users.noreply.github.com> Date: Tue, 26 Dec 2023 00:05:50 +0900 Subject: [PATCH 1/2] add parameters to edge_bundle_path --- R/bundle_path.R | 24 ++++++++++++++++++++++-- man/edge_bundle_path.Rd | 14 +++++++++++++- 2 files changed, 35 insertions(+), 3 deletions(-) diff --git a/R/bundle_path.R b/R/bundle_path.R index 16cfd84..99f76ce 100644 --- a/R/bundle_path.R +++ b/R/bundle_path.R @@ -6,6 +6,8 @@ #' @param max_distortion maximum distortion #' @param weight_fac edge weight factor #' @param segments number of subdivisions of edges +#' @param bundle_strength bundle strength factor +#' @param mode the parameter fo shortest_paths #' @return data.frame containing the bundled edges #' @author David Schoch #' @details see [online](https://github.com/schochastics/edgebundle) for plotting tips @@ -22,7 +24,8 @@ #' edge_bundle_path(g, xy) #' @export -edge_bundle_path <- function(g, xy, max_distortion = 2, weight_fac = 2, segments = 20) { +edge_bundle_path <- function(g, xy, max_distortion = 2, weight_fac = 2, segments = 20, + bundle_strength = 1, mode = "out") { # preprocess if (!igraph::is.igraph(g)) { stop("edge_bundle_path requires the input graph to be an ingraph object") @@ -50,7 +53,7 @@ edge_bundle_path <- function(g, xy, max_distortion = 2, weight_fac = 2, segments } skip[e] <- TRUE g1 <- igraph::delete.edges(g, which(skip)) - sp_verts <- suppressWarnings(igraph::shortest_paths(g1, s, t, weights = weights[!skip])$vpath[[1]]) + sp_verts <- suppressWarnings(igraph::shortest_paths(g1, s, t, weights = weights[!skip], mode=mode)$vpath[[1]]) if (length(sp_verts) < 2) { skip[e] <- FALSE next @@ -63,6 +66,7 @@ edge_bundle_path <- function(g, xy, max_distortion = 2, weight_fac = 2, segments lock[igraph::get.edge.ids(g, rep(as.integer(sp_verts), each = 2)[-c(1, 2 * length(sp_verts))])] <- TRUE cpoints[[e]] <- xy[sp_verts, ] } + cpoints <- lapply(cpoints, subdivide, bs = bundle_strength) cpoints_bezier <- lapply(cpoints, approximateBezier, n = segments) idx <- seq(0, 1, length.out = segments) @@ -85,6 +89,22 @@ path_length <- function(verts, xy) { plen } +subdivide <- function(points, bs) { + for (i in seq_len(bs-1)) { + pnrow <- nrow(points) + newCP <- points[1,] + for (j in 1:(pnrow-1)) { + p1 <- points[j,] + p2 <- points[j+1,] + p3 <- (p1+p2)/2 + newCP <- rbind(newCP, p3) + newCP <- rbind(newCP, p2) + } + points <- newCP + } + return(points) +} + approximateBezier <- function(points, n) { pnrow <- nrow(points) - 1 tseq <- seq(0, 1, length.out = n) diff --git a/man/edge_bundle_path.Rd b/man/edge_bundle_path.Rd index a9ebe73..fe67a44 100644 --- a/man/edge_bundle_path.Rd +++ b/man/edge_bundle_path.Rd @@ -4,7 +4,15 @@ \alias{edge_bundle_path} \title{Edge-Path Bundling} \usage{ -edge_bundle_path(g, xy, max_distortion = 2, weight_fac = 2, segments = 20) +edge_bundle_path( + g, + xy, + max_distortion = 2, + weight_fac = 2, + segments = 20, + bundle_strength = 1, + mode = "out" +) } \arguments{ \item{g}{an igraph object} @@ -16,6 +24,10 @@ edge_bundle_path(g, xy, max_distortion = 2, weight_fac = 2, segments = 20) \item{weight_fac}{edge weight factor} \item{segments}{number of subdivisions of edges} + +\item{bundle_strength}{bundle strength factor} + +\item{mode}{the parameter fo shortest_paths} } \value{ data.frame containing the bundled edges From c60bbf11f1455b7bd2e4176177a663969571fbb2 Mon Sep 17 00:00:00 2001 From: noriakis <31095487+noriakis@users.noreply.github.com> Date: Sat, 30 Dec 2023 17:59:39 +0900 Subject: [PATCH 2/2] spacing --- R/bundle_path.R | 12 ++++++------ 1 file changed, 6 insertions(+), 6 deletions(-) diff --git a/R/bundle_path.R b/R/bundle_path.R index 99f76ce..4671b3d 100644 --- a/R/bundle_path.R +++ b/R/bundle_path.R @@ -53,7 +53,7 @@ edge_bundle_path <- function(g, xy, max_distortion = 2, weight_fac = 2, segments } skip[e] <- TRUE g1 <- igraph::delete.edges(g, which(skip)) - sp_verts <- suppressWarnings(igraph::shortest_paths(g1, s, t, weights = weights[!skip], mode=mode)$vpath[[1]]) + sp_verts <- suppressWarnings(igraph::shortest_paths(g1, s, t, weights = weights[!skip], mode = mode)$vpath[[1]]) if (length(sp_verts) < 2) { skip[e] <- FALSE next @@ -90,13 +90,13 @@ path_length <- function(verts, xy) { } subdivide <- function(points, bs) { - for (i in seq_len(bs-1)) { + for (i in seq_len(bs - 1)) { pnrow <- nrow(points) - newCP <- points[1,] - for (j in 1:(pnrow-1)) { + newCP <- points[1, ] + for (j in 1:(pnrow - 1)) { p1 <- points[j,] - p2 <- points[j+1,] - p3 <- (p1+p2)/2 + p2 <- points[j + 1,] + p3 <- (p1 + p2) / 2 newCP <- rbind(newCP, p3) newCP <- rbind(newCP, p2) }