-
Notifications
You must be signed in to change notification settings - Fork 50
/
Copy path2015-12-31-network-graph.Rmd
90 lines (74 loc) · 1.76 KB
/
2015-12-31-network-graph.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
83
84
85
86
87
88
89
90
---
description: How to make network graphs in R with Plotly.
display_as: scientific
language: r
layout: base
name: Network Graph
order: 4
output:
html_document:
keep_md: true
page_type: example_index
permalink: r/network-graphs/
thumbnail: thumbnail/net.jpg
---
```{r, echo = FALSE, message=FALSE}
knitr::opts_chunk$set(message = FALSE, warning=FALSE)
```
### Read Graph File
We are using the well-known social network of `Zachary's karate club`. GML format file can be collected from [here](https://gist.github.com/pravj/9168fe52823c1702a07b).
```{r, results = 'hide'}
library(plotly)
library(igraph)
library(igraphdata)
data(karate, package="igraphdata")
G <- upgrade_graph(karate)
L <- layout.circle(G)
```
### Create Vertices and Edges
```{r, results = 'hide'}
vs <- V(G)
es <- as.data.frame(get.edgelist(G))
Nv <- length(vs)
Ne <- length(es[1]$V1)
```
### Create Nodes
```{r, results = 'hide'}
library(plotly)
Xn <- L[,1]
Yn <- L[,2]
names(Xn)<-vs$name
names(Yn)<-vs$name
network <- plot_ly(x = ~Xn, y = ~Yn, mode = "markers", text = vs$label, hoverinfo = "text")
```
### Creates Edges
```{r, results = 'hide'}
edge_shapes <- list()
for(i in 1:Ne) {
v0 <- es[i,]$V1
v1 <- es[i,]$V2
edge_shape = list(
type = "line",
line = list(color = "#030303", width = 0.3),
x0 = Xn[v0],
y0 = Yn[v0],
x1 = Xn[v1],
y1 = Yn[v1]
)
edge_shapes[[i]] <- edge_shape
}
```
### Create Network
```{r}
axis <- list(title = "", showgrid = FALSE, showticklabels = FALSE, zeroline = FALSE)
fig <- layout(
network,
title = 'Karate Network',
shapes = edge_shapes,
xaxis = axis,
yaxis = axis
)
fig
```
### Reference
See [https://plotly.com/python/reference/#scatter](https://plotly.com/python/reference/#scatter) for more information and chart attribute options!