Skip to content

Commit 3cf1571

Browse files
authored
Merge pull request #64 from jhollway/develop
Migrated data to tidygraph-based structure with options
2 parents ad4f911 + 1dad12b commit 3cf1571

34 files changed

+169
-191
lines changed

.gitignore

+1
Original file line numberDiff line numberDiff line change
@@ -5,3 +5,4 @@ inst/doc
55
.Ruserdata
66
*.Rd
77
data-raw/*
8+
docs/

DESCRIPTION

+4-3
Original file line numberDiff line numberDiff line change
@@ -1,16 +1,17 @@
11
Package: roctopus
2-
Title: Utility functions for multimode and multilevel network analysis
3-
Version: 0.2.6
2+
Title: Multimodal and multilevel network analysis
3+
Version: 0.3.0
44
Description: This package assembles a number of utility functions for
55
loading, visualising, and analysing multimode, multiplex, and multilevel networks.
66
URL: https://github.com/jhollway/roctopus
77
BugReports: https://github.com/jhollway/roctopus/issues
88
Depends: R (>= 3.4.0)
9-
License: MIT + file LICENSE
9+
License: MIT
1010
Encoding: UTF-8
1111
LazyData: true
1212
RoxygenNote: 7.1.1
1313
Imports: igraph,
14+
tidygraph,
1415
geosphere,
1516
threejs,
1617
roxygen2,

NAMESPACE

+3-3
Original file line numberDiff line numberDiff line change
@@ -1,13 +1,13 @@
11
# Generated by roxygen2: do not edit by hand
22

33
export(col_project)
4-
export(create_lattice)
4+
export(create_chain)
55
export(create_match)
66
export(create_nest)
7-
export(create_poles)
8-
export(create_random)
7+
export(create_silos)
98
export(df_to_mat)
109
export(netlm2)
10+
export(play_twomode)
1111
export(plot_2x2)
1212
export(plot_globalnet)
1313
export(plot_multilevel)

NEWS.md

+15
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,18 @@
1+
# roctopus 0.3.0
2+
3+
2020-11-06
4+
5+
## Data
6+
7+
* Data creation updated for `{tidygraph}` defaults
8+
* Renamed `create_lattice()` to `create_chain()` to avoid conflicts with `tidygraph::create_lattice()`
9+
* Renamed `create_poles()` to `create_silos()`
10+
* Renamed `create_random()` to `play_twomode()` to avoid conflicts with `tidygraph::play_bipartite()`
11+
* Added export options for all `create_` and `play_` functions as `tbl_graph` (default), `igraph`, and base matrix
12+
* Updated tests for new `create_` and `play_` function names
13+
* Packaged data updated for `{tidygraph}` defaults
14+
* Renamed packaged data from book to `mpn_`
15+
116
# roctopus 0.2.6
217

318
2020-08-18

R/create.R

+91-51
Original file line numberDiff line numberDiff line change
@@ -1,102 +1,142 @@
1-
#' Two-mode lattice
1+
#' Two-mode chain
22
#'
3-
#' Creates a two-mode lattice
4-
#' @param node1 Number of nodes in the first node set
5-
#' @param node2 Number of nodes in the second node set
3+
#' Creates a two-mode chain
4+
#' @param n1 Number of nodes in the first node set
5+
#' @param n2 Number of nodes in the second node set
6+
#' @param as What type of object to return.
7+
#' One of "matrix", "tbl_graph", "igraph".
8+
#' By default, creates a "tbl_graph" object.
69
#' @details Will construct a bilateral lattice,
710
#' with two ties for every second-mode node.
811
#' @export
912
#' @examples
1013
#' \dontrun{
11-
#' create_lattice(10, 12)
14+
#' create_chain(5,10) %>% ggraph() +
15+
#' geom_edge_fan(aes(alpha = stat(index)), show.legend = FALSE) +
16+
#' geom_node_point(aes(size = 5))
1217
#' }
13-
create_lattice <- function(node1, node2) {
14-
mat <- matrix(0, node1, node2)
18+
create_chain <- function(n1, n2,
19+
as = c("tbl_graph", "igraph", "matrix")) {
20+
21+
mat <- matrix(0, n1, n2)
1522
out <- suppressWarnings(((row(mat) - col(mat)) == 0 |
16-
(row(mat) - col(mat)) == (-seq.int(0, node2 - 1, node1)[-1]) |
23+
(row(mat) - col(mat)) == (-seq.int(0, n2 - 1, n1)[-1]) |
1724
(row(mat) - col(mat)) == -1 |
18-
(row(mat) - col(mat)) == -seq.int(1 + node1, node2 - 1, node1) |
25+
(row(mat) - col(mat)) == -seq.int(1 + n1, n2 - 1, n1) |
1926
(row(mat) - col(mat)) == nrow(mat) - 1) * 1)
27+
28+
as <- match.arg(as)
29+
if(as == "tbl_graph") out <- tidygraph::as_tbl_graph(out)
30+
if(as == "igraph") out <- igraph::graph_from_incidence_matrix(out)
2031
out
2132
}
2233

23-
#' Two-mode random graph
34+
#' Matched two-mode graph
2435
#'
25-
#' Creates a random two-mode network
26-
#' @param node1 Number of nodes in the first node set
27-
#' @param node2 Number of nodes in the second node set
28-
#' @param density Number of edges in the network over the number of edges possible
36+
#' Creates a matched two-mode network
37+
#' @param n1 Number of nodes in the first node set
38+
#' @param n2 Number of nodes in the second node set
2939
#' @details Will construct an affiliation matrix,
30-
#' with a random probability of a tie.
40+
#' with by default both n1 and n2 matched.
41+
#' TODO: Incorporate into create_chain (chordal_ring of certain breadth w).
3142
#' @export
3243
#' @examples
3344
#' \dontrun{
34-
#' create_random(10, 12, 0.25)
45+
#' create_match(10, 12)
3546
#' }
36-
create_random <- function(node1, node2, density) {
37-
mat <- matrix(rbinom(node1 * node2, 1, density), node1, node2)
47+
create_match <- function(n1, n2,
48+
as = c("tbl_graph", "igraph", "matrix")) {
49+
mat <- matrix(0, n1, n2)
50+
mat <- matrix(
51+
((row(mat) - col(mat)) %in% unique(-seq(0, n2 - 1, n1),
52+
seq(0, n1 - 1, n2))) * 1,
53+
n1, n2
54+
)
55+
56+
as <- match.arg(as)
57+
if(as == "tbl_graph") mat <- tidygraph::as_tbl_graph(mat)
58+
if(as == "igraph") mat <- igraph::graph_from_incidence_matrix(mat)
3859
mat
3960
}
4061

41-
#' Two-component two-mode graph
62+
#' Two-mode random graph
4263
#'
43-
#' Creates a two-component two-mode network
44-
#' @param node1 Number of nodes in the first node set
45-
#' @param node2 Number of nodes in the second node set
64+
#' Creates a random two-mode network
65+
#' @param n1 Number of nodes in the first node set
66+
#' @param n2 Number of nodes in the second node set
67+
#' @param p Number of edges in the network over the number of edges possible
68+
#' @param m Number of edges in the network
69+
#' @param as What type of object to return.
70+
#' One of "matrix", "tbl_graph", "igraph".
71+
#' By default, creates a "tbl_graph" object.
4672
#' @details Will construct an affiliation matrix,
47-
#' with full component diagonal.
73+
#' with a random probability of a tie.
4874
#' @export
4975
#' @examples
5076
#' \dontrun{
51-
#' create_poles(10, 12)
77+
#' play_twomode(10, 12, 0.25) %>% ggraph() +
78+
#' geom_edge_fan(aes(alpha = stat(index)), show.legend = FALSE) +
79+
#' geom_node_point(aes(size = 5))
5280
#' }
53-
create_poles <- function(node1, node2) {
54-
mat <- matrix(0, node1, node2)
55-
mat[1:round(node1 / 2), 1:round(node2 / 2)] <- 1
56-
mat[rowSums(mat) < 1, colSums(mat) < 1] <- 1
57-
mat
81+
play_twomode <- function(n1, n2, p, m, directed = TRUE, mode = "out",
82+
as = c("tbl_graph", "igraph", "matrix")) {
83+
84+
g <- tidygraph::play_bipartite(n1, n2, p, m, directed, mode)
85+
86+
as <- match.arg(as)
87+
if(as == "igraph" | as == "matrix") g <- as.igraph(g)
88+
if(as == "matrix") g <- as_adjacency_matrix(g)
89+
g
5890
}
5991

60-
#' Matched two-mode graph
92+
#' Two-component two-mode graph
6193
#'
62-
#' Creates a matched two-mode network
63-
#' @param node1 Number of nodes in the first node set
64-
#' @param node2 Number of nodes in the second node set
94+
#' Creates a two-component two-mode network
95+
#' @param n1 Number of nodes in the first node set
96+
#' @param n2 Number of nodes in the second node set
6597
#' @details Will construct an affiliation matrix,
66-
#' with by default both node1 and node2 matched.
67-
#' TODO: Make node set matching optional.
98+
#' with full component diagonal.
99+
#' TODO: Allow specfication of how many silos/components to create
68100
#' @export
69101
#' @examples
70102
#' \dontrun{
71-
#' create_match(10, 12)
103+
#' create_silos(10, 12)
72104
#' }
73-
create_match <- function(node1, node2) {
74-
mat <- matrix(0, node1, node2)
75-
mat <- matrix(
76-
((row(mat) - col(mat)) %in% unique(-seq(0, node2 - 1, node1),
77-
seq(0, node1 - 1, node2))) * 1,
78-
node1, node2
79-
)
105+
create_silos <- function(n1, n2,
106+
as = c("tbl_graph", "igraph", "matrix")) {
107+
mat <- matrix(0, n1, n2)
108+
mat[1:round(n1 / 2), 1:round(n2 / 2)] <- 1
109+
mat[rowSums(mat) < 1, colSums(mat) < 1] <- 1
110+
111+
as <- match.arg(as)
112+
if(as == "tbl_graph") mat <- tidygraph::as_tbl_graph(mat)
113+
if(as == "igraph") mat <- igraph::graph_from_incidence_matrix(mat)
80114
mat
81115
}
82116

83117
#' Nested two-mode graph
84118
#'
85119
#' Creates a nested two-mode network
86-
#' @param node1 Number of nodes in the first node set
87-
#' @param node2 Number of nodes in the second node set
120+
#' @param n1 Number of nodes in the first node set
121+
#' @param n2 Number of nodes in the second node set
88122
#' @details Will construct an affiliation matrix,
89-
#' with decreasing fill across node2.
90-
#' TODO: Make node set matching optional.
123+
#' with decreasing fill across n2.
91124
#' @export
92125
#' @examples
93126
#' \dontrun{
94127
#' create_nest(10, 12)
95128
#' }
96-
create_nest <- function(node1, node2) {
97-
mat <- matrix(0, node1, node2)
98-
mat[(row(mat) - col(mat)) >= 0] <- 1
99-
mat
129+
create_nest <- function(n1, n2,
130+
as = c("tbl_graph", "igraph", "matrix")) {
131+
132+
as <- match.arg(as)
133+
134+
out <- matrix(0, n1, n2)
135+
out[(row(out) - col(out)) >= 0] <- 1
136+
137+
if(as == "tbl_graph") out <- tidygraph::as_tbl_graph(out)
138+
if(as == "igraph") out <- igraph::graph_from_incidence_matrix(out)
139+
out
100140
}
101141

102142
# mat.dist <- matrix(0,5,3)

R/data_bristol.R

+1-1
Original file line numberDiff line numberDiff line change
@@ -11,4 +11,4 @@
1111
#' \item{3_}{17 Major Protest and Civic Events in Bristol, 1990-2002}
1212
#' }
1313
#' @source Knoke, Christopoulos, Diani, and Hollway. 2020. Multimodal Political Networks. Cambridge University Press: Cambridge.
14-
"bristol"
14+
"mpn_bristol"

R/data_evs.R

+9-9
Original file line numberDiff line numberDiff line change
@@ -8,8 +8,8 @@
88
#'
99
#' @docType data
1010
#' @keywords datasets
11-
#' @name evs
12-
#' @usage data(IT_1990)
11+
#' @name mpn_evs
12+
#' @usage data(mpn_IT_1990)
1313
#' @format Matrices with 14 columns:
1414
#' \describe{
1515
#' \item{Welfare}{1 if individual associated}
@@ -28,15 +28,15 @@
2828
#' \item{Health}{1 if individual associated}
2929
#' }
3030
#' @source Knoke, Christopoulos, Diani, and Hollway. 2020. Multimodal Political Networks. Cambridge University Press: Cambridge.
31-
"IT_1990"
31+
"mpn_IT_1990"
3232

33-
#' @rdname evs
34-
#' @usage data(IT_2008)
35-
"IT_2008"
33+
#' @rdname mpn_evs
34+
#' @usage data(mpn_IT_2008)
35+
"mpn_IT_2008"
3636

37-
#' @rdname evs
38-
#' @usage data(DE_1990)
39-
"DE_1990"
37+
#' @rdname mpn_evs
38+
#' @usage data(mpn_DE_1990)
39+
"mpn_DE_1990"
4040

4141
#' @rdname evs
4242
#' @usage data(DE_2008)

R/data_mexicanpower.R

+3-3
Original file line numberDiff line numberDiff line change
@@ -11,12 +11,12 @@
1111
#'
1212
#' @docType data
1313
#' @keywords datasets
14-
#' @name mexicanpower
15-
#' @usage data(mexicanpower)
14+
#' @name mpn_mexicanpower
15+
#' @usage data(mpn_mexicanpower)
1616
#' @format Matrix with 11 rows/columns
1717
#' @source Knoke, David. 1990. \emph{Political Networks}.
1818
#'
1919
#' Knoke, Christopoulos, Diani, and Hollway. 2020.
2020
#' \emph{Multimodal Political Networks: The Structural Perspective}.
2121
#' Cambridge University Press: Cambridge.
22-
"mexicanpower"
22+
"mpn_mexicanpower"

R/data_opensecrets.R

+3-3
Original file line numberDiff line numberDiff line change
@@ -9,8 +9,8 @@
99
#'
1010
#' @docType data
1111
#' @keywords datasets
12-
#' @name opensecrets
13-
#' @usage data(opensecrets)
12+
#' @name mpn_opensecrets
13+
#' @usage data(mpn_opensecrets)
1414
#' @format Matrix with 26 rows and 6+6 columns
1515
#' @source Domhoff, G William. 2016.
1616
#' “Who Rules America? Power Elite Database.” http://www2.ucsc.edu/whorulesamerica/power_elite/.
@@ -20,4 +20,4 @@
2020
#' Knoke, Christopoulos, Diani, and Hollway. 2020.
2121
#' \emph{Multimodal Political Networks: The Structural Perspective}.
2222
#' Cambridge University Press: Cambridge.
23-
"opensecrets"
23+
"mpn_opensecrets"

R/data_powerelite.R

+3-3
Original file line numberDiff line numberDiff line change
@@ -9,13 +9,13 @@
99
#'
1010
#' @docType data
1111
#' @keywords datasets
12-
#' @name powerelite
13-
#' @usage data(powerelite)
12+
#' @name mpn_powerelite
13+
#' @usage data(mpn_powerelite)
1414
#' @format Matrix with 14 rows and 20 columns
1515
#' @source Domhoff, G William. 2016.
1616
#' “Who Rules America? Power Elite Database.” http://www2.ucsc.edu/whorulesamerica/power_elite/.
1717
#'
1818
#' Knoke, Christopoulos, Diani, and Hollway. 2020.
1919
#' \emph{Multimodal Political Networks: The Structural Perspective}.
2020
#' Cambridge University Press: Cambridge.
21-
"powerelite"
21+
"mpn_powerelite"

R/data_ryanair.R

+3-3
Original file line numberDiff line numberDiff line change
@@ -9,12 +9,12 @@
99
#'
1010
#' @docType data
1111
#' @keywords datasets
12-
#' @name ryanair
13-
#' @usage data(ryanair)
12+
#' @name mpn_ryanair
13+
#' @usage data(mpn_ryanair)
1414
#' @format Matrix with 20 rows/columns
1515
#' @source Christopoulos, Dimitrios C. 2006.
1616
#' “Relational Attributes of Political Entrepreneurs: a Network Perspective.”
1717
#' \emph{Journal of European Public Policy} 13 (5): 757–78.
1818
#'
1919
#' Knoke, Christopoulos, Diani, and Hollway. 2020. \emph{Multimodal Political Networks}. Cambridge University Press: Cambridge.
20-
"ryanair"
20+
"mpn_ryanair"

R/data_senate112.R

+9-9
Original file line numberDiff line numberDiff line change
@@ -19,20 +19,20 @@
1919
#'
2020
#' @docType data
2121
#' @keywords datasets
22-
#' @name senate112
23-
#' @usage data(DemSxP)
22+
#' @name mpn_senate112
23+
#' @usage data(mpn_DemSxP)
2424
#' @format Matrix of 51 rows (Senators) and 63 columns (PACS)
2525
#' @source Knoke, Christopoulos, Diani, and Hollway. 2020.
2626
#' \emph{Multimodal Political Networks}.
2727
#' Cambridge University Press: Cambridge.
28-
"DemSxP"
28+
"mpn_DemSxP"
2929

30-
#' @rdname senate112
31-
#' @usage data(RepSxP)
30+
#' @rdname mpn_senate112
31+
#' @usage data(mpn_RepSxP)
3232
#' @format Matrix of 62 rows (Senators) and 72 columns (PACS)
33-
"RepSxP"
33+
"mpn_RepSxP"
3434

35-
#' @rdname senate112
36-
#' @usage data(OverSxP)
35+
#' @rdname mpn_senate112
36+
#' @usage data(mpn_OverSxP)
3737
#' @format Matrix of 20 rows (Senators) and 32 columns (PACS)
38-
"OverSxP"
38+
"mpn_OverSxP"

0 commit comments

Comments
 (0)