-
Notifications
You must be signed in to change notification settings - Fork 5
/
Copy pathggnetwork_edgelabel.R
48 lines (38 loc) · 1.43 KB
/
ggnetwork_edgelabel.R
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
### From ggnetwork vignette:
detach(package:igraph)
detach(package:sna)
detach(package:network)
library(ggplot2) #must be >2.0.0
library(ggnetwork)
library(network)
library(sna)
library(rsvg)
library(svglite)
svglite("plot.svg", width = 11, height = 8)
# Random graph
set.seed(1)
n <- network(rgraph(10, tprob = 0.2), directed = FALSE)
n %v% "family" <- sample(letters[1:3], 10, replace = TRUE)
n %v% "importance" <- sample(1:3, 10, replace = TRUE)
e <- network.edgecount(n) #get total edges
set.edge.attribute(n, "type", sample(letters[24:26], e, replace = TRUE))
set.edge.attribute(n, "day", sample(1:3, e, replace = TRUE))
# add ggrepel style edge labels
ggplot(n, aes(x = x, y = y, xend = xend, yend = yend)) +
geom_edges(aes(linetype = type), color = "grey75", size=1) +
geom_nodes(color = "#ffff4d", size = 12) +
geom_nodetext(aes(label = LETTERS[ vertex.names ]),size=8) +
geom_edgetext_repel(aes(label = day), color = "white", fill = "gray20",
box.padding = unit(1, "lines"),
label.size = .25,
size=6) +
theme_minimal() +
theme(axis.text = element_blank(),
axis.title = element_blank(),
panel.background = element_rect(fill = "gray20", color = "gray20"),
plot.background = element_rect(fill = "gray20"),
panel.grid = element_blank(),
legend.position="none")
dev.off()
png::writePNG(rsvg("plot.svg"), "img/ggnetworkplot1.png")
###