-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathNetwork Visualization(igraph).Rmd
119 lines (105 loc) · 3.71 KB
/
Network Visualization(igraph).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
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
---
title: "NetworkVisualization(igraph)"
data: April 7th 2017
output:
pdf_document: default
html_document: default
auther: San Wang
---
# Network visualization using igraph library
```{r setup, include=FALSE}
knitr::opts_chunk$set(echo = TRUE)
```
## Read Data
```{r}
rm(list = ls())
graphics.off()
cat("\014")
library(igraph)
# as.is default behaviour is to convert character variables to factors
links <- read.csv('Dataset1-Media-Example-EDGES.csv', as.is = TRUE)
nodes <- read.csv('Dataset1-Media-Example-NODES.csv', as.is = TRUE)
nrow(links); nrow(unique(links[,c("from", "to")]))
links <- aggregate(links[,3], links[,-3], sum)
links <- links[order(links$from, links$to),]
colnames(links)[4] <- "weight"
rownames(links) <- NULL
nodes2 <- read.csv("Dataset2-Media-User-Example-NODES.csv", header=T, as.is=T)
links2 <- read.csv("Dataset2-Media-User-Example-EDGES.csv", header=T, row.names=1)
links2 <- as.matrix(links2)
# D:
# describes the edges of the network.
# Its first two columns are the IDs of the source and the target node for each edge.
# The following columns are edge attributes (weight, type, label, or anything else)
# VERTICES:
# starts with a column of node IDs.
# Any following columns are interpreted as node attributes.
net <- graph_from_data_frame(d=links, vertices=nodes, directed=T)
```
## Developing Graph
### 1st plot:
```{r}
plot(net)
```
### 2nd plot:
```{r}
##### removing loops in the graph #####
net <- simplify(net, remove.multiple = F, remove.loops = T)
# Plot with curved edges (edge.curved=.1) and reduce arrow size:
plot(net, edge.arrow.size=.4, edge.curved=.1)
```
### 3rd plot:
```{r}
# Set edge color to gray, and the node color to orange.
### Replace the vertex label with the node names stored in "media" ###
plot(net, edge.arrow.size=.2, edge.curved=.1,
vertex.color="orange", vertex.frame.color="gray50",
vertex.label=V(net)$media, vertex.label.color="black", vertex.label.cex=.7)
```
### 4th plot:
```{r}
##### Generate colors based on media type: #####
colrs <- c("gray50", "tomato", "gold")
V(net)$color <- colrs[V(net)$media.type]
##### Set node size based on audience size: #####
V(net)$size <- V(net)$audience.size*0.7
##### Set edge width based on weight: #####
E(net)$width <- 1+E(net)$weight/12
{
##### some attribute you can change #####
# The labels are currently node IDs.
# Setting label to NA will render no labels:
V(net)$label.color <- "black"
V(net)$label <- NA
#change arrow size and edge color:
E(net)$arrow.size <- .2
E(net)$edge.color <- "gray80"
}
plot(net, edge.arrow.size=.2, edge.curved=.1,
vertex.frame.color="gray50", vertex.label=V(net)$media,
vertex.label.color="black", vertex.label.cex=.7)
##### add legend #####
legend(x=-1.5, y=-1.1, c("Newspaper","TV","Online News"), pch=21,col="#777777",
pt.bg=colrs,pt.cex=2, cex=.8,bty="n",ncol=1)
```
### 5th plot:
```{r}
##### color the edge based on their source node color #####
# get the starting node for each edge with the ends()
edge.start <- ends(net, es=E(net), names=F)[,1]
edge.col <- V(net)$color[edge.start]
plot(net, edge.arrow.size=.2, edge.curved=.1, edge.color = edge.col,
vertex.frame.color="gray50", vertex.label=V(net)$media,
vertex.label.color="black", vertex.label.cex=.7)
legend(x=-1.5, y=-1.1, c("Newspaper","TV","Online News"), pch=21,col="#777777",
pt.bg=colrs,pt.cex=2, cex=.8,bty="n",ncol=1)
```
```{r}
# Sometimes, especially with semantic networks,
# we may be interested in plotting only the labels of the nodes
plot(net, vertex.shape="none", vertex.label=V(net)$media,
vertex.label.font=2, vertex.label.color="gray40",
vertex.label.cex=.7, edge.color="gray85")
```
Reference: Network Analysis and Visualization with R and igraph
[Katherine Ognyanova, www.kateto.net]