diff --git a/NAMESPACE b/NAMESPACE index 0a5d6b24c38..3ad0778d3fe 100644 --- a/NAMESPACE +++ b/NAMESPACE @@ -140,6 +140,7 @@ export(as_adj) export(as_adj_edge_list) export(as_adj_list) export(as_adjacency_matrix) +export(as_biadjacency_matrix) export(as_bipartite) export(as_data_frame) export(as_edgelist) @@ -442,6 +443,7 @@ export(graph_attr_names) export(graph_from_adj_list) export(graph_from_adjacency_matrix) export(graph_from_atlas) +export(graph_from_biadjacency_matrix) export(graph_from_data_frame) export(graph_from_edgelist) export(graph_from_graphdb) diff --git a/R/adjacency.R b/R/adjacency.R index c1d648a65c2..13053bb9a81 100644 --- a/R/adjacency.R +++ b/R/adjacency.R @@ -374,7 +374,7 @@ graph_from_adjacency_matrix <- function(adjmatrix, if (!is.matrix(adjmatrix) && !inherits(adjmatrix, "Matrix")) { lifecycle::deprecate_soft( - "1.5.2", + "1.6.0", "graph_from_adjacency_matrix(adjmatrix = 'must be a matrix')" ) adjmatrix <- as.matrix(1) @@ -383,7 +383,7 @@ graph_from_adjacency_matrix <- function(adjmatrix, if (mode == "undirected") { if (!is_symmetric(adjmatrix)) { lifecycle::deprecate_soft( - "1.5.2", + "1.6.0", "graph_from_adjacency_matrix(adjmatrix = 'must be symmetric with mode = \"undirected\"')", details = 'Use mode = "max" to achieve the original behavior.' ) diff --git a/R/bipartite.R b/R/bipartite.R index 534cfc72478..46250da34c4 100644 --- a/R/bipartite.R +++ b/R/bipartite.R @@ -83,7 +83,7 @@ #' colnames(M) <- c("Party", "Skiing", "Badminton") #' M[] <- sample(0:1, length(M), replace = TRUE) #' M -#' g2 <- graph_from_incidence_matrix(M) +#' g2 <- graph_from_biadjacency_matrix(M) #' g2$name <- "Event network" #' proj2 <- bipartite_projection(g2) #' print(proj2[[1]], g = TRUE, e = TRUE) diff --git a/R/conversion.R b/R/conversion.R index cbe7ce9b1af..ca0b1c3bc94 100644 --- a/R/conversion.R +++ b/R/conversion.R @@ -801,10 +801,10 @@ get.incidence.sparse <- function(graph, types, names, attr) { -#' Incidence matrix of a bipartite graph +#' Bipartite adjacency matrix of a bipartite graph #' -#' This function can return a sparse or dense incidence matrix of a bipartite -#' network. The incidence matrix is an \eqn{n} times \eqn{m} matrix, \eqn{n} +#' This function can return a sparse or dense bipartite adjacency matrix of a bipartite +#' network. The bipartite adjacency matrix is an \eqn{n} times \eqn{m} matrix, \eqn{n} #' and \eqn{m} are the number of vertices of the two kinds. #' #' Bipartite graphs have a `type` vertex attribute in igraph, this is @@ -818,9 +818,9 @@ get.incidence.sparse <- function(graph, types, names, attr) { #' `type` vertex attribute. You must supply this argument if the graph has #' no `type` vertex attribute. #' @param attr Either `NULL` or a character string giving an edge -#' attribute name. If `NULL`, then a traditional incidence matrix is +#' attribute name. If `NULL`, then a traditional bipartite adjacency matrix is #' returned. If not `NULL` then the values of the given edge attribute are -#' included in the incidence matrix. If the graph has multiple edges, the edge +#' included in the bipartite adjacency matrix. If the graph has multiple edges, the edge #' attribute of an arbitrarily chosen edge (for the multiple edges) is #' included. #' @param names Logical scalar, if `TRUE` and the vertices in the graph @@ -831,16 +831,20 @@ get.incidence.sparse <- function(graph, types, names, attr) { #' created, you will need the `Matrix` package for this. #' @return A sparse or dense matrix. #' @author Gabor Csardi \email{csardi.gabor@@gmail.com} -#' @seealso [graph_from_incidence_matrix()] for the opposite operation. +#' @seealso [graph_from_biadjacency_matrix()] for the opposite operation. #' @family conversion #' @export #' @keywords graphs +#' @details +#' Some authors refer to the bipartite adjacency matrix as the +#' "bipartite incidence matrix". igraph 1.6.0 and later does not use +#' this naming to avoid confusion with the edge-vertex incidence matrix. #' @examples #' #' g <- make_bipartite_graph(c(0, 1, 0, 1, 0, 0), c(1, 2, 2, 3, 3, 4)) -#' as_incidence_matrix(g) +#' as_biadjacency_matrix(g) #' -as_incidence_matrix <- function(graph, types = NULL, attr = NULL, +as_biadjacency_matrix <- function(graph, types = NULL, attr = NULL, names = TRUE, sparse = FALSE) { # Argument checks ensure_igraph(graph) @@ -855,11 +859,30 @@ as_incidence_matrix <- function(graph, types = NULL, attr = NULL, get.incidence.dense(graph, types = types, names = names, attr = attr) } } - +#' As incidence matrix +#' +#' @description +#' `r lifecycle::badge("deprecated")` +#' +#' `as_incidence_matrix()` was renamed to `as_biadjacency_matrix()` to create a more +#' consistent API. +#' @inheritParams as_biadjacency_matrix +#' @keywords internal +#' @details +#' Some authors refer to the bipartite adjacency matrix as the +#' "bipartite incidence matrix". igraph 1.6.0 and later does not use +#' this naming to avoid confusion with the edge-vertex incidence matrix. +#' @export +as_incidence_matrix <- function(...) { # nocov start + lifecycle::deprecate_soft("1.6.0", "as_incidence_matrix()", "as_biadjacency_matrix()") + as_biadjacency_matrix(...) +} # nocov end #' @rdname graph_from_data_frame #' @param x An igraph object. #' @param what Character constant, whether to return info about vertices, #' edges, or both. The default is \sQuote{edges}. +#' @family conversion +#' @family biadjacency #' @export as_data_frame <- function(x, what = c("edges", "vertices", "both")) { ensure_igraph(x) diff --git a/R/incidence.R b/R/incidence.R index 639c0608f3e..fcffb34e49c 100644 --- a/R/incidence.R +++ b/R/incidence.R @@ -137,36 +137,36 @@ graph.incidence.dense <- function(incidence, directed, mode, multiple, res } -#' Create graphs from an incidence matrix +#' Create graphs from a bipartite adjacency matrix #' -#' `graph_from_incidence_matrix()` creates a bipartite igraph graph from an incidence +#' `graph_from_biadjacency_matrix()` creates a bipartite igraph graph from an incidence #' matrix. #' #' Bipartite graphs have a \sQuote{`type`} vertex attribute in igraph, #' this is boolean and `FALSE` for the vertices of the first kind and #' `TRUE` for vertices of the second kind. #' -#' `graph_from_incidence_matrix()` can operate in two modes, depending on the +#' `graph_from_biadjacency_matrix()` can operate in two modes, depending on the #' `multiple` argument. If it is `FALSE` then a single edge is -#' created for every non-zero element in the incidence matrix. If +#' created for every non-zero element in the bipartite adjacency matrix. If #' `multiple` is `TRUE`, then the matrix elements are rounded up to #' the closest non-negative integer to get the number of edges to create #' between a pair of vertices. #' #' @aliases graph.incidence -#' @param incidence The input incidence matrix. It can also be a sparse matrix +#' @param incidence The input bipartite adjacency matrix. It can also be a sparse matrix #' from the `Matrix` package. #' @param directed Logical scalar, whether to create a directed graph. #' @param mode A character constant, defines the direction of the edges in #' directed graphs, ignored for undirected graphs. If \sQuote{`out`}, then #' edges go from vertices of the first kind (corresponding to rows in the -#' incidence matrix) to vertices of the second kind (columns in the incidence +#' bipartite adjacency matrix) to vertices of the second kind (columns in the incidence #' matrix). If \sQuote{`in`}, then the opposite direction is used. If #' \sQuote{`all`} or \sQuote{`total`}, then mutual edges are created. #' @param multiple Logical scalar, specifies how to interpret the matrix #' elements. See details below. #' @param weighted This argument specifies whether to create a weighted graph -#' from the incidence matrix. If it is `NULL` then an unweighted graph is +#' from the bipartite adjacency matrix. If it is `NULL` then an unweighted graph is #' created and the `multiple` argument is used to determine the edges of #' the graph. If it is a character constant then for every non-zero matrix #' entry an edge is created and the value of the entry is added as an edge @@ -174,9 +174,9 @@ graph.incidence.dense <- function(incidence, directed, mode, multiple, #' weighted graph is created and the name of the edge attribute will be #' \sQuote{`weight`}. #' @param add.names A character constant, `NA` or `NULL`. -#' `graph_from_incidence_matrix()` can add the row and column names of the incidence +#' `graph_from_biadjacency_matrix()` can add the row and column names of the incidence #' matrix as vertex attributes. If this argument is `NULL` (the default) -#' and the incidence matrix has both row and column names, then these are added +#' and the bipartite adjacency matrix has both row and column names, then these are added #' as the \sQuote{`name`} vertex attribute. If you want a different vertex #' attribute for this, then give the name of the attributes as a character #' string. If this argument is `NA`, then no vertex attributes (other than @@ -192,10 +192,15 @@ graph.incidence.dense <- function(incidence, directed, mode, multiple, #' inc <- matrix(sample(0:1, 15, repl = TRUE), 3, 5) #' colnames(inc) <- letters[1:5] #' rownames(inc) <- LETTERS[1:3] -#' graph_from_incidence_matrix(inc) +#' graph_from_biadjacency_matrix(inc) #' +#' @details +#' Some authors refer to the bipartite adjacency matrix as the +#' "bipartite incidence matrix". igraph 1.6.0 and later does not use +#' this naming to avoid confusion with the edge-vertex incidence matrix. +#' @family biadjacency #' @export -graph_from_incidence_matrix <- function(incidence, directed = FALSE, +graph_from_biadjacency_matrix <- function(incidence, directed = FALSE, mode = c("all", "out", "in", "total"), multiple = FALSE, weighted = NULL, add.names = NULL) { @@ -243,9 +248,39 @@ graph_from_incidence_matrix <- function(incidence, directed = FALSE, } res } - -#' @rdname graph_from_incidence_matrix -#' @param ... Passed to `graph_from_incidence_matrix()`. -#' @family incidence +#' Graph from incidence matrix +#' +#' @description +#' `r lifecycle::badge("deprecated")` +#' +#' `graph_from_incidence_matrix()` was renamed to `graph_from_biadjacency_matrix()` to create a more +#' consistent API. +#' @inheritParams graph_from_biadjacency_matrix +#' @keywords internal +#' @details +#' Some authors refer to the bipartite adjacency matrix as the +#' "bipartite incidence matrix". igraph 1.6.0 and later does not use +#' this naming to avoid confusion with the edge-vertex incidence matrix. +#' @export +from_incidence_matrix <- function(...) { # nocov start + lifecycle::deprecate_soft("1.6.0", "graph_from_incidence_matrix()", "graph_from_biadjacency_matrix()") + graph_from_biadjacency_matrix(...) +} # nocov end +#' From incidence matrix +#' +#' @description +#' `r lifecycle::badge("deprecated")` +#' +#' `graph_from_incidence_matrix()` was renamed to `graph_from_biadjacency_matrix()` to create a more +#' consistent API. +#' @inheritParams graph_from_biadjacency_matrix +#' @keywords internal +#' @details +#' Some authors refer to the bipartite adjacency matrix as the +#' "bipartite incidence matrix". igraph 1.6.0 and later does not use +#' this naming to avoid confusion with the edge-vertex incidence matrix. #' @export -from_incidence_matrix <- function(...) constructor_spec(graph_from_incidence_matrix, ...) +graph_from_incidence_matrix <- function(...) { # nocov start + lifecycle::deprecate_soft("1.6.0", "graph_from_incidence_matrix()", "graph_from_biadjacency_matrix()") + graph_from_biadjacency_matrix(...) +} # nocov end diff --git a/R/layout.R b/R/layout.R index f20292a7fb5..15b9973ccca 100644 --- a/R/layout.R +++ b/R/layout.R @@ -270,7 +270,7 @@ normalize <- function(xmin = -1, xmax = 1, ymin = xmin, ymax = xmax, #' @examples #' # Random bipartite graph #' inc <- matrix(sample(0:1, 50, replace = TRUE, prob = c(2, 1)), 10, 5) -#' g <- graph_from_incidence_matrix(inc) +#' g <- graph_from_biadjacency_matrix(inc) #' plot(g, #' layout = layout_as_bipartite, #' vertex.color = c("green", "cyan")[V(g)$type + 1] diff --git a/R/zzz-deprecate.R b/R/zzz-deprecate.R index 54785c00651..c950fc3300c 100644 --- a/R/zzz-deprecate.R +++ b/R/zzz-deprecate.R @@ -184,7 +184,7 @@ deprecated("get.edgelist", as_edgelist) #' @export get.graph.attribute deprecated("get.graph.attribute", graph_attr) #' @export get.incidence -deprecated("get.incidence", as_incidence_matrix) +deprecated("get.incidence", as_biadjacency_matrix) #' @export get.stochastic deprecated("get.stochastic", stochastic_matrix) #' @export get.vertex.attribute @@ -244,7 +244,7 @@ deprecated("graph.full.citation", make_full_citation_graph) #' @export graph.graphdb deprecated("graph.graphdb", graph_from_graphdb) #' @export graph.incidence -deprecated("graph.incidence", graph_from_incidence_matrix) +deprecated("graph.incidence", graph_from_biadjacency_matrix) #' @export graph.isocreate deprecated("graph.isocreate", graph_from_isomorphism_class) #' @export graph.kautz diff --git a/_pkgdown.yml b/_pkgdown.yml index d80863f9f4d..8e0cd4fb4a2 100644 --- a/_pkgdown.yml +++ b/_pkgdown.yml @@ -138,7 +138,7 @@ reference: - subtitle: Bipartite graphs - contents: - has_concept("bipartite") - - has_concept("incidence") + - has_concept("biadjacency") - subtitle: Efficiency - contents: - has_concept("efficiency") diff --git a/man/as.directed.Rd b/man/as.directed.Rd index f98074fa198..6da3a446a71 100644 --- a/man/as.directed.Rd +++ b/man/as.directed.Rd @@ -105,9 +105,10 @@ Other conversion: \code{\link{as.matrix.igraph}()}, \code{\link{as_adj_list}()}, \code{\link{as_adjacency_matrix}()}, +\code{\link{as_biadjacency_matrix}()}, +\code{\link{as_data_frame}()}, \code{\link{as_edgelist}()}, \code{\link{as_graphnel}()}, -\code{\link{as_incidence_matrix}()}, \code{\link{as_long_data_frame}()}, \code{\link{graph_from_adj_list}()}, \code{\link{graph_from_graphnel}()} diff --git a/man/as.matrix.igraph.Rd b/man/as.matrix.igraph.Rd index f29e9b74bf7..7c55a76cc05 100644 --- a/man/as.matrix.igraph.Rd +++ b/man/as.matrix.igraph.Rd @@ -50,9 +50,10 @@ Other conversion: \code{\link{as.directed}()}, \code{\link{as_adj_list}()}, \code{\link{as_adjacency_matrix}()}, +\code{\link{as_biadjacency_matrix}()}, +\code{\link{as_data_frame}()}, \code{\link{as_edgelist}()}, \code{\link{as_graphnel}()}, -\code{\link{as_incidence_matrix}()}, \code{\link{as_long_data_frame}()}, \code{\link{graph_from_adj_list}()}, \code{\link{graph_from_graphnel}()} diff --git a/man/as_adj_list.Rd b/man/as_adj_list.Rd index 0cbf6432654..c71f8989f61 100644 --- a/man/as_adj_list.Rd +++ b/man/as_adj_list.Rd @@ -71,9 +71,10 @@ Other conversion: \code{\link{as.directed}()}, \code{\link{as.matrix.igraph}()}, \code{\link{as_adjacency_matrix}()}, +\code{\link{as_biadjacency_matrix}()}, +\code{\link{as_data_frame}()}, \code{\link{as_edgelist}()}, \code{\link{as_graphnel}()}, -\code{\link{as_incidence_matrix}()}, \code{\link{as_long_data_frame}()}, \code{\link{graph_from_adj_list}()}, \code{\link{graph_from_graphnel}()} diff --git a/man/as_adjacency_matrix.Rd b/man/as_adjacency_matrix.Rd index 1fd13478551..46835075049 100644 --- a/man/as_adjacency_matrix.Rd +++ b/man/as_adjacency_matrix.Rd @@ -87,9 +87,10 @@ Other conversion: \code{\link{as.directed}()}, \code{\link{as.matrix.igraph}()}, \code{\link{as_adj_list}()}, +\code{\link{as_biadjacency_matrix}()}, +\code{\link{as_data_frame}()}, \code{\link{as_edgelist}()}, \code{\link{as_graphnel}()}, -\code{\link{as_incidence_matrix}()}, \code{\link{as_long_data_frame}()}, \code{\link{graph_from_adj_list}()}, \code{\link{graph_from_graphnel}()} diff --git a/man/as_biadjacency_matrix.Rd b/man/as_biadjacency_matrix.Rd new file mode 100644 index 00000000000..18e27092556 --- /dev/null +++ b/man/as_biadjacency_matrix.Rd @@ -0,0 +1,81 @@ +% Generated by roxygen2: do not edit by hand +% Please edit documentation in R/conversion.R +\name{as_biadjacency_matrix} +\alias{as_biadjacency_matrix} +\alias{get.incidence} +\title{Bipartite adjacency matrix of a bipartite graph} +\usage{ +as_biadjacency_matrix( + graph, + types = NULL, + attr = NULL, + names = TRUE, + sparse = FALSE +) +} +\arguments{ +\item{graph}{The input graph. The direction of the edges is ignored in +directed graphs.} + +\item{types}{An optional vertex type vector to use instead of the +\code{type} vertex attribute. You must supply this argument if the graph has +no \code{type} vertex attribute.} + +\item{attr}{Either \code{NULL} or a character string giving an edge +attribute name. If \code{NULL}, then a traditional bipartite adjacency matrix is +returned. If not \code{NULL} then the values of the given edge attribute are +included in the bipartite adjacency matrix. If the graph has multiple edges, the edge +attribute of an arbitrarily chosen edge (for the multiple edges) is +included.} + +\item{names}{Logical scalar, if \code{TRUE} and the vertices in the graph +are named (i.e. the graph has a vertex attribute called \code{name}), then +vertex names will be added to the result as row and column names. Otherwise +the ids of the vertices are used as row and column names.} + +\item{sparse}{Logical scalar, if it is \code{TRUE} then a sparse matrix is +created, you will need the \code{Matrix} package for this.} +} +\value{ +A sparse or dense matrix. +} +\description{ +This function can return a sparse or dense bipartite adjacency matrix of a bipartite +network. The bipartite adjacency matrix is an \eqn{n} times \eqn{m} matrix, \eqn{n} +and \eqn{m} are the number of vertices of the two kinds. +} +\details{ +Bipartite graphs have a \code{type} vertex attribute in igraph, this is +boolean and \code{FALSE} for the vertices of the first kind and \code{TRUE} +for vertices of the second kind. + +Some authors refer to the bipartite adjacency matrix as the +"bipartite incidence matrix". igraph 1.6.0 and later does not use +this naming to avoid confusion with the edge-vertex incidence matrix. +} +\examples{ + +g <- make_bipartite_graph(c(0, 1, 0, 1, 0, 0), c(1, 2, 2, 3, 3, 4)) +as_biadjacency_matrix(g) + +} +\seealso{ +\code{\link[=graph_from_biadjacency_matrix]{graph_from_biadjacency_matrix()}} for the opposite operation. + +Other conversion: +\code{\link{as.directed}()}, +\code{\link{as.matrix.igraph}()}, +\code{\link{as_adj_list}()}, +\code{\link{as_adjacency_matrix}()}, +\code{\link{as_data_frame}()}, +\code{\link{as_edgelist}()}, +\code{\link{as_graphnel}()}, +\code{\link{as_long_data_frame}()}, +\code{\link{graph_from_adj_list}()}, +\code{\link{graph_from_graphnel}()} +} +\author{ +Gabor Csardi \email{csardi.gabor@gmail.com} +} +\concept{conversion} +\keyword{graphs} diff --git a/man/as_edgelist.Rd b/man/as_edgelist.Rd index 221e3147808..2a62c68e448 100644 --- a/man/as_edgelist.Rd +++ b/man/as_edgelist.Rd @@ -41,8 +41,9 @@ Other conversion: \code{\link{as.matrix.igraph}()}, \code{\link{as_adj_list}()}, \code{\link{as_adjacency_matrix}()}, +\code{\link{as_biadjacency_matrix}()}, +\code{\link{as_data_frame}()}, \code{\link{as_graphnel}()}, -\code{\link{as_incidence_matrix}()}, \code{\link{as_long_data_frame}()}, \code{\link{graph_from_adj_list}()}, \code{\link{graph_from_graphnel}()} diff --git a/man/as_graphnel.Rd b/man/as_graphnel.Rd index 89aed16b28b..53d9a5f1b8c 100644 --- a/man/as_graphnel.Rd +++ b/man/as_graphnel.Rd @@ -53,8 +53,9 @@ Other conversion: \code{\link{as.matrix.igraph}()}, \code{\link{as_adj_list}()}, \code{\link{as_adjacency_matrix}()}, +\code{\link{as_biadjacency_matrix}()}, +\code{\link{as_data_frame}()}, \code{\link{as_edgelist}()}, -\code{\link{as_incidence_matrix}()}, \code{\link{as_long_data_frame}()}, \code{\link{graph_from_adj_list}()}, \code{\link{graph_from_graphnel}()} diff --git a/man/as_incidence_matrix.Rd b/man/as_incidence_matrix.Rd index 316927fbc14..50c68cb715c 100644 --- a/man/as_incidence_matrix.Rd +++ b/man/as_incidence_matrix.Rd @@ -2,75 +2,19 @@ % Please edit documentation in R/conversion.R \name{as_incidence_matrix} \alias{as_incidence_matrix} -\alias{get.incidence} -\title{Incidence matrix of a bipartite graph} +\title{As incidence matrix} \usage{ -as_incidence_matrix( - graph, - types = NULL, - attr = NULL, - names = TRUE, - sparse = FALSE -) -} -\arguments{ -\item{graph}{The input graph. The direction of the edges is ignored in -directed graphs.} - -\item{types}{An optional vertex type vector to use instead of the -\code{type} vertex attribute. You must supply this argument if the graph has -no \code{type} vertex attribute.} - -\item{attr}{Either \code{NULL} or a character string giving an edge -attribute name. If \code{NULL}, then a traditional incidence matrix is -returned. If not \code{NULL} then the values of the given edge attribute are -included in the incidence matrix. If the graph has multiple edges, the edge -attribute of an arbitrarily chosen edge (for the multiple edges) is -included.} - -\item{names}{Logical scalar, if \code{TRUE} and the vertices in the graph -are named (i.e. the graph has a vertex attribute called \code{name}), then -vertex names will be added to the result as row and column names. Otherwise -the ids of the vertices are used as row and column names.} - -\item{sparse}{Logical scalar, if it is \code{TRUE} then a sparse matrix is -created, you will need the \code{Matrix} package for this.} -} -\value{ -A sparse or dense matrix. +as_incidence_matrix(...) } \description{ -This function can return a sparse or dense incidence matrix of a bipartite -network. The incidence matrix is an \eqn{n} times \eqn{m} matrix, \eqn{n} -and \eqn{m} are the number of vertices of the two kinds. -} -\details{ -Bipartite graphs have a \code{type} vertex attribute in igraph, this is -boolean and \code{FALSE} for the vertices of the first kind and \code{TRUE} -for vertices of the second kind. -} -\examples{ - -g <- make_bipartite_graph(c(0, 1, 0, 1, 0, 0), c(1, 2, 2, 3, 3, 4)) -as_incidence_matrix(g) +\ifelse{html}{\href{https://lifecycle.r-lib.org/articles/stages.html#deprecated}{\figure{lifecycle-deprecated.svg}{options: alt='[Deprecated]'}}}{\strong{[Deprecated]}} +\code{as_incidence_matrix()} was renamed to \code{as_biadjacency_matrix()} to create a more +consistent API. } -\seealso{ -\code{\link[=graph_from_incidence_matrix]{graph_from_incidence_matrix()}} for the opposite operation. - -Other conversion: -\code{\link{as.directed}()}, -\code{\link{as.matrix.igraph}()}, -\code{\link{as_adj_list}()}, -\code{\link{as_adjacency_matrix}()}, -\code{\link{as_edgelist}()}, -\code{\link{as_graphnel}()}, -\code{\link{as_long_data_frame}()}, -\code{\link{graph_from_adj_list}()}, -\code{\link{graph_from_graphnel}()} -} -\author{ -Gabor Csardi \email{csardi.gabor@gmail.com} +\details{ +Some authors refer to the bipartite adjacency matrix as the +"bipartite incidence matrix". igraph 1.6.0 and later does not use +this naming to avoid confusion with the edge-vertex incidence matrix. } -\concept{conversion} -\keyword{graphs} +\keyword{internal} diff --git a/man/as_long_data_frame.Rd b/man/as_long_data_frame.Rd index 045afcd8617..053e8077b5c 100644 --- a/man/as_long_data_frame.Rd +++ b/man/as_long_data_frame.Rd @@ -36,9 +36,10 @@ Other conversion: \code{\link{as.matrix.igraph}()}, \code{\link{as_adj_list}()}, \code{\link{as_adjacency_matrix}()}, +\code{\link{as_biadjacency_matrix}()}, +\code{\link{as_data_frame}()}, \code{\link{as_edgelist}()}, \code{\link{as_graphnel}()}, -\code{\link{as_incidence_matrix}()}, \code{\link{graph_from_adj_list}()}, \code{\link{graph_from_graphnel}()} } diff --git a/man/bipartite_projection.Rd b/man/bipartite_projection.Rd index bf17251d8b8..4e90535cef1 100644 --- a/man/bipartite_projection.Rd +++ b/man/bipartite_projection.Rd @@ -84,7 +84,7 @@ rownames(M) <- c("Alice", "Bob", "Cecil", "Dan", "Ethel") colnames(M) <- c("Party", "Skiing", "Badminton") M[] <- sample(0:1, length(M), replace = TRUE) M -g2 <- graph_from_incidence_matrix(M) +g2 <- graph_from_biadjacency_matrix(M) g2$name <- "Event network" proj2 <- bipartite_projection(g2) print(proj2[[1]], g = TRUE, e = TRUE) diff --git a/man/from_incidence_matrix.Rd b/man/from_incidence_matrix.Rd new file mode 100644 index 00000000000..4b54c6d6160 --- /dev/null +++ b/man/from_incidence_matrix.Rd @@ -0,0 +1,20 @@ +% Generated by roxygen2: do not edit by hand +% Please edit documentation in R/incidence.R +\name{from_incidence_matrix} +\alias{from_incidence_matrix} +\title{Graph from incidence matrix} +\usage{ +from_incidence_matrix(...) +} +\description{ +\ifelse{html}{\href{https://lifecycle.r-lib.org/articles/stages.html#deprecated}{\figure{lifecycle-deprecated.svg}{options: alt='[Deprecated]'}}}{\strong{[Deprecated]}} + +\code{graph_from_incidence_matrix()} was renamed to \code{graph_from_biadjacency_matrix()} to create a more +consistent API. +} +\details{ +Some authors refer to the bipartite adjacency matrix as the +"bipartite incidence matrix". igraph 1.6.0 and later does not use +this naming to avoid confusion with the edge-vertex incidence matrix. +} +\keyword{internal} diff --git a/man/graph_from_adj_list.Rd b/man/graph_from_adj_list.Rd index 4e72b5fcb2e..21ecdb70ebc 100644 --- a/man/graph_from_adj_list.Rd +++ b/man/graph_from_adj_list.Rd @@ -71,9 +71,10 @@ Other conversion: \code{\link{as.matrix.igraph}()}, \code{\link{as_adj_list}()}, \code{\link{as_adjacency_matrix}()}, +\code{\link{as_biadjacency_matrix}()}, +\code{\link{as_data_frame}()}, \code{\link{as_edgelist}()}, \code{\link{as_graphnel}()}, -\code{\link{as_incidence_matrix}()}, \code{\link{as_long_data_frame}()}, \code{\link{graph_from_graphnel}()} } diff --git a/man/graph_from_biadjacency_matrix.Rd b/man/graph_from_biadjacency_matrix.Rd new file mode 100644 index 00000000000..3f4874ead70 --- /dev/null +++ b/man/graph_from_biadjacency_matrix.Rd @@ -0,0 +1,94 @@ +% Generated by roxygen2: do not edit by hand +% Please edit documentation in R/incidence.R +\name{graph_from_biadjacency_matrix} +\alias{graph_from_biadjacency_matrix} +\alias{graph.incidence} +\title{Create graphs from a bipartite adjacency matrix} +\usage{ +graph_from_biadjacency_matrix( + incidence, + directed = FALSE, + mode = c("all", "out", "in", "total"), + multiple = FALSE, + weighted = NULL, + add.names = NULL +) +} +\arguments{ +\item{incidence}{The input bipartite adjacency matrix. It can also be a sparse matrix +from the \code{Matrix} package.} + +\item{directed}{Logical scalar, whether to create a directed graph.} + +\item{mode}{A character constant, defines the direction of the edges in +directed graphs, ignored for undirected graphs. If \sQuote{\code{out}}, then +edges go from vertices of the first kind (corresponding to rows in the +bipartite adjacency matrix) to vertices of the second kind (columns in the incidence +matrix). If \sQuote{\verb{in}}, then the opposite direction is used. If +\sQuote{\code{all}} or \sQuote{\code{total}}, then mutual edges are created.} + +\item{multiple}{Logical scalar, specifies how to interpret the matrix +elements. See details below.} + +\item{weighted}{This argument specifies whether to create a weighted graph +from the bipartite adjacency matrix. If it is \code{NULL} then an unweighted graph is +created and the \code{multiple} argument is used to determine the edges of +the graph. If it is a character constant then for every non-zero matrix +entry an edge is created and the value of the entry is added as an edge +attribute named by the \code{weighted} argument. If it is \code{TRUE} then a +weighted graph is created and the name of the edge attribute will be +\sQuote{\code{weight}}.} + +\item{add.names}{A character constant, \code{NA} or \code{NULL}. +\code{graph_from_biadjacency_matrix()} can add the row and column names of the incidence +matrix as vertex attributes. If this argument is \code{NULL} (the default) +and the bipartite adjacency matrix has both row and column names, then these are added +as the \sQuote{\code{name}} vertex attribute. If you want a different vertex +attribute for this, then give the name of the attributes as a character +string. If this argument is \code{NA}, then no vertex attributes (other than +type) will be added.} +} +\value{ +A bipartite igraph graph. In other words, an igraph graph that has a +vertex attribute \code{type}. +} +\description{ +\code{graph_from_biadjacency_matrix()} creates a bipartite igraph graph from an incidence +matrix. +} +\details{ +Bipartite graphs have a \sQuote{\code{type}} vertex attribute in igraph, +this is boolean and \code{FALSE} for the vertices of the first kind and +\code{TRUE} for vertices of the second kind. + +\code{graph_from_biadjacency_matrix()} can operate in two modes, depending on the +\code{multiple} argument. If it is \code{FALSE} then a single edge is +created for every non-zero element in the bipartite adjacency matrix. If +\code{multiple} is \code{TRUE}, then the matrix elements are rounded up to +the closest non-negative integer to get the number of edges to create +between a pair of vertices. + +Some authors refer to the bipartite adjacency matrix as the +"bipartite incidence matrix". igraph 1.6.0 and later does not use +this naming to avoid confusion with the edge-vertex incidence matrix. +} +\examples{ + +inc <- matrix(sample(0:1, 15, repl = TRUE), 3, 5) +colnames(inc) <- letters[1:5] +rownames(inc) <- LETTERS[1:3] +graph_from_biadjacency_matrix(inc) + +} +\seealso{ +\code{\link[=make_bipartite_graph]{make_bipartite_graph()}} for another way to create bipartite +graphs + +Other biadjacency: +\code{\link{as_data_frame}()} +} +\author{ +Gabor Csardi \email{csardi.gabor@gmail.com} +} +\concept{biadjacency} +\keyword{graphs} diff --git a/man/graph_from_data_frame.Rd b/man/graph_from_data_frame.Rd index ea3c4706f6d..4f7c66b9fa4 100644 --- a/man/graph_from_data_frame.Rd +++ b/man/graph_from_data_frame.Rd @@ -129,8 +129,25 @@ as_data_frame(g, what = "edges") \code{\link[=graph_from_literal]{graph_from_literal()}} for another way to create graphs, \code{\link[=read.table]{read.table()}} to read in tables from files. + +Other conversion: +\code{\link{as.directed}()}, +\code{\link{as.matrix.igraph}()}, +\code{\link{as_adj_list}()}, +\code{\link{as_adjacency_matrix}()}, +\code{\link{as_biadjacency_matrix}()}, +\code{\link{as_edgelist}()}, +\code{\link{as_graphnel}()}, +\code{\link{as_long_data_frame}()}, +\code{\link{graph_from_adj_list}()}, +\code{\link{graph_from_graphnel}()} + +Other biadjacency: +\code{\link{graph_from_biadjacency_matrix}()} } \author{ Gabor Csardi \email{csardi.gabor@gmail.com} } +\concept{biadjacency} +\concept{conversion} \keyword{graphs} diff --git a/man/graph_from_graphnel.Rd b/man/graph_from_graphnel.Rd index 6b01472dda3..0dfc1dd48e9 100644 --- a/man/graph_from_graphnel.Rd +++ b/man/graph_from_graphnel.Rd @@ -67,9 +67,10 @@ Other conversion: \code{\link{as.matrix.igraph}()}, \code{\link{as_adj_list}()}, \code{\link{as_adjacency_matrix}()}, +\code{\link{as_biadjacency_matrix}()}, +\code{\link{as_data_frame}()}, \code{\link{as_edgelist}()}, \code{\link{as_graphnel}()}, -\code{\link{as_incidence_matrix}()}, \code{\link{as_long_data_frame}()}, \code{\link{graph_from_adj_list}()} } diff --git a/man/graph_from_incidence_matrix.Rd b/man/graph_from_incidence_matrix.Rd index 6efcf24146e..c7f6cf1df15 100644 --- a/man/graph_from_incidence_matrix.Rd +++ b/man/graph_from_incidence_matrix.Rd @@ -2,91 +2,19 @@ % Please edit documentation in R/incidence.R \name{graph_from_incidence_matrix} \alias{graph_from_incidence_matrix} -\alias{graph.incidence} -\alias{from_incidence_matrix} -\title{Create graphs from an incidence matrix} +\title{From incidence matrix} \usage{ -graph_from_incidence_matrix( - incidence, - directed = FALSE, - mode = c("all", "out", "in", "total"), - multiple = FALSE, - weighted = NULL, - add.names = NULL -) - -from_incidence_matrix(...) -} -\arguments{ -\item{incidence}{The input incidence matrix. It can also be a sparse matrix -from the \code{Matrix} package.} - -\item{directed}{Logical scalar, whether to create a directed graph.} - -\item{mode}{A character constant, defines the direction of the edges in -directed graphs, ignored for undirected graphs. If \sQuote{\code{out}}, then -edges go from vertices of the first kind (corresponding to rows in the -incidence matrix) to vertices of the second kind (columns in the incidence -matrix). If \sQuote{\verb{in}}, then the opposite direction is used. If -\sQuote{\code{all}} or \sQuote{\code{total}}, then mutual edges are created.} - -\item{multiple}{Logical scalar, specifies how to interpret the matrix -elements. See details below.} - -\item{weighted}{This argument specifies whether to create a weighted graph -from the incidence matrix. If it is \code{NULL} then an unweighted graph is -created and the \code{multiple} argument is used to determine the edges of -the graph. If it is a character constant then for every non-zero matrix -entry an edge is created and the value of the entry is added as an edge -attribute named by the \code{weighted} argument. If it is \code{TRUE} then a -weighted graph is created and the name of the edge attribute will be -\sQuote{\code{weight}}.} - -\item{add.names}{A character constant, \code{NA} or \code{NULL}. -\code{graph_from_incidence_matrix()} can add the row and column names of the incidence -matrix as vertex attributes. If this argument is \code{NULL} (the default) -and the incidence matrix has both row and column names, then these are added -as the \sQuote{\code{name}} vertex attribute. If you want a different vertex -attribute for this, then give the name of the attributes as a character -string. If this argument is \code{NA}, then no vertex attributes (other than -type) will be added.} - -\item{...}{Passed to \code{graph_from_incidence_matrix()}.} -} -\value{ -A bipartite igraph graph. In other words, an igraph graph that has a -vertex attribute \code{type}. +graph_from_incidence_matrix(...) } \description{ -\code{graph_from_incidence_matrix()} creates a bipartite igraph graph from an incidence -matrix. -} -\details{ -Bipartite graphs have a \sQuote{\code{type}} vertex attribute in igraph, -this is boolean and \code{FALSE} for the vertices of the first kind and -\code{TRUE} for vertices of the second kind. - -\code{graph_from_incidence_matrix()} can operate in two modes, depending on the -\code{multiple} argument. If it is \code{FALSE} then a single edge is -created for every non-zero element in the incidence matrix. If -\code{multiple} is \code{TRUE}, then the matrix elements are rounded up to -the closest non-negative integer to get the number of edges to create -between a pair of vertices. -} -\examples{ - -inc <- matrix(sample(0:1, 15, repl = TRUE), 3, 5) -colnames(inc) <- letters[1:5] -rownames(inc) <- LETTERS[1:3] -graph_from_incidence_matrix(inc) +\ifelse{html}{\href{https://lifecycle.r-lib.org/articles/stages.html#deprecated}{\figure{lifecycle-deprecated.svg}{options: alt='[Deprecated]'}}}{\strong{[Deprecated]}} +\code{graph_from_incidence_matrix()} was renamed to \code{graph_from_biadjacency_matrix()} to create a more +consistent API. } -\seealso{ -\code{\link[=make_bipartite_graph]{make_bipartite_graph()}} for another way to create bipartite -graphs -} -\author{ -Gabor Csardi \email{csardi.gabor@gmail.com} +\details{ +Some authors refer to the bipartite adjacency matrix as the +"bipartite incidence matrix". igraph 1.6.0 and later does not use +this naming to avoid confusion with the edge-vertex incidence matrix. } -\concept{incidence} -\keyword{graphs} +\keyword{internal} diff --git a/man/layout_as_bipartite.Rd b/man/layout_as_bipartite.Rd index 4d900726f77..b4853a2809d 100644 --- a/man/layout_as_bipartite.Rd +++ b/man/layout_as_bipartite.Rd @@ -47,7 +47,7 @@ edge crossings, using the Sugiyama algorithm (see \examples{ # Random bipartite graph inc <- matrix(sample(0:1, 50, replace = TRUE, prob = c(2, 1)), 10, 5) -g <- graph_from_incidence_matrix(inc) +g <- graph_from_biadjacency_matrix(inc) plot(g, layout = layout_as_bipartite, vertex.color = c("green", "cyan")[V(g)$type + 1] diff --git a/tests/testthat/_snaps/graph.adjacency.md b/tests/testthat/_snaps/graph.adjacency.md index 301f8d84952..c46c4146288 100644 --- a/tests/testthat/_snaps/graph.adjacency.md +++ b/tests/testthat/_snaps/graph.adjacency.md @@ -21,7 +21,7 @@ graph_from_adjacency_matrix(m, mode = "undirected") Condition Warning: - The `adjmatrix` argument of `graph_from_adjacency_matrix()` must be symmetric with mode = "undirected" as of igraph 1.5.2. + The `adjmatrix` argument of `graph_from_adjacency_matrix()` must be symmetric with mode = "undirected" as of igraph 1.6.0. i Use mode = "max" to achieve the original behavior. Output IGRAPH U--- 2 2 -- @@ -53,7 +53,7 @@ graph_from_adjacency_matrix(m2, mode = "undirected") Condition Warning: - The `adjmatrix` argument of `graph_from_adjacency_matrix()` must be symmetric with mode = "undirected" as of igraph 1.5.2. + The `adjmatrix` argument of `graph_from_adjacency_matrix()` must be symmetric with mode = "undirected" as of igraph 1.6.0. i Use mode = "max" to achieve the original behavior. Output IGRAPH U--- 2 0 -- @@ -62,7 +62,7 @@ graph_from_adjacency_matrix(1) Condition Warning: - The `adjmatrix` argument of `graph_from_adjacency_matrix()` must be a matrix as of igraph 1.5.2. + The `adjmatrix` argument of `graph_from_adjacency_matrix()` must be a matrix as of igraph 1.6.0. Output IGRAPH D--- 1 1 -- + edge: @@ -71,7 +71,7 @@ graph_from_adjacency_matrix(1, mode = "undirected") Condition Warning: - The `adjmatrix` argument of `graph_from_adjacency_matrix()` must be a matrix as of igraph 1.5.2. + The `adjmatrix` argument of `graph_from_adjacency_matrix()` must be a matrix as of igraph 1.6.0. Output IGRAPH U--- 1 1 -- + edge: @@ -101,7 +101,7 @@ graph_from_adjacency_matrix(m, mode = "undirected") Condition Warning: - The `adjmatrix` argument of `graph_from_adjacency_matrix()` must be symmetric with mode = "undirected" as of igraph 1.5.2. + The `adjmatrix` argument of `graph_from_adjacency_matrix()` must be symmetric with mode = "undirected" as of igraph 1.6.0. i Use mode = "max" to achieve the original behavior. Output IGRAPH U--- 2 2 -- @@ -133,7 +133,7 @@ graph_from_adjacency_matrix(m2, mode = "undirected") Condition Warning: - The `adjmatrix` argument of `graph_from_adjacency_matrix()` must be symmetric with mode = "undirected" as of igraph 1.5.2. + The `adjmatrix` argument of `graph_from_adjacency_matrix()` must be symmetric with mode = "undirected" as of igraph 1.6.0. i Use mode = "max" to achieve the original behavior. Output IGRAPH U--- 2 0 -- diff --git a/tests/testthat/test-bipartite.projection.R b/tests/testthat/test-bipartite.projection.R index 9ec2a8abcc7..27b039aac3c 100644 --- a/tests/testthat/test-bipartite.projection.R +++ b/tests/testthat/test-bipartite.projection.R @@ -12,7 +12,7 @@ test_that("bipartite_projection works", { colnames(M) <- c("Party", "Skiing", "Badminton") M[] <- sample(0:1, length(M), replace = TRUE) M - g2 <- graph_from_incidence_matrix(M) + g2 <- graph_from_biadjacency_matrix(M) expect_that(as.matrix(g2[1:5, 6:8]), equals(M)) expect_that(as.matrix(g2[1:5, 1:5]), is_equivalent_to(matrix(0, 5, 5))) expect_that(as.matrix(g2[6:8, 6:8]), is_equivalent_to(matrix(0, 3, 3))) diff --git a/tests/testthat/test-get.incidence.R b/tests/testthat/test-get.incidence.R index 36fb800950d..b4f820dfdee 100644 --- a/tests/testthat/test-get.incidence.R +++ b/tests/testthat/test-get.incidence.R @@ -1,15 +1,15 @@ -test_that("as_incidence_matrix works", { +test_that("as_biadjacency_matrix works", { ## Dense I <- matrix(sample(0:1, 35, replace = TRUE, prob = c(3, 1)), ncol = 5) - g <- graph_from_incidence_matrix(I) - I2 <- as_incidence_matrix(g) + g <- graph_from_biadjacency_matrix(I) + I2 <- as_biadjacency_matrix(g) expect_that(I, is_equivalent_to(I2)) expect_that(rownames(I2), equals(as.character(1:7))) expect_that(colnames(I2), equals(as.character(8:12))) ## Sparse - I3 <- as_incidence_matrix(g, sparse = TRUE) + I3 <- as_biadjacency_matrix(g, sparse = TRUE) expect_that(as.matrix(I3), is_equivalent_to(I)) expect_that(rownames(I3), equals(as.character(1:7))) expect_that(colnames(I3), equals(as.character(8:12))) diff --git a/tests/testthat/test-graph.bipartite.R b/tests/testthat/test-graph.bipartite.R index 19b60b76f19..8b2c09d9b54 100644 --- a/tests/testthat/test-graph.bipartite.R +++ b/tests/testthat/test-graph.bipartite.R @@ -1,6 +1,6 @@ test_that("make_bipartite_graph works", { I <- matrix(sample(0:1, 35, replace = TRUE, prob = c(3, 1)), ncol = 5) - g <- graph_from_incidence_matrix(I) + g <- graph_from_biadjacency_matrix(I) edges <- unlist(sapply(seq_len(nrow(I)), function(x) { w <- which(I[x, ] != 0) + nrow(I) @@ -11,7 +11,7 @@ test_that("make_bipartite_graph works", { } })) g2 <- make_bipartite_graph(seq_len(nrow(I) + ncol(I)) > nrow(I), edges) - I2 <- as_incidence_matrix(g2) + I2 <- as_biadjacency_matrix(g2) expect_that(I2, is_equivalent_to(I)) }) diff --git a/tests/testthat/test-is.bipartite.R b/tests/testthat/test-is.bipartite.R index 47679e884ed..0d0015a4fc3 100644 --- a/tests/testthat/test-is.bipartite.R +++ b/tests/testthat/test-is.bipartite.R @@ -1,11 +1,11 @@ test_that("is_bipartite works", { I <- matrix(sample(0:1, 35, replace = TRUE, prob = c(3, 1)), ncol = 5) - g <- graph_from_incidence_matrix(I) + g <- graph_from_biadjacency_matrix(I) expect_true(bipartite_mapping(g)$res) set.seed(42) I <- matrix(sample(0:1, 35, replace = TRUE, prob = c(3, 1)), ncol = 5) - g <- graph_from_incidence_matrix(I) + g <- graph_from_biadjacency_matrix(I) expect_that( bipartite_mapping(g), equals(list(res = TRUE, type = c(rep(FALSE, 7), rep(TRUE, 5))))