Skip to content

Commit

Permalink
Fix #58 add h tree
Browse files Browse the repository at this point in the history
  • Loading branch information
thomasp85 committed Jan 14, 2024
1 parent 7b57f64 commit e76f02c
Show file tree
Hide file tree
Showing 31 changed files with 234 additions and 1 deletion.
1 change: 1 addition & 0 deletions NAMESPACE
Original file line number Diff line number Diff line change
Expand Up @@ -324,6 +324,7 @@ importFrom(igraph,as.undirected)
importFrom(igraph,as_edgelist)
importFrom(igraph,bfs)
importFrom(igraph,components)
importFrom(igraph,count_components)
importFrom(igraph,degree)
importFrom(igraph,delete_edges)
importFrom(igraph,delete_vertex_attr)
Expand Down
1 change: 1 addition & 0 deletions NEWS.md
Original file line number Diff line number Diff line change
Expand Up @@ -26,6 +26,7 @@
periphery of their parent (#226)
* Updated layout functions from the graphlayouts package to support grouped and
constrained versions (centrality, focus, and stress layouts)
* Added H Tree layout for binary trees (#58)

# 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
Expand Up @@ -52,6 +52,10 @@ dendrogram_spread <- function(graph, starts, y, leaf, repel, pad, ratio) {
.Call('_ggraph_dendrogram_spread', PACKAGE = 'ggraph', graph, starts, y, leaf, repel, pad, ratio)
}

hTree <- function(parent, order) {
.Call('_ggraph_hTree', PACKAGE = 'ggraph', parent, order)
}

partitionTree <- function(parent, order, weight, height) {
.Call('_ggraph_partitionTree', PACKAGE = 'ggraph', parent, order, weight, height)
}
Expand Down
2 changes: 2 additions & 0 deletions R/ggraph.R
Original file line number Diff line number Diff line change
Expand Up @@ -83,6 +83,8 @@
#' \item{`unrooted`}{Draws unrooted trees based on equal angle with optional
#' equal daylight modification. See [layout_tbl_graph_unrooted()] for further
#' details}
#' \item{`htree`}{Draws binary trees as a space filling fractal. See
#' [layout_tbl_graph_htree()] for further details}
#' }
#'
#' Alternatively a matrix or a data.frame can be provided to the `layout`
Expand Down
52 changes: 52 additions & 0 deletions R/layout_htree.R
Original file line number Diff line number Diff line change
@@ -0,0 +1,52 @@
#' Layout binary trees in a fractal H formation
#'
#' This is a spac efficient layout only useful for binary trees. It is fractal
#' and works by offsetting child nodes from their parent either horizontally or
#' vertically depending on depth. The offset is decreased at each step by a
#' factor of the square root of 2.
#'
#' @note
#' H Tree 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 sort.by The name of a node variable to sort the nodes by.
#'
#' @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 circular Logical. Should the layout be transformed to a circular
#' representation. Ignored
#'
#' @return A data.frame with the columns `x`, `y`, `leaf`, `depth`, `circular`
#' as well as any information stored as node variables in the tbl_graph object.
#'
#' @family layout_tbl_graph_*
#'
#' @importFrom igraph degree count_components
#'
layout_tbl_graph_htree <- function(graph, sort.by = NULL, direction = 'out', circular = FALSE) {
if (any(degree(graph, mode = direction) > 2)) {
cli::cli_abort("H-Tree layouts can only be used with binary trees")
}
if (count_components(graph, "weak") != 1) {
cli::cli_abort("H-Tree layouts can only be used with fully connected graphs")
}
sort.by <- enquo(sort.by)
sort.by <- eval_tidy(sort.by, .N())
hierarchy <- tree_to_hierarchy(graph, direction, sort.by, NULL)
layout <- hTree(hierarchy$parent, hierarchy$order)[-1, ]
nodes <- data_frame0(
x = layout[, 1],
y = layout[, 2],
circular = FALSE
)
nodes$leaf <- degree(graph, mode = direction) == 0
nodes$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 @@ -85,7 +85,8 @@ prepare_graph <- function(graph, layout, direction = 'out', ...) {
'treemap',
'circlepack',
'partition',
'cactustree'
'cactustree',
'htree'
)
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 _pkgdown.yml
Original file line number Diff line number Diff line change
Expand Up @@ -75,6 +75,7 @@ reference:
- layout_tbl_graph_treemap
- layout_tbl_graph_partition
- layout_tbl_graph_cactustree
- layout_tbl_graph_htree
- layout_tbl_graph_matrix
- layout_tbl_graph_hive
- layout_tbl_graph_fabric
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.

1 change: 1 addition & 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.

87 changes: 87 additions & 0 deletions man/layout_tbl_graph_htree.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.

13 changes: 13 additions & 0 deletions src/RcppExports.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -66,6 +66,18 @@ BEGIN_RCPP
return rcpp_result_gen;
END_RCPP
}
// hTree
NumericMatrix hTree(IntegerVector parent, IntegerVector order);
RcppExport SEXP _ggraph_hTree(SEXP parentSEXP, SEXP orderSEXP) {
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_result_gen = Rcpp::wrap(hTree(parent, order));
return rcpp_result_gen;
END_RCPP
}
// partitionTree
NumericMatrix partitionTree(IntegerVector parent, IntegerVector order, NumericVector weight, NumericVector height);
RcppExport SEXP _ggraph_partitionTree(SEXP parentSEXP, SEXP orderSEXP, SEXP weightSEXP, SEXP heightSEXP) {
Expand Down Expand Up @@ -149,6 +161,7 @@ static const R_CallMethodDef CallEntries[] = {
{"_ggraph_pack", (DL_FUNC) &_ggraph_pack, 1},
{"_ggraph_circlePackLayout", (DL_FUNC) &_ggraph_circlePackLayout, 2},
{"_ggraph_dendrogram_spread", (DL_FUNC) &_ggraph_dendrogram_spread, 7},
{"_ggraph_hTree", (DL_FUNC) &_ggraph_hTree, 2},
{"_ggraph_partitionTree", (DL_FUNC) &_ggraph_partitionTree, 4},
{"_ggraph_cut_lines", (DL_FUNC) &_ggraph_cut_lines, 9},
{"_ggraph_pathAttr", (DL_FUNC) &_ggraph_pathAttr, 2},
Expand Down
Loading

0 comments on commit e76f02c

Please sign in to comment.