Skip to content

Commit

Permalink
Add metro layout
Browse files Browse the repository at this point in the history
  • Loading branch information
thomasp85 committed Jan 22, 2024
1 parent 76945f1 commit 534aefe
Show file tree
Hide file tree
Showing 24 changed files with 190 additions and 1 deletion.
2 changes: 2 additions & 0 deletions NAMESPACE
Original file line number Diff line number Diff line change
Expand Up @@ -299,10 +299,12 @@ importFrom(ggforce,radial_trans)
importFrom(ggrepel,GeomLabelRepel)
importFrom(ggrepel,GeomTextRepel)
importFrom(graphlayouts,layout_as_backbone)
importFrom(graphlayouts,layout_as_metromap)
importFrom(graphlayouts,layout_with_centrality)
importFrom(graphlayouts,layout_with_centrality_group)
importFrom(graphlayouts,layout_with_constrained_stress)
importFrom(graphlayouts,layout_with_eigen)
importFrom(graphlayouts,layout_with_fixed_coords)
importFrom(graphlayouts,layout_with_focus)
importFrom(graphlayouts,layout_with_focus_group)
importFrom(graphlayouts,layout_with_pmds)
Expand Down
1 change: 1 addition & 0 deletions NEWS.md
Original file line number Diff line number Diff line change
Expand Up @@ -30,6 +30,7 @@
* Added `geom_edge_bundle_force()`, `geom_edge_bundle_path()`, and
`geom_edge_bundle_minimal()` (+ variants) to provide support for edge bundling
(#267)
* Add "metro" layout from graphlayouts for metroline like layouts

# ggraph 2.1.0

Expand Down
66 changes: 66 additions & 0 deletions R/layout_metro.R
Original file line number Diff line number Diff line change
@@ -0,0 +1,66 @@
#' Place nodes according to the standard design of metro maps
#'
#' This layouttries to optimise the placement of nodes so that they adhere to
#' the classic look of metro maps. As such it optimizes on the distribution of
#' incident edge angles, conformity of edge length, and edge angles in multiples
#' of 45 degrees. As it works as a refinement of an already existing layout
#' (the physical location of metro stations) it requires an a priori node
#' location. Due to it's purpose it probably works best with planar graphs.
#'
#' @param graph A tbl_graph object
#' @param x,y The start location of the nodes
#' @param length Desired multiple of grid point spacing. (`length * grid_space`
#' determines desired edge length)
#' @param grid_space The distance between consecitive grid points
#' @param optimization_weights The relative weight to be placed on the 5
#' criteria during optimization as a numeric vector of length 4. The criteria
#' are:
#' * `Angular Resolution Criterion`: The angles of incident edges at each
#' station should be maximized, because if there is only a small angle between
#' any two adjacent edges, then it can become difficult to distinguish between
#' them.
#' * `Edge Length Criterion`: The edge lengths across the whole map should be
#' approximately equal to ensure regular spacing between stations. It is based
#' on the preferred multiple, l, of the grid spacing, g. The purpose of the
#' criterion is to penalize edges that are longer than or shorter than lg.
#' * `Balanced Edge Length Criterion`: The length of edges incident to a
#' particular station should be similar.
#' * `Line Straightness Criterion`: (not yet implemented) Edges that form part
#' of a line should, where possible, be co-linear either side of each station
#' that the line passes through.
#' * `Octiinearity Criterion`: Each edge should be drawn horizontally,
#' vertically, or diagonally at 45 degree, so we penalize edges that are not
#' at a desired angle.
#' If `NULL` all criteria are given equal weight.
#' @param max_movement Number of grid points a station can move away rom its
#' original position
#' @param circular ignored
#'
#' @return A data.frame with the columns `x`, `y`, `circular` as
#' well as any information stored as node variables in the tbl_graph object.
#'
#' @references
#' Stott, J., Rodgers, P., Martinez-Ovando, J. C., and Walker, S. G. (2011).
#' *Automatic metro map layout using multicriteria optimization* In IEEE Trans
#' Vis Comput Graph 17(1) pp. 101-114. https://doi.org/10.1109/tvcg.2010.24
#'
#' @family layout_tbl_graph_*
#'
#' @author The underlying algorithm is implemented in the graphlayouts package
#' by David Schoch
#'
#' @importFrom graphlayouts layout_as_metromap
#'
layout_tbl_graph_metro <- function(graph, x, y, length = 2, grid_space = 0.0025,
optimization_weights = NULL, max_movement = 5,
circular = FALSE) {
x <- eval_tidy(enquo(x), .N())
y <- eval_tidy(enquo(y), .N())
if (is.null(x) || is.null(y)) {
cli::cli_abort("The metro layout must have a prior location of nodes to start with")
}
if (is.null(optimization_weights)) optimization_weights <- rep(1, 5)
xy <- layout_as_metromap(graph, xy = cbind(x, y), l = length, gr = grid_space, w = optimization_weights, bsize = max_movement)
nodes <- data_frame0(x = xy[,1], y = xy[,2], circular = FALSE)
combine_layout_nodes(nodes, as_tibble(graph, active = 'nodes'))
}
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.

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

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

4 changes: 3 additions & 1 deletion 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.

0 comments on commit 534aefe

Please sign in to comment.