diff --git a/NAMESPACE b/NAMESPACE index 4bd16e4bea4..0642cdf4496 100644 --- a/NAMESPACE +++ b/NAMESPACE @@ -126,6 +126,7 @@ export(aging.ba.game) export(aging.barabasi.game) export(aging.prefatt.game) export(algorithm) +export(align_layout) export(all_shortest_paths) export(all_simple_paths) export(alpha.centrality) diff --git a/R/layout.R b/R/layout.R index 4915534df6a..d25fee763f5 100644 --- a/R/layout.R +++ b/R/layout.R @@ -877,12 +877,18 @@ layout.circle <- function(..., params = list()) { #' #' `layout_nicely()` tries to choose an appropriate layout function for the #' supplied graph, and uses that to generate the layout. The current -#' implementation works like this: \enumerate{ \item If the graph has a graph -#' attribute called \sQuote{layout}, then this is used. If this attribute is an -#' R function, then it is called, with the graph and any other extra arguments. +#' implementation works like this: +#' \enumerate{ +#' \item If the graph has a graph attribute called \sQuote{layout}, +#' then this is used. If this attribute is an R function, then it is called, with the graph and any other extra arguments. +#' #' \item Otherwise, if the graph has vertex attributes called \sQuote{x} and #' \sQuote{y}, then these are used as coordinates. If the graph has an -#' additional \sQuote{z} vertex attribute, that is also used. \item Otherwise, +#' additional \sQuote{z} vertex attribute, that is also used. +#' +#' \item Otherwise, if the graph is a forest and has less than 30 vertices, `layout_as_tree()` is used. +#' +#' \item Otherwise, #' if the graph is connected and has less than 1000 vertices, the #' Fruchterman-Reingold layout is used, by calling `layout_with_fr()`. #' \item Otherwise the DrL layout is used, `layout_with_drl()` is called. } @@ -916,6 +922,7 @@ layout_nicely <- function(graph, dim = 2, ...) { ## 1. If there is a 'layout' graph attribute, we just use that. ## 2. Otherwise, if there are vertex attributes called 'x' and 'y', ## we use those (and the 'z' vertex attribute as well, if present). + ## 3. If the graph is a forest and has less than 30 vertices, layout_as_tree is used ## 3. Otherwise, if the graph is small (<1000) we use ## the Fruchterman-Reingold layout. ## 4. Otherwise we use the DrL layout generator. @@ -954,10 +961,15 @@ layout_nicely <- function(graph, dim = 2, ...) { } args$graph <- graph + + if (is_forest(graph) && vcount(graph) <= 30) { + return(do.call(layout_as_tree, args)) + } + args$dim <- dim if (vcount(graph) < 1000) { - do.call(layout_with_fr, args) + align_layout(graph, do.call(layout_with_fr, args)) } else { do.call(layout_with_drl, args) } @@ -3008,3 +3020,19 @@ drl_defaults <- list( final = igraph.drl.final, refine = igraph.drl.refine ) + +#' Align a vertex layout +#' This function centers a vertex layout on the coordinate system origin and +#' rotates the layout to achieve a visually pleasing alignment with the coordinate +#' axes. Doing this is particularly useful with force-directed layouts such as [layout_with_fr()]. +#' @param graph The graph whose layout is to be aligned. +#' @param layout A matrix whose rows are the coordinates of vertices. +#' @return modified layout matrix +#' @export +#' @examples +#' g <- make_lattice(c(3, 3)) +#' l1 <- layout_with_fr(g) +#' l2 <- align_layout(g,l1) +#' plot(g, layout = l1) +#' plot(g, layout = l2) +align_layout <- layout_align_impl diff --git a/_pkgdown.yml b/_pkgdown.yml index 646b86e8fc8..e543b111bf1 100644 --- a/_pkgdown.yml +++ b/_pkgdown.yml @@ -65,6 +65,7 @@ reference: - has_concept("plot.common") - has_concept("plot.shapes") - vertex.shape.pie + - align_layout - subtitle: Graph coloring - contents: - has_concept("coloring") diff --git a/man/align_layout.Rd b/man/align_layout.Rd new file mode 100644 index 00000000000..5ecdbe4ac9d --- /dev/null +++ b/man/align_layout.Rd @@ -0,0 +1,32 @@ +% Generated by roxygen2: do not edit by hand +% Please edit documentation in R/layout.R +\name{align_layout} +\alias{align_layout} +\title{Align a vertex layout +This function centers a vertex layout on the coordinate system origin and +rotates the layout to achieve a visually pleasing alignment with the coordinate +axes. Doing this is particularly useful with force-directed layouts such as \code{\link[=layout_with_fr]{layout_with_fr()}}.} +\usage{ +align_layout(graph, layout) +} +\arguments{ +\item{graph}{The graph whose layout is to be aligned.} + +\item{layout}{A matrix whose rows are the coordinates of vertices.} +} +\value{ +modified layout matrix +} +\description{ +Align a vertex layout +This function centers a vertex layout on the coordinate system origin and +rotates the layout to achieve a visually pleasing alignment with the coordinate +axes. Doing this is particularly useful with force-directed layouts such as \code{\link[=layout_with_fr]{layout_with_fr()}}. +} +\examples{ +g <- make_lattice(c(3, 3)) +l1 <- layout_with_fr(g) +l2 <- align_layout(g,l1) +plot(g, layout = l1) +plot(g, layout = l2) +} diff --git a/man/layout_nicely.Rd b/man/layout_nicely.Rd index 6ac67a3e336..a8df0563316 100644 --- a/man/layout_nicely.Rd +++ b/man/layout_nicely.Rd @@ -28,12 +28,18 @@ graph, automatically, based on a simple algorithm. See details below. \details{ \code{layout_nicely()} tries to choose an appropriate layout function for the supplied graph, and uses that to generate the layout. The current -implementation works like this: \enumerate{ \item If the graph has a graph -attribute called \sQuote{layout}, then this is used. If this attribute is an -R function, then it is called, with the graph and any other extra arguments. +implementation works like this: +\enumerate{ +\item If the graph has a graph attribute called \sQuote{layout}, +then this is used. If this attribute is an R function, then it is called, with the graph and any other extra arguments. + \item Otherwise, if the graph has vertex attributes called \sQuote{x} and \sQuote{y}, then these are used as coordinates. If the graph has an -additional \sQuote{z} vertex attribute, that is also used. \item Otherwise, +additional \sQuote{z} vertex attribute, that is also used. + +\item Otherwise, if the graph is a forest and has less than 30 vertices, \code{layout_as_tree()} is used. + +\item Otherwise, if the graph is connected and has less than 1000 vertices, the Fruchterman-Reingold layout is used, by calling \code{layout_with_fr()}. \item Otherwise the DrL layout is used, \code{layout_with_drl()} is called. }