/
example-structures.R
159 lines (140 loc) · 3.86 KB
/
example-structures.R
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
#' Build an Example Adjacency Matrix
#'
#' @template param-directed
#' @template param-loops
#'
#' @template author-bk
#'
#' @examples
#' example_adj_mat()
#'
#' example_adj_mat(directed = TRUE)
#'
#' example_adj_mat(loops = TRUE)
#'
#' example_adj_mat(directed = TRUE, loops = TRUE)
#'
#' @export
example_adj_mat <- function(directed = FALSE, loops = FALSE) {
init <- structure(c(0, 0, 1, 0, 1, 0, 0, 0, 0, 1, 1, 1, 0, 1, 1, 1, 1,
1, 0, 0, 1, 0, 1, 0, 0), .Dim = c(5L, 5L))
if (loops) {
diag(init) <- 1
}
if (directed) {
return(init)
}
init[] <- init + t(init)
init %/% 2
}
#' Build an Example `<igraph>`
#'
#' @template param-directed
#' @template param-loops
#'
#' @examples
#' example_igraph()
#'
#' example_igraph(directed = TRUE)
#'
#' example_igraph(loops = TRUE)
#'
#' example_igraph(directed = TRUE, loops = TRUE)
#'
#' @importFrom igraph ecount edge_attr<- graph_from_adjacency_matrix vertex_attr<-
#' @export
example_igraph <- function(directed = FALSE, loops = FALSE) {
out <- graph_from_adjacency_matrix(
adjmatrix = example_adj_mat(directed, loops),
mode = if (directed) "directed" else "undirected"
)
vertex_attr(out, "color") <- c(
"red", "yellow", "green", "lightblue", "salmon"
)
vertex_attr(out, "size") <- seq_len(vcount(out)) * 2
vertex_attr(out, "x") <- c(1, 5, 2, 3, 4)
vertex_attr(out, "y") <- rev(c(1, 5, 2, 3, 4))
vertex_attr(out, "name") <- letters[seq_len(vcount(out))]
list_attr <- mapply(c, LETTERS, letters, SIMPLIFY = FALSE, USE.NAMES = FALSE
)[seq_len(vcount(out))]
list_attr[[1L]] <- list(NULL)
vertex_attr(out, "list_attr") <- list_attr
hetero_attr <- c(as.list(seq_len(vcount(out) - 1)), "a")
vertex_attr(out, "hetero_attr") <- hetero_attr
edge_attr(out, "weight") <- seq_len(ecount(out))
out
}
#' Build an Example `<network>`
#'
#' @template param-directed
#' @template param-loops
#'
#' @examples
#' example_network()
#'
#' example_network(directed = TRUE)
#'
#' example_network(loops = TRUE)
#'
#' example_network(directed = TRUE, loops = TRUE)
#'
#' @importFrom network as.network.matrix network.edgecount network.size
#' @importFrom network set.edge.attribute set.vertex.attribute
#' @export
example_network <- function(directed = FALSE, loops = FALSE) {
out <- as.network.matrix(
x = example_adj_mat(directed, loops),
matrix.type = "adjacency",
directed = directed,
loops = loops,
hyper = FALSE,
multiple = FALSE
)
set.vertex.attribute(
out, "color",
value = c("red", "yellow", "green", "lightblue", "salmon")
)
set.vertex.attribute(
out, "size",
value = seq_len(network.size(out)) * 2
)
set.vertex.attribute(
out, "x",
value = c(1, 5, 2, 3, 4)
)
set.vertex.attribute(
out, "y",
value = rev(c(1, 5, 2, 3, 4))
)
set.vertex.attribute(
out, "vertex.names", value = letters[seq_len(network::network.size(out))]
)
list_attr <- mapply(c, LETTERS, letters, SIMPLIFY = FALSE, USE.NAMES = FALSE
)[seq_len(network.size(out))]
list_attr[[1L]] <- list(NULL)
set.vertex.attribute(out, "list_attr", value = list_attr)
hetero_attr <- c(as.list(seq_len(network.size(out) - 1)), "a")
set.vertex.attribute(out, "hetero_attr", value = hetero_attr)
set.edge.attribute(
out, "weight", value = seq_len(network.edgecount(out))
)
out
}
# plot_example <- function(x, ...) {
# UseMethod("plot_example")
# }
# plot_example.igraph <- function(x, ...) {
# plot(x)
# }
# plot_example.network <- function(x, ...) {
# network::plot.network(
# x, displaylabels = TRUE,
# vertex.cex = network::get.vertex.attribute(x, "size") * 0.25,
# coord = cbind(network::get.vertex.attribute(x, "x"),
# network::get.vertex.attribute(x, "y")),
# jitter = FALSE,
# vertex.col = network::get.vertex.attribute(x, "color"),
# edge.col = "gray"
#
# )
# }