geom_net: Networks

Description Usage Arguments Format Value Aesthetics Examples

View source: R/geom-net.r

Description

The net geom is used visualize networks within the ggplot2 framework. geom_net combines the many parts of a network visualization into a single layer in ggplot2. It makes use of various other geoms, including but not limited to, geom_point, geom_segment, and geom_text.

Usage

 1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
12
13
14
15
16
17
geom_net(mapping = NULL, data = NULL, stat = "net",
  position = "identity", show.legend = NA, na.rm = FALSE,
  inherit.aes = TRUE, layout.alg = "kamadakawai", layout.par = list(),
  directed = FALSE, fiteach = FALSE, selfloops = FALSE,
  singletons = TRUE, alpha = 0.25, ecolour = NULL, ealpha = NULL,
  arrow = NULL, arrowgap = 0.01, arrowsize = 1, labelon = FALSE,
  labelcolour = NULL, labelgeom = "text", repel = FALSE,
  vertices = NULL, ...)

GeomNet

StatNet

stat_net(mapping = NULL, data = NULL, geom = "net",
  position = "identity", show.legend = NA, inherit.aes = TRUE,
  layout.alg = "kamadakawai", layout.par = list(), fiteach = FALSE,
  vertices = NULL, singletons = TRUE, na.rm = FALSE, ...)

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.

stat

The statistical transformation to use on the data for this layer, as a string.

position

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

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.

na.rm

If FALSE, the default, missing values are removed with a warning. If TRUE, missing values are silently removed.

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.

layout.alg

character. Value specifying the layout algorithm to use. Defaults to "kamadakawai". See ?sna::gplot.layout documentation for more choices.

layout.par

list. Parameters detailing algorithmic specs. Default parameters from sna are used initially. See ?sna::gplot.layout documentation for all options corresponding to all layouts.

directed

logical value. Should an arrow be drawn pointing to the to_id node? Default is FALSE.

fiteach

logical. Should the network be fit in each of the panels separately, or is there going to be one fit for all?

selfloops

logical value. Should loops (self-referencing edges) be shown (by drawing a circle adjacent to the corresponding node)? Default is FALSE.

singletons

logical. Should singletons (nodes with no incoming or outgoing ties) be plotted? Default is TRUE.

alpha

numeric. Value from 0-1 of alpha blending of nodes.

ecolour

character. Colour for edges.

ealpha

numeric. Value from 0-1 of alpha blending of edges.

arrow

what kind of arrow should be drawn? See ?grid::arrow for more.

arrowgap

numeric value between 0 and 1 specifying how much (as a proportion of the line length) earlier the line segment should be stopped drawing before reaching the target node. This parameters is only regarded in directed networks.

arrowsize

numeric. How big should the arrow be drawn? Multiplicative of the default, 10 points.

labelon

logical. Include labels for all nodes. Labels are taken from the from_id variable, unless a label aesthetic is provided.

labelcolour

character. Colour for the labels. If this argument is not specified, labels have the same colour as the nodes.

labelgeom

character. Which ggplot2 geom should be used to draw the labels? Either "text" or "label". Default is "text"

repel

logical. If TRUE, uses the ggrepel package geoms to draw the node labels instead of the ggplot2 geoms.

vertices

data frame. Dataset containing vertex information. Usage is a bit awkward, because every variable in this data set can only be used with the ggplot2 double dot representation ..varname.. Better: use the fortify.edgedf method

...

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.

geom

geom_net, the geom attached to stat_net is called "net".

Format

An object of class GeomNet (inherits from Geom, ggproto) of length 6.

Value

A data frame with additional columns:

x, y

coordinates of the nodes, beginning of edges,

xend, yend

coordinates end points of edges.

Aesthetics

geom_net understands the following aesthetics (required aesthetics are in bold):

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
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
## Not run: 
library(geomnet)
data(blood)
p <- ggplot(data = blood$edges, aes(from_id = from, to_id = to))
p + geom_net(vertices=blood$vertices, aes(colour=..type..)) + theme_net()

bloodnet <- fortify(as.edgedf(blood$edges), blood$vertices)
p <- ggplot(data = bloodnet, aes(from_id = from_id, to_id = to_id))
p + geom_net()
p + geom_net(aes(colour=rho)) + theme_net()
p + geom_net(aes(colour=rho), labelon=TRUE, vjust = -0.5)
p + geom_net(aes(colour=rho, linetype = group_to, label = from_id),  
             vjust=-0.5, labelcolour="black", directed=TRUE) + 
     theme_net()
p + geom_net(colour = "orange", layout.alg = 'circle', size = 6)
p + geom_net(colour = "orange", layout.alg = 'circle', size = 6, linewidth=.75)
p + geom_net(colour = "orange", layout.alg = 'circle', size = 0, linewidth=.75,
             directed = TRUE)
p + geom_net(aes(size=Predominance, colour=rho, shape=rho, linetype=group_to),
             linewidth=0.75, labelon =TRUE, labelcolour="black") +
    facet_wrap(~Ethnicity) +
    scale_colour_brewer(palette="Set2")
gg <- ggplot(data = blood$edges, aes(from_id = from, to_id = to)) +
  geom_net(colour = "darkred", layout.alg = "circle", labelon=TRUE, size = 15,
         directed = TRUE, vjust = 0.5, labelcolour = "grey80",
         arrowsize = 1.5, linewidth = 0.5, arrowgap = 0.05,
         selfloops = TRUE, ecolour = "grey40") +
  theme_net()
gg
dframe <- ggplot_build(gg)$data[[1]] # contains calculated node and edge values

#Madmen Relationships
data(madmen)
MMnet <- fortify(as.edgedf(madmen$edges), madmen$vertices)
p <- ggplot(data = MMnet, aes(from_id = from_id, to_id = to_id))
p + geom_net(labelon=TRUE)
p + geom_net(aes(colour=Gender), size=6, linewidth=1, labelon=TRUE, fontsize=3, labelcolour="black")
p + geom_net(aes(colour=Gender), size=6, linewidth=1, labelon=TRUE, labelcolour="black") +
    scale_colour_manual(values=c("#FF69B4", "#0099ff")) + xlim(c(-.05,1.05))
p + geom_net(aes(colour=Gender), size=6, linewidth=1, directed=TRUE, labelon=TRUE,
             arrowgap=0.01, labelcolour="black") +
    scale_colour_manual(values=c("#FF69B4", "#0099ff")) + xlim(c(-.05,1.05))

p <- ggplot(data = MMnet, aes(from_id = from_id, to_id = to_id))
# alternative labelling: specify label aesthetic.
p + geom_net(aes(colour=Gender, label=Gender), size=6, linewidth=1, fontsize=3,
             labelcolour="black")

## visualizing ggplot2 theme elements
data(theme_elements)
TEnet <- fortify(as.edgedf(theme_elements$edges[,c(2,1)]), theme_elements$vertices)
ggplot(data = TEnet, aes(from_id = from_id, to_id = to_id)) +
  geom_net(labelon=TRUE, vjust=-0.5)

## emails example from VastChallenge 2014
# care has to be taken to make sure that for each panel all nodes are included with
# the necessary information.
# Otherwise line segments show on the plot without nodes.
emailedges <- as.edgedf(subset(email$edges, nrecipients < 54))
emailnet <- fortify(emailedges, email$nodes)
#no facets
ggplot(data = emailnet, aes(from_id = from_id, to_id = to_id)) +
  geom_net(aes(colour= CurrentEmploymentType), linewidth=0.5) +
  scale_colour_brewer(palette="Set2")
#facet by day

emailnet <- fortify(emailedges, email$nodes, group = "day")
ggplot(data = emailnet, aes(from_id = from, to_id = to_id)) +
  geom_net(aes(colour= CurrentEmploymentType), linewidth=0.5, fiteach=TRUE) +
  scale_colour_brewer(palette="Set2") +
  facet_wrap(~day, nrow=2) + theme(legend.position="bottom")
ggplot(data = emailnet, aes(from_id = from, to_id = to_id)) +
  geom_net(aes(colour= CitizenshipCountry), linewidth=0.5, fiteach=TRUE) +
  scale_colour_brewer(palette="Set2") +
  facet_wrap(~day, nrow=2) + theme(legend.position="bottom")
ggplot(data = emailnet, aes(from_id = from, to_id = to_id)) +
  geom_net(aes(colour= CurrentEmploymentType), linewidth=0.5, fiteach=FALSE) +
  scale_colour_brewer(palette="Set2") +
  facet_wrap(~day, nrow=2) + theme(legend.position="bottom")

## Les Miserables example
data(lesmis)
lesmisnet <- fortify(as.edgedf(lesmis$edges), lesmis$vertices[, c(2,1)])
p <- ggplot(data=lesmisnet, aes(from_id=from_id, to_id=to_id))
p + geom_net(layout.alg="fruchtermanreingold")
p + geom_net(layout.alg="fruchtermanreingold", labelon=TRUE, vjust=-0.5)
p + geom_net(layout.alg="fruchtermanreingold", labelon=TRUE, vjust=-0.5,
    aes(linewidth=degree/5))

## College Football Games in the Fall 2000 regular season
# Source: http://www-/personal.umich.edu/~mejn/netdata/
data(football)
ftnet <- fortify(as.edgedf(football$edges), football$vertices)
p <- ggplot(data=ftnet, aes(from_id=from_id, to_id=to_id))
p + geom_net(aes(colour=value), linewidth=0.75, size=4.5, ecolour="grey80") +
  scale_colour_brewer("Conference", palette="Paired") + theme_net() +
  theme(legend.position="bottom")
  
## End(Not run)

geomnet documentation built on May 29, 2017, 9:10 a.m.