Draw the edges of a network.

Share:

Description

All arguments to this geom are identical to those of geom_segment, including arrow, which is useful to plot directed networks in conjunction with the arrow.gap argument of fortify.network. The curvature, angle and ncp arguments of geom_curve are also available: if curvature is set to any value above 0 (the default), the edges produced by geom_edges will be curved.

Usage

1
2
3
geom_edges(mapping = NULL, data = NULL, position = "identity",
  arrow = NULL, curvature = 0, angle = 90, ncp = 5, na.rm = FALSE,
  show.legend = NA, inherit.aes = TRUE, ...)

Arguments

mapping

Set of aesthetic mappings created by aes or aes_. If specified and inherit.aes = TRUE (the default), it is combined with the default mapping at the top level of the plot. You must supply mapping if there is no plot mapping.

data

The data to be displayed in this layer. There are three options:

If NULL, the default, the data is inherited from the plot data as specified in the call to ggplot.

A data.frame, or other object, will override the plot data. All objects will be fortified to produce a data frame. See fortify for which variables will be created.

A function will be called with a single argument, the plot data. The return value must be a data.frame., and will be used as the layer data.

position

Position adjustment, either as a string, or the result of a call to a position adjustment function.

arrow

specification for arrow heads, as created by arrow()

curvature

A numeric value giving the amount of curvature. Negative values produce left-hand curves, positive values produce right-hand curves, and zero produces a straight line.

angle

A numeric value between 0 and 180, giving an amount to skew the control points of the curve. Values less than 90 skew the curve towards the start point and values greater than 90 skew the curve towards the end point.

ncp

The number of control points used to draw the curve. More control points creates a smoother curve.

na.rm

If FALSE (the default), removes missing values with a warning. If TRUE silently removes missing values.

show.legend

logical. Should this layer be included in the legends? NA, the default, includes if any aesthetics are mapped. FALSE never includes, and TRUE always includes.

inherit.aes

If FALSE, overrides the default aesthetics, rather than combining with them. This is most useful for helper functions that define both data and aesthetics and shouldn't inherit behaviour from the default plot specification, e.g. borders.

...

other arguments passed on to layer. These are often aesthetics, used to set an aesthetic to a fixed value, like color = "red" or size = 3. They may also be parameters to the paired geom/stat.

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

# rerun if the example does not produce reciprocated ties
n <- network(rgraph(10, tprob = 0.2), directed = TRUE)

# just edges
ggplot(n, aes(x, y, xend = xend, yend = yend)) +
  geom_edges(size = 1, color = "steelblue") +
  theme_blank()

# with nodes
ggplot(n, aes(x, y, xend = xend, yend = yend)) +
  geom_edges(size = 1, color = "steelblue") +
  geom_nodes(size = 3, color = "steelblue") +
  theme_blank()

# with arrows
ggplot(n, aes(x, y, xend = xend, yend = yend)) +
  geom_edges(size = 1, color = "steelblue",
             arrow = arrow(length = unit(0.5, "lines"), type = "closed")) +
  geom_nodes(size = 3, color = "steelblue") +
  theme_blank()

# with curvature
ggplot(n, aes(x, y, xend = xend, yend = yend)) +
  geom_edges(size = 1, color = "steelblue", curvature = 0.15,
             arrow = arrow(length = unit(0.5, "lines"), type = "closed")) +
  geom_nodes(size = 3, color = "steelblue") +
  theme_blank()

# arbitrary categorical edge attribute
e <- sample(letters[ 1:2 ], network.edgecount(n), replace = TRUE)
set.edge.attribute(n, "type", e)
ggplot(n, aes(x, y, xend = xend, yend = yend)) +
  geom_edges(aes(linetype = type), size = 1, curvature = 0.15,
             arrow = arrow(length = unit(0.5, "lines"), type = "closed")) +
  geom_nodes(size = 3, color = "steelblue") +
  theme_blank()

# arbitrary numeric edge attribute (signed network)
e <- sample(-2:2, network.edgecount(n), replace = TRUE)
set.edge.attribute(n, "weight", e)
ggplot(n, aes(x, y, xend = xend, yend = yend)) +
  geom_edges(aes(color = weight), curvature = 0.15,
             arrow = arrow(length = unit(0.5, "lines"), type = "closed")) +
  geom_nodes(size = 3, color = "grey50") +
  scale_color_gradient(low = "steelblue", high = "tomato") +
  theme_blank()

# draw only a subset of all edges
positive_weight <- function(x) { x[ x$weight >= 0, ] }
ggplot(n, aes(x, y, xend = xend, yend = yend)) +
  geom_edges(aes(color = weight), data = positive_weight) +
  geom_nodes(size = 4, color = "grey50") +
  scale_color_gradient(low = "gold", high = "tomato") +
  theme_blank()

}