-
Notifications
You must be signed in to change notification settings - Fork 0
/
scientific_network.Rmd
83 lines (68 loc) · 2.5 KB
/
scientific_network.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
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
---
title: "publication statistics"
output: html_notebook
---
```{r}
library(scholarnetwork)
library(ggplot2)
library(igraph)
d = extractNetwork(id="Led04q0AAAAJ", n=100)
plotNetwork(d$nodes, d$edges, file="network.html", width = 1080,
height = 800, charge = -10)
network <- graph_from_data_frame(d$edges, directed=FALSE)
set.seed(123)
l <- layout.fruchterman.reingold(network, niter=1500) # layout
fc <- walktrap.community(network) # community detection
# node locations
nodes <- data.frame(l); names(nodes) <- c("x", "y")
nodes$cluster <- factor(fc$membership)
nodes$label <- fc$names
nodes$degree <- igraph::degree(network)
# edge locations
edgelist <- get.edgelist(network, names=FALSE)
edges <- data.frame(nodes[edgelist[,1],c("x", "y")], nodes[edgelist[,2],c("x", "y")])
names(edges) <- c("x1", "y1", "x2", "y2")
# and now visualizing it...
p <- ggplot(nodes, aes(x=x, y=y, color=cluster, size = degree))
pq <- p + geom_text(color="black", aes(label=label, size = degree),
show.legend=FALSE) +
# nodes
geom_point(color="grey20", aes(fill=cluster),
shape=21, show.legend=FALSE, alpha=1/2) +
# edges
geom_segment(
aes(x=x1, y=y1, xend=x2, yend=y2, label=NA),
data=edges, size=0.25, color="grey20", alpha=1/5) +
## note that here I add a border to the points
scale_fill_discrete(labels=labels) +
scale_size_continuous(range = c(5, 8)) +
theme(
panel.background = element_rect(fill = "white"),
plot.background = element_rect(fill="white"),
axis.line = element_blank(), axis.text = element_blank(),
axis.ticks = element_blank(),
axis.title = element_blank(), panel.border = element_blank(),
panel.grid.major = element_blank(),
panel.grid.minor = element_blank(),
legend.background = element_rect(colour = F, fill = "black"),
legend.key = element_rect(fill = "black", colour = F),
legend.title = element_text(color="white"),
legend.text = element_text(color="white")
) +
## changing size of points in legend
guides(fill = guide_legend(override.aes = list(size=3)))
pq
```
```{r}
library(ggraph)
# data wrangling
bundle <- graph_from_data_frame(d$edges, directed=TRUE)
ggraph(bundle, layout = 'dendrogram', circular = TRUE) +
geom_edge_diagonal() +
theme_void()
from <- match( d$edges$node1, d$edges$weight)
to <- match( d$edges$node2, d$edges$weight)
ggraph(bundle, layout = 'dendrogram', circular = TRUE) +
geom_conn_bundle(data = get_con(from = from, to = to), alpha=0.2, colour="skyblue", tension = 0) +
theme_void()
```