Last data update: 2014.03.03

R: Geom for network visualization within the ggplot2 framework
geom_netR Documentation

Geom for network visualization within the ggplot2 framework

Description

Geom for network visualization within the ggplot2 framework

Usage

geom_net(mapping = NULL, data = NULL, stat = "net",
  position = "identity", show.legend = NA, na.rm = TRUE,
  inherit.aes = TRUE, layout = "kamadakawai", layout.par = list(),
  directed = FALSE, fiteach = FALSE, selfies = FALSE, colour = NULL,
  size = NULL, alpha = 0.25, ecolour = NULL, ealpha = NULL,
  linewidth = NULL, arrow = NULL, arrowgap = 0.01, arrowsize = 1,
  label = FALSE, labelcolour = NULL, labelgeom = "text", repel = FALSE,
  fontsize = NULL, vertices = NULL, ...)

GeomNet

StatNet

stat_net(mapping = NULL, data = NULL, geom = "point",
  position = "identity", show.legend = NA, inherit.aes = TRUE,
  layout = "kamadakawai", layout.par = list(), fiteach = FALSE,
  vertices = NULL, 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

character string of the network stat corresponding to geom_net.

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), removes missing values with a warning. If TRUE silently removes missing values.

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

character value specifying the layout algorithm to use. Defaults to "kamadakawai". See ?gplot.layout in the package sna for other choices.

layout.par

list of parameters detailing algorithmic specs. Default parameters are used initially. See ?gplot.layout in the package sna for other choices.

directed

logical value. Should an arrow be drawn from 'from' to 'to' node?

fiteach

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

selfies

logical value. Should self-references be shown (by drawing a circle adjacent to the corresponding node)? defaults to FALSE.

colour

colour for nodes

size

node size.

alpha

numeric value of alpha blending of vertices.

ecolour

colour for edges.

ealpha

numeric value of alpha blending of edges.

linewidth

width of the edges.

arrow

what kind of arrow should be drawn? See specification of function arrow in grid package

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 value (non-negative). How big should the arrow be drawn? Multiplicative of a pre-specified unit.

label

logical value. Include labels for (all) nodes. labelcolour specifies colour of labels, if they should not be the same as the nodes. labels are taken from the from_id variable, unless a label variable is given.

labelcolour

character of colour for the labels.

labelgeom

character. Which ggplot2 geom to use to draw the labels. Either "text" or "label".

repel

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

fontsize

numeric. If labels are present, changes the size of the label.

vertices

data frame 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 a joint to include this information in the data dataframe

...

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

The geometric object to use display the data

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.

Examples

library(ggplot2)
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 <- merge(blood$edges, blood$vertices, by.x="from", by.y="label", all=TRUE)
p <- ggplot(data = bloodnet, aes(from_id = from, to_id = to))
p + geom_net()
p + geom_net(aes(colour=rho)) + theme_net()
p + geom_net(aes(colour=rho), label=TRUE, vjust = -0.5)
p + geom_net(aes(colour=rho), label=TRUE, vjust=-0.5, labelcolour="black",
             directed=TRUE, curvature=0.2) + theme_net()
p + geom_net(colour = "orange", layout = 'circle', size = 6)
p + geom_net(colour = "orange", layout = 'circle', size = 6, linewidth=.75)
p + geom_net(colour = "orange", layout = 'circle', size = 0, linewidth=.75,
             directed = TRUE)
p + geom_net(aes(size=Predominance, colour=rho, shape=rho, linetype=group_to),
             linewidth=0.75, label =TRUE, labelcolour="black") +
    facet_wrap(~Ethnicity) +
    scale_colour_brewer(palette="Set2")
ggplot(data = blood$edges, aes(from_id = from, to_id = to)) +
  geom_net(colour = "darkred", layout = "circle", label = TRUE, size = 15,
         directed = TRUE, vjust = 0.5, labelcolour = "grey80",
         arrowsize = 1.5, linewidth = 0.5, arrowgap = 0.05,
         selfies = TRUE, ecolour = "grey40") +
  theme_net()

#Madmen Relationships
data(madmen)
MMnet <- merge(madmen$edges, madmen$vertices, by.x="Name1", by.y="label", all=TRUE)
p <- ggplot(data = MMnet, aes(from_id = Name1, to_id = Name2))
p + geom_net(label=TRUE)
p + geom_net(aes(colour=Gender), size=6, linewidth=1, label=TRUE, fontsize=3, labelcolour="black")
p + geom_net(aes(colour=Gender), size=6, linewidth=1, label=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, label=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 = Name1, to_id = Name2))
# alternative labelling: specify label variable.
p + geom_net(aes(colour=Gender, label=Gender), size=6, linewidth=1, fontsize=3,
             labelcolour="black")

## visualizing ggplot2 theme elements
data(theme_elements)
TEnet <- merge(theme_elements$edges, theme_elements$vertices, by.x="parent",
               by.y="name", all=TRUE)
ggplot(data = TEnet, aes(from_id = parent, to_id = child)) +
  geom_net(label=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.

data(email)
employee <- data.frame(expand.grid(
              label=unique(email$nodes$label), day=unique(email$edges$day)))
employee <- merge(employee, email$nodes, by="label")
emailnet <- merge(subset(email$edges, nrecipients < 54), employee,
                  by.x=c("From", "day"), by.y=c("label", "day"), all=TRUE)

#no facets
ggplot(data = emailnet, aes(from_id = From, to_id = to)) +
  geom_net(aes(colour= CurrentEmploymentType), linewidth=0.5) +
  scale_colour_brewer(palette="Set2")

#facet by day
ggplot(data = emailnet, aes(from_id = From, to_id = to)) +
  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)) +
  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)) +
  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 <- merge(lesmis$edges, lesmis$vertices, by.x="from", by.y="label", all=TRUE)
p <- ggplot(data=lesmisnet, aes(from_id=from, to_id=to))
p + geom_net(layout="fruchtermanreingold")
p + geom_net(layout="fruchtermanreingold", label=TRUE, vjust=-0.5)
p + geom_net(layout="fruchtermanreingold", label=TRUE, vjust=-0.5, aes(linewidth=degree/5))

## College Football Games in the Fall 2000 regular season
# Hello world! 
# Source: http://www-personal.umich.edu/~mejn/netdata/
data(football)
ftnet <- merge(football$edges, football$vertices, by.x="from", by.y="label", all=TRUE)
p <- ggplot(data=ftnet, aes(from_id=from, to_id=to))
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")

Results