-
Notifications
You must be signed in to change notification settings - Fork 39
/
ggnetwork.Rmd
49 lines (40 loc) · 1.72 KB
/
ggnetwork.Rmd
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
---
title: "ggplot2 extensions: ggnetwork"
---
### ggnetwork
<https://github.com/briatte/ggnetwork>
The ggnetwork package provides a way to build network plots with ggplot2.
```{r, message=FALSE,warning=FALSE}
# Example from https://briatte.github.io/ggnetwork/
library(ggplot2)
library(ggnetwork)
# 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
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))
# Let’s now draw the network edges using geom_edges
# theme_blank
ggplot(data = ggnetwork(n), aes(x = x, y = y, xend = xend, yend = yend)) +
geom_edges(aes(linetype = type), color = "grey50") +
theme_blank()
# Let’s now draw the nodes using geom_nodes
ggplot(data = ggnetwork(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()
# Let’s now add node labels.
ggplot(data = ggnetwork(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()
```