Function for plotting network objects using ggplot2, with additional control
over graphical parameters that are not supported by the ggnet
function. Please visit https://github.com/briatte/ggnet for the latest
version of ggnet2, and https://briatte.github.io/ggnet/ for a vignette
that contains many examples and explanations.
ggnet2(
net,
mode = "fruchtermanreingold",
layout.par = NULL,
layout.exp = 0,
alpha = 1,
color = "grey75",
shape = 19,
size = 9,
max_size = 9,
na.rm = NA,
palette = NULL,
alpha.palette = NULL,
alpha.legend = NA,
color.palette = palette,
color.legend = NA,
shape.palette = NULL,
shape.legend = NA,
size.palette = NULL,
size.legend = NA,
size.zero = FALSE,
size.cut = FALSE,
size.min = NA,
size.max = NA,
label = FALSE,
label.alpha = 1,
label.color = "black",
label.size = max_size/2,
label.trim = FALSE,
node.alpha = alpha,
node.color = color,
node.label = label,
node.shape = shape,
node.size = size,
edge.alpha = 1,
edge.color = "grey50",
edge.lty = "solid",
edge.size = 0.25,
edge.label = NULL,
edge.label.alpha = 1,
edge.label.color = label.color,
edge.label.fill = "white",
edge.label.size = max_size/2,
arrow.size = 0,
arrow.gap = 0,
arrow.type = "closed",
legend.size = 9,
legend.position = "right",
...
)
an object of class network
, or any object
that can be coerced to this class, such as an adjacency or incidence matrix,
or an edge list: see edgeset.constructors and
network for details. If the object is of class
igraph and the
intergraph package is installed,
it will be used to convert the object: see
asNetwork
for details.
a placement method from those provided in the
sna
package: see gplot.layout for
details. Also accepts the names of two numeric vertex attributes of
net
, or a matrix of numeric coordinates, in which case the first two
columns of the matrix are used.
Defaults to the Fruchterman-Reingold force-directed algorithm.
options to be passed to the placement method, as listed in
gplot.layout.
Defaults to NULL
.
a multiplier to expand the horizontal axis if node labels
get clipped: see expand_range for details.
Defaults to 0
(no expansion).
the level of transparency of the edges and nodes, which might be
a single value, a vertex attribute, or a vector of values.
Also accepts "mode"
on bipartite networks (see 'Details').
Defaults to 1
(no transparency).
the color of the nodes, which might be a single value, a vertex
attribute, or a vector of values.
Also accepts "mode"
on bipartite networks (see 'Details').
Defaults to grey75
.
the shape of the nodes, which might be a single value, a vertex
attribute, or a vector of values.
Also accepts "mode"
on bipartite networks (see 'Details').
Defaults to 19
(solid circle).
the size of the nodes, in points, which might be a single value,
a vertex attribute, or a vector of values. Also accepts "indegree"
,
"outdegree"
, "degree"
or "freeman"
to size the nodes by
their unweighted degree centrality ("degree"
and "freeman"
are
equivalent): see degree
for details. All node sizes must
be strictly positive.
Also accepts "mode"
on bipartite networks (see 'Details').
Defaults to 9
.
the maximum size of the node when size
produces
nodes of different sizes, in points.
Defaults to 9
.
whether to subset the network to nodes that are not
missing a given vertex attribute. If set to any vertex attribute of
net
, the nodes for which this attribute is NA
will be removed.
Defaults to NA
(does nothing).
the palette to color the nodes, when color
is not a
color value or a vector of color values. Accepts named vectors of color
values, or if RColorBrewer is installed, any
ColorBrewer palette name: see RColorBrewer::brewer.pal()
and
https://colorbrewer2.org/ for details.
Defaults to NULL
, which will create an array of grayscale color values
if color
is not a color value or a vector of color values.
the palette to control the transparency levels of the
nodes set by alpha
when the levels are not numeric values.
Defaults to NULL
, which will create an array of alpha transparency
values if alpha
is not a numeric value or a vector of numeric values.
the name to assign to the legend created by
alpha
when its levels are not numeric values.
Defaults to NA
(no name).
see palette
the name to assign to the legend created by
palette
.
Defaults to NA
(no name).
the palette to control the shapes of the nodes set by
shape
when the shapes are not numeric values.
Defaults to NULL
, which will create an array of shape values if
shape
is not a numeric value or a vector of numeric values.
the name to assign to the legend created by
shape
when its levels are not numeric values.
Defaults to NA
(no name).
the palette to control the sizes of the nodes set by
size
when the sizes are not numeric values.
the name to assign to the legend created by
size
.
Defaults to NA
(no name).
whether to accept zero-sized nodes based on the value(s) of
size
.
Defaults to FALSE
, which ensures that zero-sized nodes are still
shown in the plot and its size legend.
whether to cut the size of the nodes into a certain number of
quantiles. Accepts TRUE
, which tries to cut the sizes into quartiles,
or any positive numeric value, which tries to cut the sizes into that many
quantiles. If the size of the nodes do not contain the specified number of
distinct quantiles, the largest possible number is used.
See quantile
and cut
for details.
Defaults to FALSE
(does nothing).
whether to subset the network to nodes with a minimum size,
based on the values of size
.
Defaults to NA
(preserves all nodes).
whether to subset the network to nodes with a maximum size,
based on the values of size
.
Defaults to NA
(preserves all nodes).
whether to label the nodes. If set to TRUE
, nodes are
labeled with their vertex names. If set to a vector that contains as many
elements as there are nodes in net
, nodes are labeled with these. If
set to any other vector of values, the nodes are labeled only when their
vertex name matches one of these values.
Defaults to FALSE
(no labels).
the level of transparency of the node labels, as a
numeric value, a vector of numeric values, or as a vertex attribute
containing numeric values.
Defaults to 1
(no transparency).
the color of the node labels, as a color value, a vector
of color values, or as a vertex attribute containing color values.
Defaults to "black"
.
the size of the node labels, in points, as a numeric value,
a vector of numeric values, or as a vertex attribute containing numeric
values.
Defaults to max_size / 2
(half the maximum node size), which defaults
to 4.5
.
whether to apply some trimming to the node labels. Accepts
any function that can process a character vector, or a strictly positive
numeric value, in which case the labels are trimmed to a fixed-length
substring of that length: see substr
for details.
Defaults to FALSE
(does nothing).
see alpha
see color
see label
see shape
see size
the level of transparency of the edges.
Defaults to the value of alpha
, which defaults to 1
.
the color of the edges, as a color value, a vector of color
values, or as an edge attribute containing color values.
Defaults to "grey50"
.
the linetype of the edges, as a linetype value, a vector of
linetype values, or as an edge attribute containing linetype values.
Defaults to "solid"
.
the size of the edges, in points, as a numeric value, a
vector of numeric values, or as an edge attribute containing numeric values.
All edge sizes must be strictly positive.
Defaults to 0.25
.
the labels to plot at the middle of the edges, as a single
value, a vector of values, or as an edge attribute.
Defaults to NULL
(no edge labels).
the level of transparency of the edge labels, as a
numeric value, a vector of numeric values, or as an edge attribute
containing numeric values.
Defaults to 1
(no transparency).
the color of the edge labels, as a color value, a
vector of color values, or as an edge attribute containing color values.
Defaults to label.color
, which defaults to "black"
.
the background color of the edge labels.
Defaults to "white"
.
the size of the edge labels, in points, as a numeric
value, a vector of numeric values, or as an edge attribute containing numeric
values. All edge label sizes must be strictly positive.
Defaults to max_size / 2
(half the maximum node size), which defaults
to 4.5
.
the size of the arrows for directed network edges, in
points. See arrow
for details.
Defaults to 0
(no arrows).
a setting aimed at improving the display of edge arrows by
plotting slightly shorter edges. Accepts any value between 0
and
1
, where a value of 0.05
will generally achieve good results
when the size of the nodes is reasonably small.
Defaults to 0
(no shortening).
the type of the arrows for directed network edges. See
arrow
for details.
Defaults to "closed"
.
the size of the legend symbols and text, in points.
Defaults to 9
.
the location of the plot legend(s). Accepts all
legend.position
values supported by theme
.
Defaults to "right"
.
other arguments passed to the geom_text
object that sets
the node labels: see geom_text
for details.
The degree centrality measures that can be produced through the
size
argument will take the directedness of the network into account,
but will be unweighted. To compute weighted network measures, see the
tnet
package by Tore Opsahl (help("tnet", package = "tnet")
).
The nodes of bipartite networks can be mapped to their mode by passing the
"mode"
argument to any of alpha
, color
, shape
and
size
, in which case the nodes of the primary mode will be mapped as
"actor"
, and the nodes of the secondary mode will be mapped as
"event"
.
ggnet
in this package,
gplot
in the sna
package, and
plot.network
in the network
package
# Small function to display plots only if it's interactive
p_ <- GGally::print_if_interactive
library(network)
# random adjacency matrix
x <- 10
ndyads <- x * (x - 1)
density <- x / ndyads
m <- matrix(0, nrow = x, ncol = x)
dimnames(m) <- list(letters[1:x], letters[1:x])
m[row(m) != col(m)] <- runif(ndyads) < density
m
#> a b c d e f g h i j
#> a 0 0 1 0 0 0 0 0 0 0
#> b 0 0 1 0 0 0 0 1 1 0
#> c 1 0 0 0 0 0 0 0 0 0
#> d 0 0 0 0 0 0 0 0 0 0
#> e 0 0 0 1 0 0 0 0 0 0
#> f 1 0 0 1 0 0 0 0 0 0
#> g 0 0 0 0 0 0 0 0 0 0
#> h 0 0 0 0 1 0 0 0 0 0
#> i 1 0 0 0 1 0 0 0 0 0
#> j 1 0 0 0 0 0 0 0 1 0
# random undirected network
n <- network::network(m, directed = FALSE)
n
#> Network attributes:
#> vertices = 10
#> directed = FALSE
#> hyper = FALSE
#> loops = FALSE
#> multiple = FALSE
#> bipartite = FALSE
#> total edges= 12
#> missing edges= 0
#> non-missing edges= 12
#>
#> Vertex attribute names:
#> vertex.names
#>
#> No edge attributes
p_(ggnet2(n, label = TRUE))
p_(ggnet2(n, label = TRUE, shape = 15))
p_(ggnet2(n, label = TRUE, shape = 15, color = "black", label.color = "white"))
# add vertex attribute
x = network.vertex.names(n)
x = ifelse(x %in% c("a", "e", "i"), "vowel", "consonant")
n %v% "phono" = x
p_(ggnet2(n, color = "phono"))
p_(ggnet2(n, color = "phono", palette = c("vowel" = "gold", "consonant" = "grey")))
p_(ggnet2(n, shape = "phono", color = "phono"))
#> Warning: Duplicated `override.aes` is ignored.
if (require(RColorBrewer)) {
# random groups
n %v% "group" <- sample(LETTERS[1:3], 10, replace = TRUE)
p_(ggnet2(n, color = "group", palette = "Set2"))
}
#> Loading required package: RColorBrewer
# random weights
n %e% "weight" <- sample(1:3, network.edgecount(n), replace = TRUE)
p_(ggnet2(n, edge.size = "weight", edge.label = "weight"))
# edge arrows on a directed network
p_(ggnet2(network(m, directed = TRUE), arrow.gap = 0.05, arrow.size = 10))
# Padgett's Florentine wedding data
data(flo, package = "network")
flo
#> Acciaiuoli Albizzi Barbadori Bischeri Castellani Ginori Guadagni
#> Acciaiuoli 0 0 0 0 0 0 0
#> Albizzi 0 0 0 0 0 1 1
#> Barbadori 0 0 0 0 1 0 0
#> Bischeri 0 0 0 0 0 0 1
#> Castellani 0 0 1 0 0 0 0
#> Ginori 0 1 0 0 0 0 0
#> Guadagni 0 1 0 1 0 0 0
#> Lamberteschi 0 0 0 0 0 0 1
#> Medici 1 1 1 0 0 0 0
#> Pazzi 0 0 0 0 0 0 0
#> Peruzzi 0 0 0 1 1 0 0
#> Pucci 0 0 0 0 0 0 0
#> Ridolfi 0 0 0 0 0 0 0
#> Salviati 0 0 0 0 0 0 0
#> Strozzi 0 0 0 1 1 0 0
#> Tornabuoni 0 0 0 0 0 0 1
#> Lamberteschi Medici Pazzi Peruzzi Pucci Ridolfi Salviati Strozzi
#> Acciaiuoli 0 1 0 0 0 0 0 0
#> Albizzi 0 1 0 0 0 0 0 0
#> Barbadori 0 1 0 0 0 0 0 0
#> Bischeri 0 0 0 1 0 0 0 1
#> Castellani 0 0 0 1 0 0 0 1
#> Ginori 0 0 0 0 0 0 0 0
#> Guadagni 1 0 0 0 0 0 0 0
#> Lamberteschi 0 0 0 0 0 0 0 0
#> Medici 0 0 0 0 0 1 1 0
#> Pazzi 0 0 0 0 0 0 1 0
#> Peruzzi 0 0 0 0 0 0 0 1
#> Pucci 0 0 0 0 0 0 0 0
#> Ridolfi 0 1 0 0 0 0 0 1
#> Salviati 0 1 1 0 0 0 0 0
#> Strozzi 0 0 0 1 0 1 0 0
#> Tornabuoni 0 1 0 0 0 1 0 0
#> Tornabuoni
#> Acciaiuoli 0
#> Albizzi 0
#> Barbadori 0
#> Bischeri 0
#> Castellani 0
#> Ginori 0
#> Guadagni 1
#> Lamberteschi 0
#> Medici 1
#> Pazzi 0
#> Peruzzi 0
#> Pucci 0
#> Ridolfi 1
#> Salviati 0
#> Strozzi 0
#> Tornabuoni 0
p_(ggnet2(flo, label = TRUE))
p_(ggnet2(flo, label = TRUE, label.trim = 4, vjust = -1, size = 3, color = 1))
p_(ggnet2(flo, label = TRUE, size = 12, color = "white"))