Fortify method for networks of class network

Share:

Description

See the vignette at https://briatte.github.io/ggnetwork/ for a description of both this function and the rest of the ggnetwork package.

Usage

1
2
3
4
## S3 method for class 'network'
fortify(model, data = NULL,
  layout = "fruchtermanreingold", weights = NULL,
  arrow.gap = ifelse(network::is.directed(x), 0.025, 0), by = NULL, ...)

Arguments

model

an object of class network.

data

not used by this method.

layout

a network layout supplied by gplot.layout, such as "fruchtermanreingold" (the default), or a two-column matrix with as many rows as there are nodes in the network, in which case the matrix is used as nodes coordinates.

weights

the name of an edge attribute to use as edge weights when computing the network layout, if the layout supports such weights (see 'Details'). Defaults to NULL (no edge weights).

arrow.gap

a parameter that will shorten the network edges in order to avoid overplotting edge arrows and nodes; defaults to 0 when the network is undirected (no edge shortening), or to 0.025 when the network is directed. Small values near 0.025 will generally achieve good results when the size of the nodes is reasonably small.

by

a character vector that matches an edge attribute, which will be used to generate a data frame that can be plotted with facet_wrap or facet_grid. The nodes of the network will appear in all facets, at the same coordinates. Defaults to NULL (no faceting).

...

additional parameters for the layout argument; see gplot.layout for available options.

Details

fortify.network will return a warning if it finds duplicated edges after converting the network to an edge list. Duplicated edges should be eliminated in favour of single weighted edges before using a network layout that supports edge weights, such as the Kamada-Kawai force-directed placement algorithm.

Value

a data.frame object.

Examples

 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
if (require(ggplot2) && require(network)) {

  # source: ?network::flo
  data(flo)

  # data example
  ggnetwork(flo)

  # plot example
  ggplot(ggnetwork(flo), aes(x, y, xend = xend, yend = yend)) +
    geom_edges(alpha = 0.5) +
    geom_nodes(size = 12, color = "white") +
    geom_nodetext(aes(label = vertex.names), fontface = "bold") +
    theme_blank()

  # source: ?network::emon
  data(emon)

  # data example
  ggnetwork(emon[[1]], layout = "target", niter = 100)

  # data example with edge weights
  ggnetwork(emon[[1]], layout = "kamadakawai", weights = "Frequency")

  # plot example with straight edges
  ggplot(ggnetwork(emon[[1]], layout = "kamadakawai", arrow.gap = 0.025),
         aes(x, y, xend = xend, yend = yend)) +
    geom_edges(aes(color = Frequency),
               arrow = arrow(length = unit(10, "pt"), type = "closed")) +
    geom_nodes(aes(size = Formalization)) +
    scale_color_gradient(low = "grey50", high = "tomato") +
    scale_size_area(breaks = 1:3) +
    theme_blank()

  # plot example with curved edges
  ggplot(ggnetwork(emon[[1]], layout = "kamadakawai", arrow.gap = 0.025),
         aes(x, y, xend = xend, yend = yend)) +
    geom_edges(aes(color = Frequency), curvature = 0.1,
               arrow = arrow(length = unit(10, "pt"), type = "open")) +
    geom_nodes(aes(size = Formalization)) +
    scale_color_gradient(low = "grey50", high = "tomato") +
    scale_size_area(breaks = 1:3) +
    theme_blank()

  # facet by edge attribute
  ggplot(ggnetwork(emon[[1]], arrow.gap = 0.02, by = "Frequency"),
         aes(x, y, xend = xend, yend = yend)) +
    geom_edges(arrow = arrow(length = unit(5, "pt"), type = "closed")) +
    geom_nodes() +
    theme_blank() +
    facet_grid(. ~ Frequency, labeller = label_both)

  # user-provided layout
  ggplot(ggnetwork(emon[[1]], layout = matrix(runif(28), ncol = 2)),
         aes(x, y, xend = xend, yend = yend)) +
    geom_edges(arrow = arrow(length = unit(5, "pt"), type = "closed")) +
    geom_nodes() +
    theme_blank()

}