Skip to content

Commit da790ef

Browse files
Merge pull request #62 from stocnet/develop
v0.4.2
2 parents 8b1a435 + 278ea9b commit da790ef

18 files changed

+255
-253
lines changed

DESCRIPTION

+3-3
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
11
Package: manynet
22
Title: Many Ways to Make, Modify, Mark, and Map Myriad Networks
3-
Version: 0.4.1
4-
Date: 2024-01-24
3+
Version: 0.4.2
4+
Date: 2024-03-12
55
Description: A set of tools for making, modifying, marking, and mapping many different types of networks.
66
All functions operate with matrices, edge lists, and 'igraph', 'network', and 'tidygraph' objects,
77
and on one-mode, two-mode (bipartite), and sometimes three-mode networks.
@@ -15,7 +15,7 @@ License: MIT + file LICENSE
1515
Language: en-GB
1616
Encoding: UTF-8
1717
LazyData: true
18-
RoxygenNote: 7.3.0
18+
RoxygenNote: 7.3.1
1919
Imports:
2020
dplyr (>= 1.1.0),
2121
ggplot2,

NAMESPACE

+1
Original file line numberDiff line numberDiff line change
@@ -338,6 +338,7 @@ export(join_ties)
338338
export(labs)
339339
export(layout_tbl_graph_alluvial)
340340
export(layout_tbl_graph_concentric)
341+
export(layout_tbl_graph_configuration)
341342
export(layout_tbl_graph_hierarchy)
342343
export(layout_tbl_graph_ladder)
343344
export(layout_tbl_graph_lineage)

NEWS.md

+20
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,23 @@
1+
# manynet 0.4.2
2+
3+
2024-03-12
4+
5+
## Making
6+
7+
- Closed #57 by updating `play_diffusions()` to revert future plan on exit
8+
- Fixed bug with how `generate_random()` works for two-mode networks with specified number of ties
9+
10+
## Mapping
11+
12+
- Closed #6 by updating how "lineage" layout works and places nodes on Y axis
13+
- Closed #39 by making `autographr()` more flexible and efficient in setting variables to aesthetics
14+
- Updated themes to be compatible with newer and older versions of `{ggplot2}`
15+
- Added "configuration" layout for small triad/quad networks
16+
17+
## Modifying
18+
19+
- Updated `to_reciprocated.matrix()` to consistently work with matrices
20+
121
# manynet 0.4.1
222

323
2023-12-24

R/make_generate.R

+1-1
Original file line numberDiff line numberDiff line change
@@ -79,7 +79,7 @@ generate_random <- function(n, p = 0.5, directed = FALSE, with_attr = TRUE) {
7979
if(!as.integer(p)==p) stop("`p` must be an integer if above 1.")
8080
g <- igraph::sample_bipartite(n[1], n[2],
8181
m = p,
82-
type = "gmp",
82+
type = "gnm",
8383
directed = directed,
8484
mode = "out")
8585
} else {

R/make_play.R

+4-1
Original file line numberDiff line numberDiff line change
@@ -257,8 +257,11 @@ play_diffusions <- function(.data,
257257
verbose = FALSE) {
258258
thisRequires("future")
259259
thisRequires("furrr")
260+
oplan <- future::plan(strategy)
261+
on.exit(future::plan(oplan), add = TRUE)
262+
260263
if(missing(steps)) steps <- network_nodes(.data)
261-
future::plan(strategy)
264+
262265
out <- furrr::future_map_dfr(1:times, function(j){
263266
data.frame(sim = j,
264267
play_diffusion(.data,

R/manip_reformat.R

+1-1
Original file line numberDiff line numberDiff line change
@@ -212,7 +212,7 @@ to_reciprocated.tbl_graph <- function(.data) {
212212

213213
#' @export
214214
to_reciprocated.matrix <- function(.data) {
215-
as_matrix(to_reciprocated(as_igraph(.data)))
215+
.data + t(.data)
216216
}
217217

218218
#' @export

R/map_autographr.R

+98-120
Large diffs are not rendered by default.

R/map_layout_configurations.R

+17
Original file line numberDiff line numberDiff line change
@@ -1,10 +1,27 @@
11
#' Layout algorithms based on configurational positions
22
#'
3+
#' @description
4+
#' Configurational layouts locate nodes at symmetric coordinates
5+
#' to help illustrate the particular layouts.
6+
#' Currently "triad" and "quad" layouts are available.
7+
#' The "configuration" layout will choose the appropriate configurational
8+
#' layout automatically.
9+
#'
310
#' @name configuration_layouts
411
#' @family mapping
512
#' @inheritParams partition_layouts
613
NULL
714

15+
#' @rdname configuration_layouts
16+
#' @export
17+
layout_tbl_graph_configuration <- function(.data,
18+
circular = FALSE, times = 1000){
19+
if (network_nodes(.data) == 3) {
20+
layout_tbl_graph_triad(.data, circular = circular, times = times)
21+
} else if (network_nodes(.data) == 4) {
22+
layout_tbl_graph_quad(.data, circular = circular, times = times)
23+
}}
24+
825
#' @rdname configuration_layouts
926
#' @export
1027
layout_tbl_graph_triad <- function(.data,

R/map_layout_partition.R

+14-43
Original file line numberDiff line numberDiff line change
@@ -61,7 +61,7 @@ layout_tbl_graph_hierarchy <- function(.data, center = NULL,
6161
if (is.null(center)) {
6262
thisRequiresBio("Rgraphviz")
6363
prep <- as_matrix(.data, twomode = FALSE)
64-
if(anyDuplicated(rownames(prep))){
64+
if(anyDuplicated(rownames(prep))) {
6565
rownames(prep) <- seq_len(nrow(prep))
6666
colnames(prep) <- seq_len(ncol(prep))
6767
}
@@ -84,15 +84,13 @@ layout_tbl_graph_hierarchy <- function(.data, center = NULL,
8484
Evt2 <- cbind(rep(2, floor(ncol(net)/2)), nrm(rng(floor(mm/2))))
8585
crd <- rbind(Act, Evt1, Evt2)
8686
crd[which(is.nan(crd))] <- 0.5
87-
crd[, 2] <- crd[, 2] * cos(pi) - crd[, 1] * sin(pi)
8887
rownames(crd) <- c(dimnames(net)[[1]], dimnames(net)[[2]])
8988
} else if (center == "events") {
9089
Act1 <- cbind(rep(0, ceiling(nrow(net)/2)), nrm(rng(ceiling(nn/2))))
9190
Act2 <- cbind(rep(2, floor(nrow(net)/2)), nrm(rng(floor(nn/2))))
9291
Evt <- cbind(rep(1, ncol(net)), nrm(rng(mm)))
9392
crd <- rbind(Act1, Act2, Evt)
9493
crd[which(is.nan(crd))] <- 0.5
95-
crd[, 2] <- crd[, 2] * cos(pi) - crd[, 1] * sin(pi)
9694
rownames(crd) <- c(dimnames(net)[[1]], dimnames(net)[[2]])
9795
} else {
9896
if (center %in% node_names(.data)) {
@@ -107,7 +105,6 @@ layout_tbl_graph_hierarchy <- function(.data, center = NULL,
107105
}
108106
crd <- rbind(side1, side2)
109107
crd[which(is.nan(crd))] <- 0.5
110-
crd[, 2] <- crd[, 2] * cos(pi) - crd[, 1] * sin(pi)
111108
rownames(crd) <- c(dimnames(net)[[1]], dimnames(net)[[2]])
112109
} else stop("Please declare actors, events, or a node name as center.")
113110
}
@@ -139,7 +136,7 @@ layout_tbl_graph_alluvial <- function(.data,
139136
#' @rdname partition_layouts
140137
#' @export
141138
layout_tbl_graph_railway <- function(.data,
142-
circular = FALSE, times = 1000){
139+
circular = FALSE, times = 1000) {
143140
res <- layout_tbl_graph_hierarchy(as_igraph(.data))
144141
res$x <- c(match(res[res[,2]==0,1], sort(res[res[,2]==0,1])),
145142
match(res[res[,2]==1,1], sort(res[res[,2]==1,1])))
@@ -239,52 +236,21 @@ layout_tbl_graph_lineage <- function(.data, rank, circular = FALSE) {
239236
if (length(rank) > 1 & length(rank) != length(.data)) {
240237
stop("Please pass the function a `rank` node attribute or a vector.")
241238
} else if (length(rank) != length(.data)) {
242-
rank <- node_attribute(.data, rank)
243-
if (!is.numeric(rank))
244-
stop("Please declare a numeric attribute to `rank` nodes.")
239+
rank <- as.numeric(node_attribute(.data, rank))
245240
}
246241
thisRequiresBio("Rgraphviz")
247-
prep <- as_matrix(.data, twomode = FALSE)
248-
if(anyDuplicated(rownames(prep))){
249-
rownames(prep) <- seq_len(nrow(prep))
250-
colnames(prep) <- seq_len(ncol(prep))
251-
}
252-
if(any(prep<0)) prep[prep<0] <- 0
253-
out <- as_graphAM(prep)
254-
out <- suppressMessages(Rgraphviz::layoutGraph(out, layoutType = 'dot',
255-
attrs = list(graph = list(rankdir = "BT"))))
256-
nodeX <- .rescale(out@renderInfo@nodes$nodeX)
257-
names <- names(nodeX)
258-
nodeY <- .rescale(rank*(-1))
259-
.to_lo(.adjust(nodeX, nodeY, names))
242+
out <- layout_tbl_graph_alluvial(
243+
as_igraph(mutate(.data, type = ifelse(
244+
rank > mean(rank), TRUE, FALSE)), twomode = TRUE))
245+
out$x <- .rescale(rank)
246+
.check_dup(out)
260247
}
261248

262249
.rescale <- function(vector){
263250
(vector - min(vector)) / (max(vector) - min(vector))
264251
}
265252

266-
.adjust <- function(x, y, names) {
267-
out <- data.frame(cbind(x, y, names))
268-
adj <- data.frame()
269-
for (k in levels(as.factor(y))) {
270-
a <- subset(out, y == k)
271-
if (length(a[,1]) == 1) {
272-
a[,1] <- ifelse(a[,1] > 0.8, as.numeric(a[,1])*0.8,
273-
ifelse(a[,1] < 0.2, as.numeric(a[,1])*1.2,
274-
as.numeric(a[,1])))
275-
} else if (length(a[,1]) > 2) {
276-
a[,1] <- seq(min(a[,1]), max(a[,1]), len = length(a[,1]))
277-
}
278-
adj <- rbind(adj, a)
279-
}
280-
name <- data.frame(names = out[,3])
281-
out <- dplyr::left_join(name, adj, by = "names")
282-
out <- apply(out[,2:3], 2, as.numeric)
283-
rownames(out) <- name$names
284-
out
285-
}
286-
287-
.to_lo <- function(mat){
253+
.to_lo <- function(mat) {
288254
res <- as.data.frame(mat)
289255
names(res) <- c("x","y")
290256
res
@@ -299,6 +265,11 @@ to_list <- function(members){
299265
out
300266
}
301267

268+
.check_dup <- function(mat) {
269+
mat$y <- ifelse(duplicated(mat[c('x','y')]), mat$y*0.95, mat$y)
270+
mat
271+
}
272+
302273
#' @importFrom igraph degree
303274
getNNvec <- function(.data, members){
304275
lapply(members, function(circle){

0 commit comments

Comments
 (0)