-
Notifications
You must be signed in to change notification settings - Fork 4
/
Regeringsøvelse1.R
96 lines (71 loc) · 3.39 KB
/
Regeringsøvelse1.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
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
##########################
#### Regeringseksemplet
# Først installerer vi de nødvendige pakker
# Rstudio vil bede dig om at vælge repository. Vælg fx. Aalborg og set det som default.
install.packages("igraph")
install.packages("ggplot2")
# Så indlæser vi de nødvendige pakker:
library(igraph)
# Vælg dit working directory
# I mit tilfælde er det:
# setwd("~/My Dropbox/R/interlocks")
# Indsæt dit eget eller arbejd i dit standard bibliotek
# Download datasættet
# Windows
download.file("https://raw.github.com/antongrau/soc.sna/master/edgelist.csv", destfile="edgelist.csv")
# Mac/Linux
download.file("https://raw.github.com/antongrau/soc.sna/master/edgelist.csv", destfile="edgelist.csv", method="curl")
data <- read.csv(file="edgelist.csv", sep=",", fileEncoding="latin1")
# Her fjerner vi tomme rækker i data
navn <- data$NAVN
org <- data$ORG
a <- navn==""
navn <- navn[a==FALSE, drop=TRUE]
org <- org[a==FALSE, drop=TRUE]
ind.org <- data.frame(navn, org)
colnames(ind.org) <- c("navn", "org")
# Først laves Case affiliation matricen:
affiliation <- table(ind.org)
affiliation <- as.matrix(affiliation)
# Så laves adjacency matricen for individer
adjacency.individ <- affiliation%*%t(affiliation)
# Så laves adjacency matricen for organisationer
adjacency.organisation <- t(affiliation)%*%affiliation
# Her slettes vi diagonalen i begge matricer
diag(adjacency.organisation) <- 0
diag(adjacency.individ) <- 0
# Så laver vi et netværksobjekt for individer
graph.individ <- graph.adjacency(adjacency.individ, mode="undirected", weighted=TRUE)
# Nu laver vi et netværksobjekt for organisationer
graph.organisation <- graph.adjacency(adjacency.organisation, mode="undirected", weighted=TRUE)
# Standardplot for individer
plot.igraph(graph.individ, vertex.label=V(graph.individ)$name,layout=layout.fruchterman.reingold, edge.color="black",edge.width=E(graph.individ)$weight)
# Standardplot for organisationer
plot.igraph(graph.organisation, vertex.label=V(graph.organisation)$name,layout=layout.fruchterman.reingold, edge.color="black",edge.width=E(graph.organisation)$weight)
#################################################################
#### Fancy plots! ####
#################################################################
# Først henter vi koden til plot funktionen
# Windows
download.file("https://raw.github.com/antongrau/soc.sna/master/gplot.R", destfile="gplot.R")
# Mac/Linux
download.file("https://raw.github.com/antongrau/soc.sna/master/gplot.R", destfile="gplot.R", method="curl")
source("gplot.R")
library(ggplot2)
# Så laver vi fancy plot for individerne
gplot(graph.individ, vertex.coord=layout.kamada.kawai(graph.individ))
# Et til!
gplot(graph.individ, vertex.coord=layout.kamada.kawai(graph.individ), edge.colour="red", edge.alpha=0.2, text.size=6)
# Fancy plots for organisationerne
gplot(graph.organisation, vertex.coord=layout.kamada.kawai(graph.organisation))
# Et til!
gplot(graph.organisation, vertex.coord=layout.fruchterman.reingold(graph.organisation), edge.colour="blue", edge.alpha=0.2 )
#Lidt mål
sort(closeness(graph.individ), decreasing=TRUE)
sort(degree(graph.individ), decreasing=TRUE)
sort(betweenness(graph.individ), decreasing=TRUE)
sort(evcent(graph.individ), decreasing=TRUE)
evcent(graph.individ)
page.rank(graph.individ)
authority.score(graph.individ)
hub.score(graph.individ)