Skip to content

Commit

Permalink
Fix 226 Add cactustree layout
Browse files Browse the repository at this point in the history
  • Loading branch information
thomasp85 committed Jan 14, 2024
1 parent abb0a31 commit 3b18c35
Show file tree
Hide file tree
Showing 27 changed files with 289 additions and 1 deletion.
3 changes: 3 additions & 0 deletions NEWS.md
Original file line number Diff line number Diff line change
Expand Up @@ -21,6 +21,9 @@
* The linear layout has gained a `weight` argument allowing you to set how much
space each node occupy. Further, the layout now calculates statistics so that
it can be used in conjunction with `geom_node_tile()` and `geom_node_arc_bar()`
* New layout added. Cactustree is a hierarchical layout optimised for
hierarchical edge bundling by placing nodes as budding circles on the
periphery of their parent (#226)

# ggraph 2.1.0

Expand Down
4 changes: 4 additions & 0 deletions R/RcppExports.R
Original file line number Diff line number Diff line change
@@ -1,6 +1,10 @@
# Generated by using Rcpp::compileAttributes() -> do not edit by hand
# Generator token: 10BE3573-1514-4C36-9D1C-5A225CD40393

cactusTree <- function(parent, order, weight, scale, overlap, upright) {
.Call('_ggraph_cactusTree', PACKAGE = 'ggraph', parent, order, weight, scale, overlap, upright)
}

#' Pack circles together
#'
#' This function is a direct interface to the circle packing algorithm used by
Expand Down
65 changes: 65 additions & 0 deletions R/layout_cactustree.R
Original file line number Diff line number Diff line change
@@ -0,0 +1,65 @@
#' Calculate nodes as fractal circle buds
#'
#' The cactustree layout is a hierarchical layout optimised for use with
#' hierarchical edge bundling ([geom_conn_bundle()]). It is a fractal layout
#' that places node children as circles on the periphery of their parent circle,
#' each circle scaled by the total weight of their children.
#'
#' @note
#' cactustree is a layout intended for trees, that is, graphs where nodes
#' only have one parent and zero or more children. If the provided graph does
#' not fit this format an attempt to convert it to such a format will be made.
#'
#' @param graph An `tbl_graph` object
#'
#' @param direction The direction of the tree in the graph. `'out'` (default)
#' means that parents point towards their children, while `'in'` means that
#' children point towards their parent.
#'
#' @param weight An optional node variable to use as weight. If `NULL` all leaf
#' nodes will be assigned a weight of `1`.
#'
#' @param scale_factor A scaling factor for the circles in the layout. The
#' radius will be calculated as `total_weight ^ scale_factor` with `total_weight`
#' being the weight of the node and all it's children.
#'
#' @param overlap How much is the center of child nodes offset from the
#' periphery of their parent as a proportion of their own radius.
#'
#' @param upright Logical. Should the children of the root only be distributed
#' around the top half of the root or all the way around.
#'
#' @param circular Logical. Should the layout be transformed to a circular
#' representation. Ignored.
#'
#' @return A data.frame with the columns `x`, `y`, `r`, `leaf`, `depth`,
#' `circular` as well as any information stored as node variables in the
#' tbl_graph object.
#'
#' @references
#' Dang, T., Forbes, A. (2017). *CactusTree: A Tree Drawing Approach
#' for Hierarchical Edge Bundling*. 2017 IEEE Pacific Visualization Symposium,
#' 210-214. https://doi.org/10.1109/PACIFICVIS.2017.8031596
#'
#' @family layout_tbl_graph_*
#'
layout_tbl_graph_cactustree <- function(graph, direction = "out", weight = NULL, scale_factor = 0.75, overlap = 0.5, upright = FALSE, circular = FALSE) {
weight <- enquo(weight)
weight <- eval_tidy(weight, .N())
if (is.null(weight)) {
weight <- as.numeric(degree(graph, mode = direction) == 0)
}
hierarchy <- tree_to_hierarchy(graph, direction, NULL, weight, NULL)
layout <- cactusTree(hierarchy$parent, hierarchy$order, hierarchy$weight, scale_factor, overlap, upright)[-1, ]
nodes <- data_frame0(
x = layout[, 1],
y = layout[, 2],
r = layout[, 3],
circular = FALSE,
leaf = degree(graph, mode = direction) == 0,
depth = node_depth(graph, mode = direction)
)
nodes <- combine_layout_nodes(nodes, as_tibble(graph, active = 'nodes'))
attr(nodes, 'graph') <- add_direction(graph, nodes)
nodes
}
3 changes: 2 additions & 1 deletion R/tbl_graph.R
Original file line number Diff line number Diff line change
Expand Up @@ -84,7 +84,8 @@ prepare_graph <- function(graph, layout, direction = 'out', ...) {
'dendrogram',
'treemap',
'circlepack',
'partition'
'partition',
'cactustree'
)
graph_is_treeish <- with_graph(graph, graph_is_tree() || graph_is_forest())
if (is_hierarchy || (layout == 'auto' && graph_is_treeish)) {
Expand Down
1 change: 1 addition & 0 deletions man/layout_tbl_graph_auto.Rd

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

1 change: 1 addition & 0 deletions man/layout_tbl_graph_backbone.Rd

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

82 changes: 82 additions & 0 deletions man/layout_tbl_graph_cactustree.Rd

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

1 change: 1 addition & 0 deletions man/layout_tbl_graph_centrality.Rd

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

1 change: 1 addition & 0 deletions man/layout_tbl_graph_circlepack.Rd

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

1 change: 1 addition & 0 deletions man/layout_tbl_graph_dendrogram.Rd

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

1 change: 1 addition & 0 deletions man/layout_tbl_graph_eigen.Rd

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

1 change: 1 addition & 0 deletions man/layout_tbl_graph_fabric.Rd

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

1 change: 1 addition & 0 deletions man/layout_tbl_graph_focus.Rd

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

1 change: 1 addition & 0 deletions man/layout_tbl_graph_hive.Rd

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

1 change: 1 addition & 0 deletions man/layout_tbl_graph_igraph.Rd

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

1 change: 1 addition & 0 deletions man/layout_tbl_graph_linear.Rd

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

1 change: 1 addition & 0 deletions man/layout_tbl_graph_manual.Rd

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

1 change: 1 addition & 0 deletions man/layout_tbl_graph_matrix.Rd

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

1 change: 1 addition & 0 deletions man/layout_tbl_graph_partition.Rd

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

1 change: 1 addition & 0 deletions man/layout_tbl_graph_pmds.Rd

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

1 change: 1 addition & 0 deletions man/layout_tbl_graph_stress.Rd

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

1 change: 1 addition & 0 deletions man/layout_tbl_graph_treemap.Rd

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

1 change: 1 addition & 0 deletions man/layout_tbl_graph_unrooted.Rd

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

17 changes: 17 additions & 0 deletions src/RcppExports.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,22 @@ Rcpp::Rostream<true>& Rcpp::Rcout = Rcpp::Rcpp_cout_get();
Rcpp::Rostream<false>& Rcpp::Rcerr = Rcpp::Rcpp_cerr_get();
#endif

// cactusTree
NumericMatrix cactusTree(IntegerVector parent, IntegerVector order, NumericVector weight, double scale, double overlap, bool upright);
RcppExport SEXP _ggraph_cactusTree(SEXP parentSEXP, SEXP orderSEXP, SEXP weightSEXP, SEXP scaleSEXP, SEXP overlapSEXP, SEXP uprightSEXP) {
BEGIN_RCPP
Rcpp::RObject rcpp_result_gen;
Rcpp::RNGScope rcpp_rngScope_gen;
Rcpp::traits::input_parameter< IntegerVector >::type parent(parentSEXP);
Rcpp::traits::input_parameter< IntegerVector >::type order(orderSEXP);
Rcpp::traits::input_parameter< NumericVector >::type weight(weightSEXP);
Rcpp::traits::input_parameter< double >::type scale(scaleSEXP);
Rcpp::traits::input_parameter< double >::type overlap(overlapSEXP);
Rcpp::traits::input_parameter< bool >::type upright(uprightSEXP);
rcpp_result_gen = Rcpp::wrap(cactusTree(parent, order, weight, scale, overlap, upright));
return rcpp_result_gen;
END_RCPP
}
// pack
NumericMatrix pack(NumericVector areas);
RcppExport SEXP _ggraph_pack(SEXP areasSEXP) {
Expand Down Expand Up @@ -129,6 +145,7 @@ END_RCPP
}

static const R_CallMethodDef CallEntries[] = {
{"_ggraph_cactusTree", (DL_FUNC) &_ggraph_cactusTree, 6},
{"_ggraph_pack", (DL_FUNC) &_ggraph_pack, 1},
{"_ggraph_circlePackLayout", (DL_FUNC) &_ggraph_circlePackLayout, 2},
{"_ggraph_dendrogram_spread", (DL_FUNC) &_ggraph_dendrogram_spread, 7},
Expand Down
Loading

0 comments on commit 3b18c35

Please sign in to comment.