Title: | Geometries to Plot Networks with 'ggplot2' |
---|---|
Description: | Geometries to plot network objects with 'ggplot2'. |
Authors: | François Briatte [aut, cre] , Michał Bojanowski [ctb] , Mickaël Canouil [ctb] , Zachary Charlop-Powers [ctb] , Jacob C. Fisher [ctb] , Kipp Johnson [ctb] , Tyler Rinker [ctb] |
Maintainer: | François Briatte <[email protected]> |
License: | GPL-3 |
Version: | 0.5.13 |
Built: | 2024-11-22 04:37:15 UTC |
Source: | https://github.com/briatte/ggnetwork |
igraph
Fortify method for networks of class igraph
## S3 method for class 'igraph' fortify( model, data = NULL, layout = igraph::nicely(), arrow.gap = ifelse(igraph::is.directed(model), 0.025, 0), by = NULL, scale = TRUE, stringsAsFactors = getOption("stringsAsFactors", FALSE), ... )
## S3 method for class 'igraph' fortify( model, data = NULL, layout = igraph::nicely(), arrow.gap = ifelse(igraph::is.directed(model), 0.025, 0), by = NULL, scale = TRUE, stringsAsFactors = getOption("stringsAsFactors", FALSE), ... )
model |
an object of class |
data |
not used by this method. |
layout |
a function call to an
|
arrow.gap |
a parameter that will shorten the network edges in order to
avoid overplotting edge arrows and nodes; defaults to |
by |
a character vector that matches an edge attribute, which will be
used to generate a data frame that can be plotted with
|
scale |
whether to (re)scale the layout coordinates. Defaults to
|
stringsAsFactors |
whether vertex and edge attributes should be
converted to factors if they are of class |
... |
additional parameters for the |
a data.frame
object.
network
See the vignette at https://briatte.github.io/ggnetwork/ for a
description of both this function and the rest of the ggnetwork
package.
## S3 method for class 'network' fortify( model, data = NULL, layout = "fruchtermanreingold", weights = NULL, arrow.gap = ifelse(network::is.directed(model), 0.025, 0), by = NULL, scale = TRUE, stringsAsFactors = getOption("stringsAsFactors", FALSE), ... )
## S3 method for class 'network' fortify( model, data = NULL, layout = "fruchtermanreingold", weights = NULL, arrow.gap = ifelse(network::is.directed(model), 0.025, 0), by = NULL, scale = TRUE, stringsAsFactors = getOption("stringsAsFactors", FALSE), ... )
model |
an object of class |
data |
not used by this method. |
layout |
a network layout supplied by |
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 |
arrow.gap |
a parameter that will shorten the network edges in order to
avoid overplotting edge arrows and nodes; defaults to |
by |
a character vector that matches an edge attribute, which will be
used to generate a data frame that can be plotted with
|
scale |
whether to (re)scale the layout coordinates. Defaults to
|
stringsAsFactors |
whether vertex and edge attributes should be
converted to factors if they are of class |
... |
additional parameters for the |
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.
a data.frame
object.
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() }
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() }
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.
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, ... )
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, ... )
mapping |
Set of aesthetic mappings created by |
data |
The data to be displayed in this layer. There are three options: If A A |
position |
Position adjustment, either as a string naming the adjustment
(e.g. |
arrow |
specification for arrow heads, as created by |
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 |
show.legend |
logical. Should this layer be included in the legends?
|
inherit.aes |
If |
... |
Other arguments passed on to |
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, colour = "steelblue") + theme_blank() # with nodes ggplot(n, aes(x, y, xend = xend, yend = yend)) + geom_edges(size = 1, colour = "steelblue") + geom_nodes(size = 3, colour = "steelblue") + theme_blank() # with arrows ggplot(n, aes(x, y, xend = xend, yend = yend)) + geom_edges( size = 1, colour = "steelblue", arrow = arrow(length = unit(0.5, "lines"), type = "closed") ) + geom_nodes(size = 3, colour = "steelblue") + theme_blank() # with curvature ggplot(n, aes(x, y, xend = xend, yend = yend)) + geom_edges( size = 1, colour = "steelblue", curvature = 0.15, arrow = arrow(length = unit(0.5, "lines"), type = "closed") ) + geom_nodes(size = 3, colour = "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, colour = "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(colour = weight), curvature = 0.15, arrow = arrow(length = unit(0.5, "lines"), type = "closed") ) + geom_nodes(size = 3, colour = "grey50") + scale_colour_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(colour = weight), data = positive_weight) + geom_nodes(size = 4, colour = "grey50") + scale_colour_gradient(low = "gold", high = "tomato") + theme_blank() }
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, colour = "steelblue") + theme_blank() # with nodes ggplot(n, aes(x, y, xend = xend, yend = yend)) + geom_edges(size = 1, colour = "steelblue") + geom_nodes(size = 3, colour = "steelblue") + theme_blank() # with arrows ggplot(n, aes(x, y, xend = xend, yend = yend)) + geom_edges( size = 1, colour = "steelblue", arrow = arrow(length = unit(0.5, "lines"), type = "closed") ) + geom_nodes(size = 3, colour = "steelblue") + theme_blank() # with curvature ggplot(n, aes(x, y, xend = xend, yend = yend)) + geom_edges( size = 1, colour = "steelblue", curvature = 0.15, arrow = arrow(length = unit(0.5, "lines"), type = "closed") ) + geom_nodes(size = 3, colour = "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, colour = "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(colour = weight), curvature = 0.15, arrow = arrow(length = unit(0.5, "lines"), type = "closed") ) + geom_nodes(size = 3, colour = "grey50") + scale_colour_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(colour = weight), data = positive_weight) + geom_nodes(size = 4, colour = "grey50") + scale_colour_gradient(low = "gold", high = "tomato") + theme_blank() }
All arguments to both geom_edgetext
and
geom_edgelabel
are identical to those of
geom_label
, with the only difference that the
label.size
argument defaults to 0
in order to avoid drawing a
border around the edge labels. The labels will be drawn at mid-edges.
geom_text
and geom_label
produce strictly
identical results.
geom_edgetext( mapping = NULL, data = NULL, position = "identity", parse = FALSE, ..., nudge_x = 0, nudge_y = 0, label.padding = unit(0.25, "lines"), label.r = unit(0.15, "lines"), label.size = 0, na.rm = FALSE, show.legend = NA, inherit.aes = TRUE ) geom_edgelabel( mapping = NULL, data = NULL, position = "identity", parse = FALSE, ..., nudge_x = 0, nudge_y = 0, label.padding = unit(0.25, "lines"), label.r = unit(0.15, "lines"), label.size = 0, na.rm = FALSE, show.legend = NA, inherit.aes = TRUE )
geom_edgetext( mapping = NULL, data = NULL, position = "identity", parse = FALSE, ..., nudge_x = 0, nudge_y = 0, label.padding = unit(0.25, "lines"), label.r = unit(0.15, "lines"), label.size = 0, na.rm = FALSE, show.legend = NA, inherit.aes = TRUE ) geom_edgelabel( mapping = NULL, data = NULL, position = "identity", parse = FALSE, ..., nudge_x = 0, nudge_y = 0, label.padding = unit(0.25, "lines"), label.r = unit(0.15, "lines"), label.size = 0, na.rm = FALSE, show.legend = NA, inherit.aes = TRUE )
mapping |
Set of aesthetic mappings created by |
data |
The data to be displayed in this layer. There are three options: If A A |
position |
Position adjustment, either as a string, or the result of
a call to a position adjustment function. Cannot be jointly specified with
|
parse |
If |
... |
Other arguments passed on to |
nudge_x , nudge_y
|
Horizontal and vertical adjustment to nudge labels by.
Useful for offsetting text from points, particularly on discrete scales.
Cannot be jointly specified with |
label.padding |
Amount of padding around label. Defaults to 0.25 lines. |
label.r |
Radius of rounded corners. Defaults to 0.15 lines. |
label.size |
Size of label border, in mm. |
na.rm |
If |
show.legend |
logical. Should this layer be included in the legends?
|
inherit.aes |
If |
if (require(network) && require(sna)) { data(flo, package = "network") n <- network(flo, directed = FALSE) # arbitrary categorical edge attribute e <- sample(letters[ 1:4 ], network.edgecount(n), replace = TRUE) set.edge.attribute(n, "type", e) # with labelled edges ggplot(n, aes(x, y, xend = xend, yend = yend)) + geom_edges(aes(colour = type)) + geom_edgetext(aes(label = type, colour = type)) + geom_nodes(size = 4, colour = "grey50") + theme_blank() # label only a subset of all edges with arbitrary symbol edge_type <- function(x) { x[ x$type == "a", ] } ggplot(n, aes(x, y, xend = xend, yend = yend)) + geom_edges() + geom_edgetext(label = "=", data = edge_type) + geom_nodes(size = 4, colour = "grey50") + theme_blank() }
if (require(network) && require(sna)) { data(flo, package = "network") n <- network(flo, directed = FALSE) # arbitrary categorical edge attribute e <- sample(letters[ 1:4 ], network.edgecount(n), replace = TRUE) set.edge.attribute(n, "type", e) # with labelled edges ggplot(n, aes(x, y, xend = xend, yend = yend)) + geom_edges(aes(colour = type)) + geom_edgetext(aes(label = type, colour = type)) + geom_nodes(size = 4, colour = "grey50") + theme_blank() # label only a subset of all edges with arbitrary symbol edge_type <- function(x) { x[ x$type == "a", ] } ggplot(n, aes(x, y, xend = xend, yend = yend)) + geom_edges() + geom_edgetext(label = "=", data = edge_type) + geom_nodes(size = 4, colour = "grey50") + theme_blank() }
All arguments to both geom_edgetext_repel
and
geom_edgelabel_repel
are identical to those of
geom_label_repel
. geom_text_repel
and
geom_label_repel
produce strictly identical results.
geom_edgetext_repel( mapping = NULL, data = NULL, parse = FALSE, ..., box.padding = unit(0.25, "lines"), label.padding = unit(0.25, "lines"), point.padding = unit(1e-06, "lines"), label.r = unit(0.15, "lines"), label.size = 0.25, arrow = NULL, force = 1, max.iter = 10000, nudge_x = 0, nudge_y = 0, na.rm = FALSE, show.legend = NA, inherit.aes = TRUE ) geom_edgelabel_repel( mapping = NULL, data = NULL, parse = FALSE, ..., box.padding = unit(0.25, "lines"), label.padding = unit(0.25, "lines"), point.padding = unit(1e-06, "lines"), label.r = unit(0.15, "lines"), label.size = 0.25, arrow = NULL, force = 1, max.iter = 10000, nudge_x = 0, nudge_y = 0, na.rm = FALSE, show.legend = NA, inherit.aes = TRUE )
geom_edgetext_repel( mapping = NULL, data = NULL, parse = FALSE, ..., box.padding = unit(0.25, "lines"), label.padding = unit(0.25, "lines"), point.padding = unit(1e-06, "lines"), label.r = unit(0.15, "lines"), label.size = 0.25, arrow = NULL, force = 1, max.iter = 10000, nudge_x = 0, nudge_y = 0, na.rm = FALSE, show.legend = NA, inherit.aes = TRUE ) geom_edgelabel_repel( mapping = NULL, data = NULL, parse = FALSE, ..., box.padding = unit(0.25, "lines"), label.padding = unit(0.25, "lines"), point.padding = unit(1e-06, "lines"), label.r = unit(0.15, "lines"), label.size = 0.25, arrow = NULL, force = 1, max.iter = 10000, nudge_x = 0, nudge_y = 0, na.rm = FALSE, show.legend = NA, inherit.aes = TRUE )
mapping |
Set of aesthetic mappings created by |
data |
A data frame. If specified, overrides the default data frame defined at the top level of the plot. |
parse |
If TRUE, the labels will be parsed into expressions and displayed as described in ?plotmath |
... |
other arguments passed on to
|
box.padding |
Amount of padding around bounding box, as unit or number.
Defaults to 0.25. (Default unit is lines, but other units can be specified
by passing |
label.padding |
Amount of padding around label, as unit or number.
Defaults to 0.25. (Default unit is lines, but other units can be specified
by passing |
point.padding |
Amount of padding around labeled point, as unit or
number. Defaults to 0. (Default unit is lines, but other units can be
specified by passing |
label.r |
Radius of rounded corners, as unit or number. Defaults
to 0.15. (Default unit is lines, but other units can be specified by
passing |
label.size |
Size of label border, in mm. |
arrow |
specification for arrow heads, as created by |
force |
Force of repulsion between overlapping text labels. Defaults to 1. |
max.iter |
Maximum number of iterations to try to resolve overlaps. Defaults to 10000. |
nudge_x , nudge_y
|
Horizontal and vertical adjustments to nudge the
starting position of each text label. The units for |
na.rm |
If |
show.legend |
logical. Should this layer be included in the legends?
|
inherit.aes |
If |
if (require(network) && require(sna)) { data(flo, package = "network") n <- network(flo, directed = FALSE) # arbitrary categorical edge attribute e <- sample(1:4, network.edgecount(n), replace = TRUE) set.edge.attribute(n, "day", e) # with repulsive edge labels ggplot(n, aes(x, y, xend = xend, yend = yend)) + geom_edges() + geom_edgetext_repel(aes(label = day), box.padding = unit(0.5, "lines")) + geom_nodes(size = 4, colour = "grey50") + theme_blank() # repulsive edge labels for only a subset of all edges edge_day <- function(x) { x[ x$day > 2, ] } ggplot(n, aes(x, y, xend = xend, yend = yend)) + geom_edges(aes(colour = cut(day, (4:0)[ -3 ]))) + geom_edgetext_repel(aes( label = paste("day", day), colour = cut(day, (4:0)[ -3 ]) ), data = edge_day) + geom_nodes(size = 4, colour = "grey50") + scale_colour_manual("day", labels = c("old ties", "day 3", "day 4"), values = c("grey50", "gold", "tomato") ) + theme_blank() }
if (require(network) && require(sna)) { data(flo, package = "network") n <- network(flo, directed = FALSE) # arbitrary categorical edge attribute e <- sample(1:4, network.edgecount(n), replace = TRUE) set.edge.attribute(n, "day", e) # with repulsive edge labels ggplot(n, aes(x, y, xend = xend, yend = yend)) + geom_edges() + geom_edgetext_repel(aes(label = day), box.padding = unit(0.5, "lines")) + geom_nodes(size = 4, colour = "grey50") + theme_blank() # repulsive edge labels for only a subset of all edges edge_day <- function(x) { x[ x$day > 2, ] } ggplot(n, aes(x, y, xend = xend, yend = yend)) + geom_edges(aes(colour = cut(day, (4:0)[ -3 ]))) + geom_edgetext_repel(aes( label = paste("day", day), colour = cut(day, (4:0)[ -3 ]) ), data = edge_day) + geom_nodes(size = 4, colour = "grey50") + scale_colour_manual("day", labels = c("old ties", "day 3", "day 4"), values = c("grey50", "gold", "tomato") ) + theme_blank() }
All arguments to this geom are identical to those of
geom_point
.
geom_nodes( mapping = NULL, data = NULL, position = "identity", na.rm = FALSE, show.legend = NA, inherit.aes = TRUE, ... )
geom_nodes( mapping = NULL, data = NULL, position = "identity", na.rm = FALSE, show.legend = NA, inherit.aes = TRUE, ... )
mapping |
Set of aesthetic mappings created by |
data |
The data to be displayed in this layer. There are three options: If A A |
position |
Position adjustment, either as a string naming the adjustment
(e.g. |
na.rm |
If |
show.legend |
logical. Should this layer be included in the legends?
|
inherit.aes |
If |
... |
Other arguments passed on to |
if (require(network) && require(sna)) { data(flo, package = "network") n <- network(flo, directed = FALSE) # just nodes ggplot(n, aes(x, y)) + geom_nodes(size = 3, shape = 21, colour = "steelblue") + theme_blank() # with edges ggplot(n, aes(x, y, xend = xend, yend = yend)) + geom_edges(colour = "steelblue") + geom_nodes(size = 3, shape = 21, colour = "steelblue", fill = "white") + theme_blank() # with nodes sized according to degree centrality ggplot(n, aes(x, y, xend = xend, yend = yend)) + geom_edges(colour = "steelblue") + geom_nodes(size = degree(n), shape = 21, colour = "steelblue", fill = "white") + theme_blank() # with nodes colored according to betweenness centrality n %v% "betweenness" <- betweenness(flo) ggplot(n, aes(x, y, xend = xend, yend = yend)) + geom_edges(colour = "grey50") + geom_nodes(aes(colour = betweenness), size = 3) + scale_colour_gradient(low = "gold", high = "tomato") + theme_blank() + theme(legend.position = "bottom") }
if (require(network) && require(sna)) { data(flo, package = "network") n <- network(flo, directed = FALSE) # just nodes ggplot(n, aes(x, y)) + geom_nodes(size = 3, shape = 21, colour = "steelblue") + theme_blank() # with edges ggplot(n, aes(x, y, xend = xend, yend = yend)) + geom_edges(colour = "steelblue") + geom_nodes(size = 3, shape = 21, colour = "steelblue", fill = "white") + theme_blank() # with nodes sized according to degree centrality ggplot(n, aes(x, y, xend = xend, yend = yend)) + geom_edges(colour = "steelblue") + geom_nodes(size = degree(n), shape = 21, colour = "steelblue", fill = "white") + theme_blank() # with nodes colored according to betweenness centrality n %v% "betweenness" <- betweenness(flo) ggplot(n, aes(x, y, xend = xend, yend = yend)) + geom_edges(colour = "grey50") + geom_nodes(aes(colour = betweenness), size = 3) + scale_colour_gradient(low = "gold", high = "tomato") + theme_blank() + theme(legend.position = "bottom") }
All arguments to these geoms are identical to those of
geom_text
and geom_label
.
geom_nodetext( mapping = NULL, data = NULL, position = "identity", ..., parse = FALSE, nudge_x = 0, nudge_y = 0, check_overlap = FALSE, na.rm = FALSE, show.legend = NA, inherit.aes = TRUE ) geom_nodelabel( mapping = NULL, data = NULL, position = "identity", ..., parse = FALSE, nudge_x = 0, nudge_y = 0, label.padding = unit(0.25, "lines"), label.r = unit(0.15, "lines"), label.size = 0.25, na.rm = FALSE, show.legend = NA, inherit.aes = TRUE )
geom_nodetext( mapping = NULL, data = NULL, position = "identity", ..., parse = FALSE, nudge_x = 0, nudge_y = 0, check_overlap = FALSE, na.rm = FALSE, show.legend = NA, inherit.aes = TRUE ) geom_nodelabel( mapping = NULL, data = NULL, position = "identity", ..., parse = FALSE, nudge_x = 0, nudge_y = 0, label.padding = unit(0.25, "lines"), label.r = unit(0.15, "lines"), label.size = 0.25, na.rm = FALSE, show.legend = NA, inherit.aes = TRUE )
mapping |
Set of aesthetic mappings created by |
data |
The data to be displayed in this layer. There are three options: If A A |
position |
Position adjustment, either as a string, or the result of
a call to a position adjustment function. Cannot be jointly specified with
|
... |
Other arguments passed on to |
parse |
If |
nudge_x , nudge_y
|
Horizontal and vertical adjustment to nudge labels by.
Useful for offsetting text from points, particularly on discrete scales.
Cannot be jointly specified with |
check_overlap |
If |
na.rm |
If |
show.legend |
logical. Should this layer be included in the legends?
|
inherit.aes |
If |
label.padding |
Amount of padding around label. Defaults to 0.25 lines. |
label.r |
Radius of rounded corners. Defaults to 0.15 lines. |
label.size |
Size of label border, in mm. |
## geom_nodetext examples if (require(network) && require(sna)) { n <- network(rgraph(10, tprob = 0.2), directed = FALSE) # just node labels ggplot(n, aes(x, y)) + geom_nodetext(aes(label = vertex.names)) + theme_blank() # with nodes underneath ggplot(n, aes(x, y)) + geom_nodes(colour = "gold", size = 9) + geom_nodetext(aes(label = vertex.names)) + theme_blank() # with nodes and edges ggplot(n, aes(x, y, xend = xend, yend = yend)) + geom_edges(colour = "gold") + geom_nodes(colour = "gold", size = 9) + geom_nodetext(aes(label = vertex.names)) + theme_blank() } ## geom_nodelabel examples if (require(network) && require(sna)) { data(flo, package = "network") n <- network(flo, directed = FALSE) # with text labels ggplot(n, aes(x, y, xend = xend, yend = yend)) + geom_edges(colour = "grey50") + geom_nodelabel(aes(label = vertex.names)) + theme_blank() # with text labels coloured according to degree centrality n %v% "degree" <- degree(n) ggplot(n, aes(x, y, xend = xend, yend = yend)) + geom_edges(colour = "grey50") + geom_nodelabel(aes(label = vertex.names, fill = degree)) + scale_fill_gradient(low = "gold", high = "tomato") + theme_blank() # label only a subset of all nodes high_degree <- function(x) { x[ x$degree > median(x$degree), ] } ggplot(n, aes(x, y, xend = xend, yend = yend)) + geom_edges(colour = "steelblue") + geom_nodes(aes(size = degree), colour = "steelblue") + geom_nodelabel(aes(label = vertex.names), data = high_degree, colour = "white", fill = "tomato" ) + theme_blank() }
## geom_nodetext examples if (require(network) && require(sna)) { n <- network(rgraph(10, tprob = 0.2), directed = FALSE) # just node labels ggplot(n, aes(x, y)) + geom_nodetext(aes(label = vertex.names)) + theme_blank() # with nodes underneath ggplot(n, aes(x, y)) + geom_nodes(colour = "gold", size = 9) + geom_nodetext(aes(label = vertex.names)) + theme_blank() # with nodes and edges ggplot(n, aes(x, y, xend = xend, yend = yend)) + geom_edges(colour = "gold") + geom_nodes(colour = "gold", size = 9) + geom_nodetext(aes(label = vertex.names)) + theme_blank() } ## geom_nodelabel examples if (require(network) && require(sna)) { data(flo, package = "network") n <- network(flo, directed = FALSE) # with text labels ggplot(n, aes(x, y, xend = xend, yend = yend)) + geom_edges(colour = "grey50") + geom_nodelabel(aes(label = vertex.names)) + theme_blank() # with text labels coloured according to degree centrality n %v% "degree" <- degree(n) ggplot(n, aes(x, y, xend = xend, yend = yend)) + geom_edges(colour = "grey50") + geom_nodelabel(aes(label = vertex.names, fill = degree)) + scale_fill_gradient(low = "gold", high = "tomato") + theme_blank() # label only a subset of all nodes high_degree <- function(x) { x[ x$degree > median(x$degree), ] } ggplot(n, aes(x, y, xend = xend, yend = yend)) + geom_edges(colour = "steelblue") + geom_nodes(aes(size = degree), colour = "steelblue") + geom_nodelabel(aes(label = vertex.names), data = high_degree, colour = "white", fill = "tomato" ) + theme_blank() }
All arguments to these geoms are identical to those of
geom_text_repel
and
geom_label_repel
.
geom_nodetext_repel( mapping = NULL, data = NULL, parse = FALSE, ..., box.padding = unit(0.25, "lines"), point.padding = unit(1e-06, "lines"), arrow = NULL, force = 1, max.iter = 10000, nudge_x = 0, nudge_y = 0, na.rm = FALSE, show.legend = NA, inherit.aes = TRUE ) geom_nodelabel_repel( mapping = NULL, data = NULL, parse = FALSE, ..., box.padding = unit(0.25, "lines"), label.padding = unit(0.25, "lines"), point.padding = unit(1e-06, "lines"), label.r = unit(0.15, "lines"), label.size = 0.25, arrow = NULL, force = 1, max.iter = 10000, nudge_x = 0, nudge_y = 0, na.rm = FALSE, show.legend = NA, inherit.aes = TRUE )
geom_nodetext_repel( mapping = NULL, data = NULL, parse = FALSE, ..., box.padding = unit(0.25, "lines"), point.padding = unit(1e-06, "lines"), arrow = NULL, force = 1, max.iter = 10000, nudge_x = 0, nudge_y = 0, na.rm = FALSE, show.legend = NA, inherit.aes = TRUE ) geom_nodelabel_repel( mapping = NULL, data = NULL, parse = FALSE, ..., box.padding = unit(0.25, "lines"), label.padding = unit(0.25, "lines"), point.padding = unit(1e-06, "lines"), label.r = unit(0.15, "lines"), label.size = 0.25, arrow = NULL, force = 1, max.iter = 10000, nudge_x = 0, nudge_y = 0, na.rm = FALSE, show.legend = NA, inherit.aes = TRUE )
mapping |
Set of aesthetic mappings created by |
data |
A data frame. If specified, overrides the default data frame defined at the top level of the plot. |
parse |
If TRUE, the labels will be parsed into expressions and displayed as described in ?plotmath |
... |
other arguments passed on to
|
box.padding |
Amount of padding around bounding box, as unit or number.
Defaults to 0.25. (Default unit is lines, but other units can be specified
by passing |
point.padding |
Amount of padding around labeled point, as unit or
number. Defaults to 0. (Default unit is lines, but other units can be
specified by passing |
arrow |
specification for arrow heads, as created by |
force |
Force of repulsion between overlapping text labels. Defaults to 1. |
max.iter |
Maximum number of iterations to try to resolve overlaps. Defaults to 10000. |
nudge_x , nudge_y
|
Horizontal and vertical adjustments to nudge the
starting position of each text label. The units for |
na.rm |
If |
show.legend |
logical. Should this layer be included in the legends?
|
inherit.aes |
If |
label.padding |
Amount of padding around label, as unit or number.
Defaults to 0.25. (Default unit is lines, but other units can be specified
by passing |
label.r |
Radius of rounded corners, as unit or number. Defaults
to 0.15. (Default unit is lines, but other units can be specified by
passing |
label.size |
Size of label border, in mm. |
## geom_nodetext_repel example if (require(network) && require(sna)) { n <- network(rgraph(10, tprob = 0.2), directed = FALSE) ggplot(n, aes(x, y, xend = xend, yend = yend)) + geom_edges(colour = "steelblue") + geom_nodetext_repel(aes(label = paste("node", vertex.names)), box.padding = unit(1, "lines") ) + geom_nodes(colour = "steelblue", size = 3) + theme_blank() } ## geom_nodelabel_repel examples if (require(network) && require(sna)) { data(flo, package = "network") n <- network(flo, directed = FALSE) ggplot(n, aes(x, y, xend = xend, yend = yend)) + geom_edges(colour = "steelblue") + geom_nodelabel_repel(aes(label = vertex.names), box.padding = unit(1, "lines") ) + geom_nodes(colour = "steelblue", size = 3) + theme_blank() # label only a subset of all nodes n %v% "degree" <- degree(n) low_degree <- function(x) { x[ x$degree < median(x$degree), ] } ggplot(n, aes(x, y, xend = xend, yend = yend)) + geom_edges(colour = "steelblue") + geom_nodelabel_repel(aes(label = vertex.names), box.padding = unit(1.5, "lines"), data = low_degree, segment.colour = "tomato", colour = "white", fill = "tomato" ) + geom_nodes(aes(size = degree), colour = "steelblue") + theme_blank() }
## geom_nodetext_repel example if (require(network) && require(sna)) { n <- network(rgraph(10, tprob = 0.2), directed = FALSE) ggplot(n, aes(x, y, xend = xend, yend = yend)) + geom_edges(colour = "steelblue") + geom_nodetext_repel(aes(label = paste("node", vertex.names)), box.padding = unit(1, "lines") ) + geom_nodes(colour = "steelblue", size = 3) + theme_blank() } ## geom_nodelabel_repel examples if (require(network) && require(sna)) { data(flo, package = "network") n <- network(flo, directed = FALSE) ggplot(n, aes(x, y, xend = xend, yend = yend)) + geom_edges(colour = "steelblue") + geom_nodelabel_repel(aes(label = vertex.names), box.padding = unit(1, "lines") ) + geom_nodes(colour = "steelblue", size = 3) + theme_blank() # label only a subset of all nodes n %v% "degree" <- degree(n) low_degree <- function(x) { x[ x$degree < median(x$degree), ] } ggplot(n, aes(x, y, xend = xend, yend = yend)) + geom_edges(colour = "steelblue") + geom_nodelabel_repel(aes(label = vertex.names), box.padding = unit(1.5, "lines"), data = low_degree, segment.colour = "tomato", colour = "white", fill = "tomato" ) + geom_nodes(aes(size = degree), colour = "steelblue") + theme_blank() }
A wrapper for the fortify.network
and
fortify.igraph
functions that will also try to coerce matrices
and data frames to network objects.
ggnetwork(x, ...)
ggnetwork(x, ...)
x |
an object of class |
... |
arguments passed to the |
Discussed in PR #32: https://github.com/briatte/ggnetwork/pull/32
scale_safely(x, scale = diff(range(x)))
scale_safely(x, scale = diff(range(x)))
x |
a vector to rescale |
scale |
the scale on which to rescale the vector |
The rescaled vector, coerced to a vector if necessary. If the original vector was constant, all of its values are replaced by 0.5.
Kipp Johnson
A ggplot2
theme without lines, borders, axis text or titles, suited
for plotting networks.
theme_blank(base_size = 12, base_family = "", ...)
theme_blank(base_size = 12, base_family = "", ...)
base_size |
base font size |
base_family |
base font family |
... |
other |
A variation of theme_blank
that adds a panel border to the
plot, which is often suitable for plotting faceted networks.
theme_facet(base_size = 12, base_family = "", ...)
theme_facet(base_size = 12, base_family = "", ...)
base_size |
base font size |
base_family |
base font family |
... |
other |