-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-The ggnetwork
package provides a way to build network plots with ggplot2
.
-
-
Install the stable version from CRAN:
-
install.packages("ggnetwork")
-
Or use devtools
to install the latest version of the package from GitHub:
-
devtools::install_github("briatte/ggnetwork")
-
The package is meant to be used with ggplot2
version 2.0.0 or above, so make sure that you update your version of ggplot2
from CRAN before using ggnetwork
:
-
install.packages("ggplot2")
-library(ggplot2)
-
ggnetwork
further requires the network
and sna
packages for network manipulation, and will also install the ggrepel
package for repulsive label drawing.
-
The ggnetwork
package is very much related to the development of geom_net
by Samantha C. Tyner and Heike Hoffmann. It also shares some similarity to the ggnet
and ggnet2
functions, which are part of the GGally
package by Barret Schloerke and others. Each of these projects are extensions to Hadley Wickham’s implementation of Leland Wilkinson’s “grammar of graphics” in ggplot2.
-
-
Minimal example
-
Let’s define a small random graph to illustrate each component of ggnetwork
:
-
library(network)
-library(sna)
-n <- network(rgraph(10, tprob = 0.2), directed = FALSE)
-
Let’s now add categorical and continuous attributes for both edges and vertices. We’ll start with nodes, adding a categorical vertex attribute called "family"
, which is set to either "a"
, "b"
or "c"
, and a continuous vertex attribute called "importance"
, which is set to either 1, 2 or 3.
-
n %v% "family" <- sample(letters[1:3], 10, replace = TRUE)
-n %v% "importance" <- sample(1:3, 10, replace = TRUE)
-
We now add a categorical edge attribute called "type"
, which is set to either "x"
, "y"
or "z"
, and a continuous vertex attribute called "day"
, which is set to either 1, 2 or 3.
-
e <- network.edgecount(n)
-set.edge.attribute(n, "type", sample(letters[24:26], e, replace = TRUE))
-set.edge.attribute(n, "day", sample(1:3, e, replace = TRUE))
-
Last, note that ggnetwork
contains a “blank” plot theme that will avoid plotting axes on the sides of the network. We will use that theme in most of the plots:
-
theme_blank
-
## function(base_size = 12, base_family = "", ...) {
-## ggplot2::theme_bw(base_size = base_size, base_family = base_family) +
-## ggplot2::theme(
-## axis.text = ggplot2::element_blank(),
-## axis.ticks = ggplot2::element_blank(),
-## axis.title = ggplot2::element_blank(),
-## legend.key = ggplot2::element_blank(),
-## panel.background = ggplot2::element_rect(fill = "white", colour = NA),
-## panel.border = ggplot2::element_blank(),
-## panel.grid = ggplot2::element_blank(),
-## ...
-## )
-## }
-## <environment: namespace:ggnetwork>
-
-
-
Main building blocks
-
-
ggnetwork
-
The ggnetwork
package is organised around a ‘workhorse’ function of the same name, which will ‘flatten’ the network object to a data frame that contains the edge list of the network, along with the edge attributes and the vertex attributes of the sender nodes.
-
The network object referred to above might be an object of class network
, or any data structure that can be coerced to it, such as an edge list, an adjacency matrix or an incidence matrix. If the intergraph
package is installed, then objects of class igraph
can also be used with the ggnetwork
package.
-
The data frame returned by ggnetwork
also contains the coordinates needed for node placement as columns "x"
, "y"
, "xend"
and "yend"
, which as a consequence are “reserved” names in the context of ggnetwork
. If these names show up in the edge or the vertex attributes, the function will simply fail to work.
-
The default node placement algorithm used by ggnetwork
to produce these coordinates is the Fruchterman-Reingold force-directed layout algorithm. All of the placement algorithms implemented in the sna
package are available through ggnetwork
, which also accepts additional layout parameters:
-
ggnetwork(n, layout = "fruchtermanreingold", cell.jitter = 0.75)
-ggnetwork(n, layout = "target", niter = 100)
-
The layout
argument will also accept user-submitted coordinates as a two-column matrix with as many rows as the number of nodes in the network.
-
The top of the data frame produced by ggnetwork
contains self-loops to force every node to be included in the plot. This explains why the rows shown below have the same values in "x"
and "xend"
(and in "y"
and "yend"
), and only missing values in the columns corresponding to the edge attributes:
-
head(ggnetwork(n))
-
## x y family importance na.x vertex.names xend
-## 1 0.1830104 0.42047953 c 2 FALSE 1 0.1830104
-## 2 0.4690597 0.00000000 b 2 FALSE 2 0.4690597
-## 3 1.0000000 0.25891124 c 3 FALSE 3 1.0000000
-## 4 0.2814689 1.00000000 c 3 FALSE 4 0.2814689
-## 5 0.0000000 0.50312529 a 2 FALSE 5 0.0000000
-## 6 0.9306397 0.02183163 c 1 FALSE 6 0.9306397
-## yend day na.y type
-## 1 0.42047953 NA NA <NA>
-## 2 0.00000000 NA NA <NA>
-## 3 0.25891124 NA NA <NA>
-## 4 1.00000000 NA NA <NA>
-## 5 0.50312529 NA NA <NA>
-## 6 0.02183163 NA NA <NA>
-
The next rows of the data frame contain the actual edges:
-
tail(ggnetwork(n))
-
## x y family importance na.x vertex.names xend
-## 18 0.8748765 0.2445744 a 2 FALSE 5 0.4203488
-## 19 0.8748765 0.2445744 a 2 FALSE 5 0.4989169
-## 20 1.0000000 0.7239847 c 2 FALSE 10 0.8748765
-## 21 1.0000000 0.7239847 c 2 FALSE 10 0.7160749
-## 22 1.0000000 0.7239847 c 2 FALSE 10 0.7091942
-## 23 1.0000000 0.7239847 c 2 FALSE 10 0.4939615
-## yend day na.y type
-## 18 0.5530424 2 FALSE z
-## 19 0.0000000 2 FALSE y
-## 20 0.2445744 2 FALSE x
-## 21 0.6762465 3 FALSE y
-## 22 0.4029760 1 FALSE z
-## 23 0.8738652 1 FALSE x
-
The data frame returned by ggnetwork
has (N + E) rows, where N is the number of nodes of the network, and E its number of edges. This data format is very likely to include duplicate information about the nodes, which is unavoidable.
-
Note that ggnetwork
does not include any safety mechanism against duplicate column names. As a consequence, if there is both a vertex attribute called "na"
and an edge attribute called "na"
, as in the example above, then the vertex attribute will be renamed "na.x"
and the edge attribute will be renamed "na.y"
.
-
-
-
fortify.network
and fortify.igraph
-
The ‘flattening’ process described above is implemented by ggnetwork
as fortify
methods that are recognised by ggplot2
. As a result, ggplot2
will understand the following syntax as long as n
is an object of class network
or of class igraph
:
-
ggplot(n)
-
However, if the object n
is a matrix or an edge list to be coerced to a network object, you are required to use the ggnetwork
function to pass the object to ggplot2
:
-
ggplot(ggnetwork(n))
-
-
-
geom_edges
-
Let’s now draw the network edges using geom_edges
, which is just a lightly hacked version of geom_segment
. In the example below, we map the type
edge attribute to the linetype of the network edges:
-
ggplot(n, aes(x = x, y = y, xend = xend, yend = yend)) +
- geom_edges(aes(linetype = type), color = "grey50") +
- theme_blank()
-
-
The other aesthetics that we mapped are the basic coordinates of the network plot. These might also be set as part of the call to geom_segment
, but setting them at the root of the plot avoids having to repeat them in additional geoms.
-
Note that geom_edges
can also produce curved edges by setting its curvature
argument to any value above 0
(the default):
-
ggplot(n, aes(x = x, y = y, xend = xend, yend = yend)) +
- geom_edges(aes(linetype = type), color = "grey50", curvature = 0.1) +
- theme_blank()
-
-
-
-
geom_nodes
-
Let’s now draw the nodes using geom_nodes
, which is just a lightly hacked version of geom_point
. In the example below, we map the family
vertex attribute to the color of the nodes, and make the size of these nodes proportional to the importance
vertx attribute:
-
ggplot(n, aes(x = x, y = y, xend = xend, yend = yend)) +
- geom_edges(aes(linetype = type), color = "grey50") +
- geom_nodes(aes(color = family, size = importance)) +
- theme_blank()
-
-
Because ggplot2
follows Wilkinson’s grammar of graphics, it accepts only one color scale. In the example above, that scale is mapped to a vertex attribute, but it could have also been mapped to an edge attribute. Mapping a color to both a vertex attribute and an edge attribute will create a single color scale that incorrectly merges both attributes into one:
-
ggplot(n, aes(x = x, y = y, xend = xend, yend = yend)) +
- geom_edges(aes(color = type)) +
- geom_nodes(aes(color = family)) +
- theme_blank()
-
This is a limitation of ggnetwork
that would require violating some fundamental aspects of the grammar of graphics to be circumvented.
-
-
-
-
More building blocks
-
-
geom_nodetext
-
Let’s now add node labels. These are simply plotted over the nodes by the nodetext
geom, which works exactly like geom_text
. In the example below, we map the vertex.names
attribute (which contains numbers 1 to 10) to uppercase letters:
-
ggplot(n, aes(x = x, y = y, xend = xend, yend = yend)) +
- geom_edges(color = "black") +
- geom_nodes(color = "black", size = 8) +
- geom_nodetext(aes(color = family, label = LETTERS[ vertex.names ]),
- fontface = "bold") +
- theme_blank()
-
-
-
-
geom_nodelabel
-
If you prefer to use the geom_label
geom recently introduced in ggplot2
, ggnetwork
also supports these through the nodelabel
geom:
-
ggplot(n, aes(x = x, y = y, xend = xend, yend = yend)) +
- geom_edges(color = "black") +
- geom_nodelabel(aes(color = family, label = LETTERS[ vertex.names ]),
- fontface = "bold") +
- theme_blank()
-
-
-
-
geom_nodetext_repel
and geom_nodelabel_repel
-
ggnetwork
supports the repulsive label functions introduced by the ggrepel
package, which allows to label nodes with non-overlapping annotations. Simply add _repel
to either geom_nodetext
or geom_nodelabel
to use that functionality:
-
ggplot(n, aes(x = x, y = y, xend = xend, yend = yend)) +
- geom_edges(color = "black") +
- geom_nodelabel_repel(aes(color = family, label = LETTERS[ vertex.names ]),
- fontface = "bold", box.padding = unit(1, "lines")) +
- geom_nodes(color = "black", size = 8) +
- theme_blank()
-
-
-
-
geom_edgetext
and geom_edgelabel
-
Let’s now add edge labels. These are plotted at mid-distance of the nodes that the edges connect by the edgetext
geom, which works exactly like geom_label
, except that its default arguments do not draw a border around the labels. Here’s an example where we map the day
edge attribute to edge labels:
-
ggplot(n, aes(x = x, y = y, xend = xend, yend = yend)) +
- geom_edges(aes(linetype = type), color = "grey75") +
- geom_nodes(color = "gold", size = 8) +
- geom_nodetext(aes(label = LETTERS[ vertex.names ])) +
- geom_edgetext(aes(label = day), color = "white", fill = "grey25") +
- theme_minimal() +
- theme(axis.text = element_blank(),
- axis.title = element_blank(),
- panel.background = element_rect(fill = "grey25"),
- panel.grid = element_blank())
-
-
The edgelabel
geom is just an alias of the edgetext
geom. Note that these geoms are unlikely to produce adequate results if the edges produced by geom_edges
are curved.
-
-
-
geom_edgetext_repel
and geom_edgelabel_repel
-
As you would do with nodes, simply add _repel
to either geom_edgetext
or geom_edgelabel
to draw repulsive edge labels:
-
ggplot(n, aes(x = x, y = y, xend = xend, yend = yend)) +
- geom_edges(aes(linetype = type), color = "grey75") +
- geom_nodes(color = "gold", size = 8) +
- geom_nodetext(aes(label = LETTERS[ vertex.names ])) +
- geom_edgetext_repel(aes(label = day), color = "white", fill = "grey25",
- box.padding = unit(1, "lines")) +
- theme_minimal() +
- theme(axis.text = element_blank(),
- axis.title = element_blank(),
- panel.background = element_rect(fill = "grey25"),
- panel.grid = element_blank())
-
-
-
-
-
More plotting parameters
-
This section presents some rather experimental features of ggnetwork
.
-
-
Edge arrows
-
ggnetwork
uses code by Heike Hoffmann to better show arrows in directed graphs. To illustrate this, we will need a directed graph example, so let’s use the first of the seven emon
networks bundled in the network
package:
-
data(emon)
-emon[[1]]
-
## Network attributes:
-## vertices = 14
-## directed = TRUE
-## hyper = FALSE
-## loops = FALSE
-## multiple = FALSE
-## total edges= 83
-## missing edges= 0
-## non-missing edges= 83
-##
-## Vertex attribute names:
-## Command.Rank.Score Decision.Rank.Score Formalization Location Paid.Staff Sponsorship vertex.names Volunteer.Staff
-##
-## Edge attribute names:
-## Frequency
-
If this network is passed to ggnetwork
without any further plotting parameter, the result will feature “shortened” edges that do not reach their receiver nodes:
-
-
This is because directed networks are expected to be plotted with edge arrows indicating the directedness of each edge. Adding edge arrows with geom_edges
works through the same call to the arrow
function that is supported by geom_segment
:
-
ggplot(emon[[1]], aes(x = x, y = y, xend = xend, yend = yend)) +
- geom_edges(arrow = arrow(length = unit(6, "pt"), type = "closed")) +
- geom_nodes(color = "tomato", size = 4) +
- theme_blank()
-
-
The slightly shortened edges avoid overplotting the edge arrows and the nodes. The amount of “edge shortening” can be set through the arrow.gap
parameter of ggnetwork
, which defaults to 0
when the network is undirected and 0.025
when it is. This parameter might need adjustment depending on the size of the nodes, and it will probably not manage to avoid any overplotting when the size of the nodes is not constant.
-
-
-
Edge weights
-
ggnetwork
can use an edge attribute as edge weights when computing the network layout. The name of that edge attribute should be passed to the weights
argument for that to happen, as in this example, which will produce different layouts than if weights
had been left set to NULL
(the default):
-
ggnetwork(emon[[1]], weights = "Frequency")
-
The Kamada-Kawai is one example of a network layout that supports edge weights. The user should refer to the documentation of each network layout to understand which of these can make use of edge weights.
-
If ggnetwork
finds duplicated edges in a network, it will return a warning, as these edges should probably have been converted to single weighted edges for adequate plotting.
-
-
-
Node faceting
-
In order for ggnetwork
to operate correctly with faceted plots, the by
argument, which is NULL
by default, can be set to the name of an edge attribute. The result will be a longer data frame that can be plotted with either facet_wrap
or facet_grid
, as in the example below, where the faceting variable, the Frequency
edge attribute, has to be specified twice (once to ggnetwork
, once to facet_wrap
):
-
ggplot(ggnetwork(emon[[1]], arrow.gap = 0.04, by = "Frequency"),
- aes(x = x, y = y, xend = xend, yend = yend)) +
- geom_edges(arrow = arrow(length = unit(6, "pt"), type = "closed"),
- aes(color = Sponsorship)) +
- geom_nodes(aes(color = Sponsorship), size = 4) +
- facet_wrap(~ Frequency) +
- theme_facet()
-
-
The by
argument is basically an attempt to bring minimal support for temporal networks in ggnetwork
. It will systematically show all nodes in all plot facets, using the same coordinates in every facet. For more advanced plots of dynamic networks, the user should turn to the ndtv
and tsna
packages.
-
The example above also shows how to use a vertex attribute as part of the aesthetics of the edges. Given how ggnetwork
operates, these vertex attributes will always be those of the sender node.
-
Last, the example also shows that ggnetwork
comes with a theme called theme_facet
. This theme is a variation of the previously mentioned theme_blank
that preserves its facet boxes:
-
theme_facet
-
## function(base_size = 12, base_family = "", ...) {
-## theme_blank(base_size = base_size, base_family = base_family) +
-## ggplot2::theme(
-## panel.border = ggplot2::element_rect(fill = NA, color = "grey50"),
-## ...
-## )
-## }
-## <environment: namespace:ggnetwork>
-
-
-
-
Additional methods
-
Since ggnetwork
works entirely through ggplot2
, all ggplot2
methods apply:
-
ggplot(n, aes(x = x, y = y, xend = xend, yend = yend)) +
- geom_edges(aes(linetype = type), color = "grey50") +
- geom_nodes(aes(x, y, color = family, size = 1.5 * importance)) +
- geom_nodetext(aes(label = LETTERS[ vertex.names ], size = 0.5 * importance)) +
- geom_edgetext(aes(label = day), color = "grey25") +
- scale_color_brewer(palette = "Set2") +
- scale_size_area("importance", breaks = 1:3, max_size = 9) +
- theme_blank()
-
-
Similarly, it is possible to use any of the geometries more than once per plot:
-
ggplot(n, aes(x = x, y = y, xend = xend, yend = yend)) +
- geom_edges(color = "grey50", alpha = 0.5) +
- geom_nodes(aes(x, y, color = family, size = 5.5 * importance), alpha = 0.5) +
- geom_nodes(aes(x, y, color = family, size = 1.5 * importance)) +
- scale_color_brewer(palette = "Set1") +
- guides(size = FALSE) +
- theme_blank()
-
-
Last, all geoms provided by ggnetwork
can be subsetted through the data
argument, just as any ggplot2
geom, and as in the example below, which draws only a subset of all node labels:
-
ggplot(n, aes(x = x, y = y, xend = xend, yend = yend)) +
- geom_edges(color = "grey50", alpha = 0.5) +
- geom_nodes(aes(x, y, color = family), size = 3) +
- geom_nodelabel_repel(aes(label = vertex.names),
- box.padding = unit(1, "lines"),
- data = function(x) { x[ x$family == "a", ]}) +
- scale_color_brewer(palette = "Set1") +
- theme_blank()
-
-
-
Last printed on Mar 25, 2016, using ggnetwork version 0.4.
-
-
-
-
-
-
-
-
-
-
-
-
-
-
diff --git a/man/fortify.Rd b/man/fortify.Rd
new file mode 100644
index 0000000..e07fce8
--- /dev/null
+++ b/man/fortify.Rd
@@ -0,0 +1,9 @@
+% Generated by roxygen2: do not edit by hand
+% Please edit documentation in R/utilities.R
+\name{fortify}
+\alias{fortify}
+\title{fortify generic}
+\description{
+See \code{ggplot2::\link[ggplot2]{fortify}} for details.
+}
+\keyword{internal}
diff --git a/man/fortify.igraph.Rd b/man/fortify.igraph.Rd
index b6c8bb9..07d0768 100644
--- a/man/fortify.igraph.Rd
+++ b/man/fortify.igraph.Rd
@@ -5,15 +5,16 @@
\title{Fortify method for networks of class \code{\link[igraph:igraph-package]{igraph}}}
\usage{
\method{fortify}{igraph}(model, data = NULL, layout = igraph::nicely(),
- arrow.gap = ifelse(igraph::is.directed(x), 0.025, 0), by = NULL, ...)
+ arrow.gap = ifelse(igraph::is.directed(model), 0.025, 0), by = NULL,
+ ...)
}
\arguments{
\item{model}{an object of class \code{\link[igraph:igraph-package]{igraph}}}
\item{data}{not used by this method.}
-\item{layout}{a function call to an
-\code{\link[igraph:igraph-package]{igraph}} layout function, such as
+\item{layout}{a function call to an
+\code{\link[igraph:igraph-package]{igraph}} layout function, such as
\code{\link[igraph]{layout_nicely}} (the default), or a 2 column matrix
giving the x and y coordinates for the vertices.
See \code{\link[igraph]{layout_}} for details.}
@@ -27,8 +28,7 @@ good results when the size of the nodes is reasonably small.}
\item{by}{a character vector that matches an edge attribute, which will be
used to generate a data frame that can be plotted with}
-\item{...}{additional parameters for the \code{\link[igraph]{layout_}}
-function}
+\item{...}{additional parameters for the \code{\link[igraph]{layout_}} function}
}
\description{
Fortify method for networks of class \code{\link[igraph:igraph-package]{igraph}}
diff --git a/man/fortify.network.Rd b/man/fortify.network.Rd
index 23f703d..2355aa4 100644
--- a/man/fortify.network.Rd
+++ b/man/fortify.network.Rd
@@ -80,39 +80,50 @@ if (require(ggplot2) && require(network)) {
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)) +
+ 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")) +
+ 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")) +
+ 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)) +
+ 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)) +
+ 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()
-
}
+
}
diff --git a/man/geom_edges.Rd b/man/geom_edges.Rd
index fc4c5c8..6003956 100644
--- a/man/geom_edges.Rd
+++ b/man/geom_edges.Rd
@@ -78,60 +78,70 @@ edges produced by \code{geom_edges} will be curved.
\examples{
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()
-
+ # 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()
}
+
}
diff --git a/man/geom_edgetext.Rd b/man/geom_edgetext.Rd
index b183729..ad54032 100644
--- a/man/geom_edgetext.Rd
+++ b/man/geom_edgetext.Rd
@@ -7,14 +7,14 @@
\usage{
geom_edgetext(mapping = NULL, data = NULL, position = "identity",
parse = FALSE, ..., nudge_x = 0, nudge_y = 0,
- label.padding = unit(0.25, "lines"), label.r = ggplot2::unit(0.15,
- "lines"), label.size = 0, na.rm = FALSE, show.legend = NA,
+ 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 = ggplot2::unit(0.15,
- "lines"), label.size = 0, na.rm = FALSE, show.legend = NA,
+ label.padding = unit(0.25, "lines"), label.r = unit(0.15, "lines"),
+ label.size = 0, na.rm = FALSE, show.legend = NA,
inherit.aes = TRUE)
}
\arguments{
@@ -83,28 +83,29 @@ identical results.
}
\examples{
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()
-
+ 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()
}
+
}
diff --git a/man/geom_edgetext_repel.Rd b/man/geom_edgetext_repel.Rd
index 68de4e3..051539f 100644
--- a/man/geom_edgetext_repel.Rd
+++ b/man/geom_edgetext_repel.Rd
@@ -101,31 +101,36 @@ All arguments to both \code{\link{geom_edgetext_repel}} and
}
\examples{
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()
-
+ 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()
}
+
}
diff --git a/man/geom_nodes.Rd b/man/geom_nodes.Rd
index 0c4e0b7..5c3dff9 100644
--- a/man/geom_nodes.Rd
+++ b/man/geom_nodes.Rd
@@ -56,36 +56,35 @@ All arguments to this geom are identical to those of
}
\examples{
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")
-
+ 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")
}
+
}
diff --git a/man/geom_nodetext.Rd b/man/geom_nodetext.Rd
index 01e2bec..b5fa7b2 100644
--- a/man/geom_nodetext.Rd
+++ b/man/geom_nodetext.Rd
@@ -82,58 +82,60 @@ All arguments to these geoms are identical to those of
## 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()
-
+ 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()
+ 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()
}
}
diff --git a/man/geom_nodetext_repel.Rd b/man/geom_nodetext_repel.Rd
index f36c5ab..a7cd8eb 100644
--- a/man/geom_nodetext_repel.Rd
+++ b/man/geom_nodetext_repel.Rd
@@ -100,42 +100,45 @@ All arguments to these geoms are identical to those of
## 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()
-
+ 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()
-
+ 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()
}
+
}
diff --git a/man/scale_safely.Rd b/man/scale_safely.Rd
index f6c31fe..27dfe3b 100644
--- a/man/scale_safely.Rd
+++ b/man/scale_safely.Rd
@@ -2,7 +2,7 @@
% Please edit documentation in R/utilities.R
\name{scale_safely}
\alias{scale_safely}
-\title{Rescale `x` to (0, 1), except if `x` is constant}
+\title{Rescale x to (0, 1), except if x is constant}
\usage{
scale_safely(x, scale = diff(range(x)))
}
@@ -13,9 +13,10 @@ scale_safely(x, scale = diff(range(x)))
}
\value{
The rescaled vector, coerced to a vector if necessary.
+If the original vector was constant, all of its values are replaced by 0.5.
}
\description{
-Discussed in PR #32.
+Discussed in PR #32: https://github.com/briatte/ggnetwork/pull/32
}
\author{
Kipp Johnson
diff --git a/man/unit.Rd b/man/unit.Rd
new file mode 100644
index 0000000..5f07b6e
--- /dev/null
+++ b/man/unit.Rd
@@ -0,0 +1,9 @@
+% Generated by roxygen2: do not edit by hand
+% Please edit documentation in R/utilities.R
+\name{unit}
+\alias{unit}
+\title{unit}
+\description{
+See \code{ggplot2::\link[ggplot2]{unit}} for details.
+}
+\keyword{internal}
diff --git a/tests/testthat/test-fortify.R b/tests/testthat/test-fortify.R
index 456711c..092d70f 100644
--- a/tests/testthat/test-fortify.R
+++ b/tests/testthat/test-fortify.R
@@ -1,38 +1,40 @@
-context("Test ggnetwork")
+test_that("fortify.network", {
+ utils::data(flo, package = "network")
+ n <- network::network(flo, directed = FALSE)
-data(flo, package = "network")
-
-library(network)
-library(sna)
-
-test_that("fortify.network works", {
-
- n <- network(flo, directed = FALSE)
-
- expect_is(fortify(n), "data.frame")
+ expect_s3_class(fortify(n), "data.frame")
expect_true(all(c("x", "y", "xend", "yend") %in% names(fortify(n))))
- ggplot(n, aes(x, y, xend = xend, yend = yend)) +
- geom_nodes() +
- geom_edges() +
- geom_nodetext(aes(label = vertex.names)) +
- geom_edgetext(aes(label = 1)) +
- theme_blank()
-
+ expect_s3_class({
+ ggplot2::ggplot(n, ggplot2::aes(x, y, xend = xend, yend = yend)) +
+ geom_nodes() +
+ geom_edges() +
+ geom_nodetext(aes(label = vertex.names)) +
+ geom_edgetext(aes(label = 1)) +
+ theme_blank()
+ }, class = "ggplot")
+
+ expect_s3_class({
+ utils::data(emon, package = "network")
+ ggplot2::ggplot(emon[[1]], ggplot2::aes(x = x, y = y, xend = xend, yend = yend)) +
+ geom_edges() +
+ geom_nodes(color = "tomato", size = 4) +
+ theme_blank()
+ }, class = "ggplot")
})
-test_that("fortify.igraph works", {
-
+test_that("fortify.igraph", {
n <- igraph::graph_from_adjacency_matrix(flo, mode = "undirected")
- expect_is(fortify(n), "data.frame")
+ expect_s3_class(fortify(n), "data.frame")
expect_true(all(c("x", "y", "xend", "yend") %in% names(fortify(n))))
- ggplot(n, aes(x, y, xend = xend, yend = yend)) +
- geom_edges() +
- geom_nodes() +
- geom_nodetext(aes(label = vertex.names)) +
- geom_edgetext(aes(label = 1)) +
- theme_blank()
-
+ expect_s3_class({
+ ggplot2::ggplot(n, ggplot2::aes(x, y, xend = xend, yend = yend)) +
+ geom_edges() +
+ geom_nodes() +
+ geom_nodetext(aes(label = name)) +
+ geom_edgetext(aes(label = 1)) +
+ theme_blank()
+ }, class = "ggplot")
})
diff --git a/tests/testthat/test-geoms.R b/tests/testthat/test-geoms.R
index 137454e..76a20d2 100644
--- a/tests/testthat/test-geoms.R
+++ b/tests/testthat/test-geoms.R
@@ -3,109 +3,126 @@ context("Test all geoms")
data(emon, package = "network")
test_that("geom_nodes works", {
+ expect_s3_class({
+ ggplot(emon[[1]], aes(x, y)) +
+ geom_nodes()
+ }, class = "ggplot")
- ggplot(emon[[1]], aes(x, y)) +
- geom_nodes()
-
- ggplot(emon[[1]], aes(x, y, xend = xend, yend = yend)) +
- geom_nodes()
-
+ expect_s3_class({
+ ggplot(emon[[1]], aes(x, y, xend = xend, yend = yend)) +
+ geom_nodes()
+ }, class = "ggplot")
})
test_that("geom_nodetext works", {
+ expect_s3_class({
+ ggplot(emon[[1]], aes(x, y, xend = xend, yend = yend)) +
+ geom_nodetext(aes(label = vertex.names))
+ }, class = "ggplot")
- ggplot(emon[[1]], aes(x, y, xend = xend, yend = yend)) +
- geom_nodetext(aes(label = vertex.names))
-
- ggplot(emon[[1]], aes(x, y, xend = xend, yend = yend)) +
- geom_nodetext(aes(label = Paid.Staff), nudge_x = 1, nudge_y = 1)
+ expect_s3_class({
+ ggplot(emon[[1]], aes(x, y, xend = xend, yend = yend)) +
+ geom_nodetext(aes(label = Paid.Staff), nudge_x = 1, nudge_y = 1)
+ }, class = "ggplot")
expect_error(
ggplot(emon[[1]], aes(x, y, xend = xend, yend = yend)) +
- geom_nodetext(aes(label = Paid.Staff), nudge_x = 1, nudge_y = 1,
- position = "identity"),
+ geom_nodetext(aes(label = Paid.Staff),
+ nudge_x = 1, nudge_y = 1,
+ position = "identity"
+ ),
"Specify either"
)
-
})
test_that("geom_nodetext_repel works", {
-
- ggplot(emon[[1]], aes(x, y, xend = xend, yend = yend)) +
- geom_nodetext_repel(aes(label = vertex.names))
-
+ expect_s3_class({
+ ggplot(emon[[1]], aes(x, y, xend = xend, yend = yend)) +
+ geom_nodetext_repel(aes(label = vertex.names))
+ }, class = "ggplot")
})
test_that("geom_nodelabel works", {
+ expect_s3_class({
+ ggplot(emon[[1]], aes(x, y, xend = xend, yend = yend)) +
+ geom_nodelabel(aes(label = vertex.names))
+ }, class = "ggplot")
- ggplot(emon[[1]], aes(x, y, xend = xend, yend = yend)) +
- geom_nodelabel(aes(label = vertex.names))
-
- ggplot(emon[[1]], aes(x, y, xend = xend, yend = yend)) +
- geom_nodelabel(aes(label = Paid.Staff), nudge_x = 1, nudge_y = 1)
+ expect_s3_class({
+ ggplot(emon[[1]], aes(x, y, xend = xend, yend = yend)) +
+ geom_nodelabel(aes(label = Paid.Staff), nudge_x = 1, nudge_y = 1)
+ }, class = "ggplot")
expect_error(
ggplot(emon[[1]], aes(x, y, xend = xend, yend = yend)) +
- geom_nodelabel(aes(label = Paid.Staff), nudge_x = 1, nudge_y = 1,
- position = "identity"),
+ geom_nodelabel(aes(label = Paid.Staff),
+ nudge_x = 1, nudge_y = 1,
+ position = "identity"
+ ),
"Specify either"
)
-
})
test_that("geom_nodelabel_repel works", {
-
- ggplot(emon[[1]], aes(x, y, xend = xend, yend = yend)) +
- geom_nodelabel_repel(aes(label = vertex.names))
-
+ expect_s3_class({
+ ggplot(emon[[1]], aes(x, y, xend = xend, yend = yend)) +
+ geom_nodelabel_repel(aes(label = vertex.names))
+ }, class = "ggplot")
})
test_that("geom_edges works", {
# straight
- ggplot(emon[[1]], aes(x, y, xend = xend, yend = yend)) +
- geom_edges()
+ expect_s3_class({
+ ggplot(emon[[1]], aes(x, y, xend = xend, yend = yend)) +
+ geom_edges()
+ }, class = "ggplot")
# curved
- ggplot(emon[[1]], aes(x, y, xend = xend, yend = yend)) +
- geom_edges(curvature = 0.1)
-
+ expect_s3_class({
+ ggplot(emon[[1]], aes(x, y, xend = xend, yend = yend)) +
+ geom_edges(curvature = 0.1)
+ }, class = "ggplot")
})
test_that("geom_edgetext works", {
+ expect_s3_class({
+ ggplot2::ggplot(emon[[1]], ggplot2::aes(x, y, xend = xend, yend = yend)) +
+ geom_edgetext(aes(label = Frequency))
+ }, class = "ggplot")
- ggplot(emon[[1]], aes(x, y, xend = xend, yend = yend)) +
- geom_edgetext(aes(label = Frequency))
-
- ggplot(emon[[1]], aes(x, y, xend = xend, yend = yend)) +
- geom_edgetext(aes(label = Frequency), nudge_x = 1, nudge_y = 1)
+ expect_s3_class({
+ ggplot2::ggplot(emon[[1]], ggplot2::aes(x, y, xend = xend, yend = yend)) +
+ geom_edgetext(aes(label = Frequency), nudge_x = 1, nudge_y = 1)
+ }, class = "ggplot")
expect_error(
- ggplot(emon[[1]], aes(x, y, xend = xend, yend = yend)) +
- geom_edgetext(aes(label = Frequency), nudge_x = 1, nudge_y = 1,
- position = "identity"),
+ ggplot2::ggplot(emon[[1]], ggplot2::aes(x, y, xend = xend, yend = yend)) +
+ geom_edgetext(aes(label = Frequency),
+ nudge_x = 1, nudge_y = 1,
+ position = "identity"
+ ),
"Specify either"
)
-
})
test_that("geom_edgelabel works", {
-
- ggplot(emon[[1]], aes(x, y, xend = xend, yend = yend)) +
- geom_edgelabel(aes(label = Frequency))
-
+ expect_s3_class({
+ ggplot2::ggplot(emon[[1]], ggplot2::aes(x, y, xend = xend, yend = yend)) +
+ geom_edgelabel(aes(label = Frequency))
+ }, class = "ggplot")
})
test_that("geom_edgetext_repel works", {
-
- ggplot(emon[[1]], aes(x, y, xend = xend, yend = yend)) +
- geom_edgetext_repel(aes(label = Frequency))
-
+ expect_s3_class({
+ ggplot2::ggplot(emon[[1]], ggplot2::aes(x, y, xend = xend, yend = yend)) +
+ geom_edgetext_repel(aes(label = Frequency))
+ }, class = "ggplot")
})
test_that("geom_edgelabel_repel works", {
-
- ggplot(emon[[1]], aes(x, y, xend = xend, yend = yend)) +
- geom_edgelabel_repel(aes(label = Frequency))
-
-})
\ No newline at end of file
+ expect_s3_class({
+ ggplot2::ggplot(emon[[1]], ggplot2::aes(x, y, xend = xend, yend = yend)) +
+ geom_edgelabel_repel(aes(label = Frequency))
+ }, class = "ggplot")
+})
diff --git a/tests/testthat/test-ggnetwork.R b/tests/testthat/test-ggnetwork.R
index 9569a92..8e4aecb 100644
--- a/tests/testthat/test-ggnetwork.R
+++ b/tests/testthat/test-ggnetwork.R
@@ -1,27 +1,29 @@
-context("Test ggnetwork")
-
-data(emon, package = "network")
-
test_that("ggnetwork works", {
-
+ data(emon, package = "network")
expect_error(ggnetwork(-999, "could not coerce"))
expect_error(ggnetwork(emon[[1]], layout = -999, "unsupported layout"))
# facet by edge attribute
- ggnetwork(emon[[1]], arrow.gap = 0.02, by = "Frequency")
+ expect_s3_class({
+ ggnetwork(emon[[1]], arrow.gap = 0.02, by = "Frequency")
+ }, class = "data.frame")
# user-provided layout
- ggnetwork(emon[[1]], layout = matrix(runif(28), ncol = 2))
+ expect_s3_class({
+ ggnetwork(emon[[1]], layout = matrix(runif(28), ncol = 2))
+ }, class = "data.frame")
# edge weights in layout
- ggnetwork(emon[[1]], layout = "kamadakawai", weights = "Frequency")
+ expect_s3_class({
+ ggnetwork(emon[[1]], layout = "kamadakawai", weights = "Frequency")
+ }, class = "data.frame")
# duplicated edges warning
expect_warning(
ggnetwork(rbind(
matrix(c(1:2, 2:1), nrow = 2),
- matrix(c(1:2, 2:1), nrow = 2))),
+ matrix(c(1:2, 2:1), nrow = 2)
+ )),
"duplicated edges"
)
-
})
diff --git a/tests/testthat/test-utilities.R b/tests/testthat/test-utilities.R
index 9fb00fd..3deb10e 100644
--- a/tests/testthat/test-utilities.R
+++ b/tests/testthat/test-utilities.R
@@ -1,19 +1,17 @@
-context("Test utilities")
-
test_that("theme_blank works", {
-
- ggplot(ggnetwork(emon[[1]]), aes(x, y, xend = xend, yend = yend)) +
- geom_edges() +
- geom_nodes() +
- theme_blank()
-
+ expect_s3_class({
+ ggplot2::ggplot(ggnetwork(emon[[1]]), ggplot2::aes(x, y, xend = xend, yend = yend)) +
+ geom_edges() +
+ geom_nodes() +
+ theme_blank()
+ }, class = "ggplot")
})
test_that("theme_facet works", {
-
- ggplot(ggnetwork(emon[[1]]), aes(x, y, xend = xend, yend = yend)) +
- geom_edges() +
- geom_nodes() +
- theme_facet()
-
+ expect_s3_class({
+ ggplot2::ggplot(ggnetwork(emon[[1]]), ggplot2::aes(x, y, xend = xend, yend = yend)) +
+ geom_edges() +
+ geom_nodes() +
+ theme_facet()
+ }, class = "ggplot")
})
diff --git a/vignettes/ggnetwork.Rmd b/vignettes/ggnetwork.Rmd
index 0501193..4874f63 100644
--- a/vignettes/ggnetwork.Rmd
+++ b/vignettes/ggnetwork.Rmd
@@ -25,10 +25,10 @@ Install the stable version from CRAN:
install.packages("ggnetwork")
```
-Or use `devtools` to install the latest version of the package [from GitHub](https://github.com/briatte/ggnetwork):
+Or use `remotes` to install the latest version of the package [from GitHub](https://github.com/briatte/ggnetwork):
```{r, eval=FALSE}
-devtools::install_github("briatte/ggnetwork")
+remotes::install_github("briatte/ggnetwork")
```
The package is meant to be used with `ggplot2` version 2.0.0 or above, so make sure that you update your version of `ggplot2` from CRAN before using `ggnetwork`: