r:social_network_analysis_tutorial
Differences
This shows you the differences between two versions of the page.
Both sides previous revisionPrevious revisionNext revision | Previous revisionNext revisionBoth sides next revision | ||
r:social_network_analysis_tutorial [2019/11/27 07:40] – [Analysis] hkimscil | r:social_network_analysis_tutorial [2019/11/27 14:30] – [Measurements] hkimscil | ||
---|---|---|---|
Line 1: | Line 1: | ||
====== T1. ====== | ====== T1. ====== | ||
+ | '' | ||
===== Dataset ===== | ===== Dataset ===== | ||
<file csv star-wars-network-edges.csv> | <file csv star-wars-network-edges.csv> | ||
Line 320: | Line 321: | ||
</ | </ | ||
===== Vis ===== | ===== Vis ===== | ||
- | |||
- | |||
< | < | ||
plot(g) | plot(g) | ||
Line 332: | Line 331: | ||
</ | </ | ||
{{: | {{: | ||
+ | |||
+ | |||
+ | < | ||
+ | par(mar=c(0, | ||
+ | plot(g, | ||
+ | | ||
+ | | ||
+ | | ||
+ | | ||
+ | | ||
+ | </ | ||
+ | {{: | ||
+ | |||
+ | < | ||
+ | strength(g) | ||
+ | as.data.frame(strength(g)) | ||
+ | </ | ||
+ | |||
+ | < | ||
+ | > strength(g) | ||
+ | R2-D2 | ||
+ | | ||
+ | | ||
+ | | ||
+ | | ||
+ | 1 | ||
+ | GOLD FIVE | ||
+ | 0 | ||
+ | > data.frame(strength(g)) | ||
+ | strength.g. | ||
+ | R2-D2 50 | ||
+ | CHEWBACCA | ||
+ | C-3PO 64 | ||
+ | LUKE 129 | ||
+ | DARTH VADER 11 | ||
+ | CAMIE 4 | ||
+ | BIGGS 14 | ||
+ | LEIA 59 | ||
+ | BERU 9 | ||
+ | OWEN 8 | ||
+ | OBI-WAN | ||
+ | MOTTI 4 | ||
+ | TARKIN | ||
+ | HAN 80 | ||
+ | GREEDO | ||
+ | JABBA 1 | ||
+ | DODONNA | ||
+ | GOLD LEADER | ||
+ | WEDGE 9 | ||
+ | RED LEADER | ||
+ | RED TEN 2 | ||
+ | GOLD FIVE 0 | ||
+ | > | ||
+ | </ | ||
+ | |||
+ | < | ||
+ | V(g)$size <- strength(g) | ||
+ | par(mar=c(0, | ||
+ | plot(g) | ||
+ | </ | ||
+ | {{: | ||
+ | |||
+ | |||
+ | < | ||
+ | # taking the log to improve it | ||
+ | V(g)$size <- log(strength(g)) * 4 + 3 | ||
+ | par(mar=c(0, | ||
+ | plot(g) | ||
+ | </ | ||
+ | {{: | ||
+ | |||
+ | < | ||
+ | plot(g, | ||
+ | | ||
+ | | ||
+ | | ||
+ | | ||
+ | | ||
+ | </ | ||
+ | {{: | ||
+ | |||
+ | < | ||
+ | V(g)$label <- ifelse( strength(g)> | ||
+ | par(mar=c(0, | ||
+ | plot(g, | ||
+ | | ||
+ | | ||
+ | | ||
+ | | ||
+ | | ||
+ | </ | ||
+ | {{: | ||
+ | |||
+ | < | ||
+ | t.n | ||
+ | </ | ||
+ | |||
+ | ===== Using ifelse ===== | ||
+ | < | ||
+ | t.n | ||
+ | </ | ||
+ | |||
+ | < | ||
+ | > t.n | ||
+ | name id | ||
+ | 1 R2-D2 0 | ||
+ | 2 CHEWBACCA | ||
+ | 3 C-3PO 2 | ||
+ | 4 | ||
+ | 5 DARTH VADER 4 | ||
+ | 6 CAMIE 5 | ||
+ | 7 BIGGS 6 | ||
+ | 8 | ||
+ | 9 | ||
+ | 10 OWEN 9 | ||
+ | 11 | ||
+ | 12 MOTTI 11 | ||
+ | 13 TARKIN 12 | ||
+ | 14 HAN 13 | ||
+ | 15 GREEDO 14 | ||
+ | 16 JABBA 15 | ||
+ | 17 | ||
+ | 18 GOLD LEADER 17 | ||
+ | 19 WEDGE 18 | ||
+ | 20 RED LEADER 19 | ||
+ | 21 RED TEN 20 | ||
+ | 22 GOLD FIVE 21 | ||
+ | > | ||
+ | </ | ||
+ | |||
+ | < | ||
+ | t.n$name==" | ||
+ | </ | ||
+ | |||
+ | < | ||
+ | > t.n$name==" | ||
+ | | ||
+ | [13] FALSE FALSE FALSE FALSE FALSE FALSE FALSE FALSE FALSE FALSE | ||
+ | > | ||
+ | </ | ||
+ | |||
+ | < | ||
+ | as.data.frame(t.n$name==" | ||
+ | </ | ||
+ | |||
+ | < | ||
+ | > as.data.frame(t.n$name==" | ||
+ | | ||
+ | 1 TRUE | ||
+ | 2 FALSE | ||
+ | 3 FALSE | ||
+ | 4 FALSE | ||
+ | 5 FALSE | ||
+ | 6 FALSE | ||
+ | 7 FALSE | ||
+ | 8 FALSE | ||
+ | 9 FALSE | ||
+ | 10 FALSE | ||
+ | 11 FALSE | ||
+ | 12 FALSE | ||
+ | 13 FALSE | ||
+ | 14 FALSE | ||
+ | 15 FALSE | ||
+ | 16 FALSE | ||
+ | 17 FALSE | ||
+ | 18 FALSE | ||
+ | 19 FALSE | ||
+ | 20 FALSE | ||
+ | 21 FALSE | ||
+ | 22 FALSE | ||
+ | > | ||
+ | </ | ||
+ | |||
+ | |||
+ | < | ||
+ | tmp.a <- as.data.frame(t.n) | ||
+ | tmp.b <- as.data.frame(t.n$name==" | ||
+ | tmp <- data.frame(tmp.a, | ||
+ | tmp | ||
+ | </ | ||
+ | |||
+ | < | ||
+ | > tmp.a <- as.data.frame(t.n) | ||
+ | > tmp.b <- as.data.frame(t.n$name==" | ||
+ | > tmp <- data.frame(tmp.a, | ||
+ | > tmp | ||
+ | name id t.n.name.....R2.D2. | ||
+ | 1 R2-D2 0 TRUE | ||
+ | 2 CHEWBACCA | ||
+ | 3 C-3PO 2 FALSE | ||
+ | 4 | ||
+ | 5 DARTH VADER 4 FALSE | ||
+ | 6 CAMIE 5 FALSE | ||
+ | 7 BIGGS 6 FALSE | ||
+ | 8 | ||
+ | 9 | ||
+ | 10 OWEN 9 FALSE | ||
+ | 11 | ||
+ | 12 MOTTI 11 FALSE | ||
+ | 13 TARKIN 12 FALSE | ||
+ | 14 HAN 13 FALSE | ||
+ | 15 GREEDO 14 FALSE | ||
+ | 16 JABBA 15 FALSE | ||
+ | 17 | ||
+ | 18 GOLD LEADER 17 FALSE | ||
+ | 19 WEDGE 18 FALSE | ||
+ | 20 RED LEADER 19 FALSE | ||
+ | 21 RED TEN 20 FALSE | ||
+ | 22 GOLD FIVE 21 FALSE | ||
+ | </ | ||
+ | |||
+ | |||
+ | < | ||
+ | ifelse(t.n$name==" | ||
+ | </ | ||
+ | |||
+ | < | ||
+ | > ifelse(t.n$name==" | ||
+ | [1] " | ||
+ | [13] " | ||
+ | > | ||
+ | </ | ||
+ | |||
+ | < | ||
+ | ifelse(grepl(" | ||
+ | </ | ||
+ | |||
+ | < | ||
+ | > ifelse(grepl(" | ||
+ | [1] " | ||
+ | [13] " | ||
+ | > | ||
+ | </ | ||
+ | |||
+ | < | ||
+ | tmp.a <- data.frame(t.n$name) | ||
+ | tmp.b <- data.frame(ifelse(grepl(" | ||
+ | data.frame(tmp.a, | ||
+ | </ | ||
+ | |||
+ | < | ||
+ | > tmp.a <- data.frame(t.n$name) | ||
+ | > tmp.b <- data.frame(ifelse(grepl(" | ||
+ | > data.frame(tmp.a, | ||
+ | t.n.name ifelse.grepl..R...t.n.name....yes....no.. | ||
+ | 1 R2-D2 yes | ||
+ | 2 CHEWBACCA | ||
+ | 3 C-3PO no | ||
+ | 4 | ||
+ | 5 DARTH VADER yes | ||
+ | 6 CAMIE no | ||
+ | 7 BIGGS no | ||
+ | 8 | ||
+ | 9 | ||
+ | 10 OWEN no | ||
+ | 11 | ||
+ | 12 | ||
+ | 13 TARKIN | ||
+ | 14 | ||
+ | 15 GREEDO | ||
+ | 16 | ||
+ | 17 | ||
+ | 18 GOLD LEADER | ||
+ | 19 | ||
+ | 20 RED LEADER | ||
+ | 21 RED TEN yes | ||
+ | 22 GOLD FIVE no | ||
+ | > | ||
+ | </ | ||
+ | ===== Coloring nodes ===== | ||
+ | < | ||
+ | dark_side <- c(" | ||
+ | light_side <- c(" | ||
+ | " | ||
+ | "GOLD LEADER", | ||
+ | other <- c(" | ||
+ | # node we'll create a new color variable as a node property | ||
+ | V(g)$color <- NA | ||
+ | V(g)$color[V(g)$name %in% dark_side] <- " | ||
+ | V(g)$color[V(g)$name %in% light_side] <- " | ||
+ | V(g)$color[V(g)$name %in% other] <- " | ||
+ | vertex_attr(g) | ||
+ | </ | ||
+ | |||
+ | < | ||
+ | > dark_side <- c(" | ||
+ | > light_side <- c(" | ||
+ | + " | ||
+ | + " | ||
+ | > other <- c(" | ||
+ | > # node we'll create a new color variable as a node property | ||
+ | > V(g)$color <- NA | ||
+ | > V(g)$color[V(g)$name %in% dark_side] <- " | ||
+ | > V(g)$color[V(g)$name %in% light_side] <- " | ||
+ | > V(g)$color[V(g)$name %in% other] <- " | ||
+ | > vertex_attr(g) | ||
+ | $name | ||
+ | [1] " | ||
+ | [6] " | ||
+ | [11] " | ||
+ | [16] " | ||
+ | [21] "RED TEN" | ||
+ | |||
+ | $id | ||
+ | | ||
+ | |||
+ | $size | ||
+ | [1] 18.648092 19.572539 19.635532 22.439250 12.591581 | ||
+ | [8] 19.310150 11.788898 11.317766 18.567281 | ||
+ | [15] 3.000000 | ||
+ | [22] -Inf | ||
+ | |||
+ | $label | ||
+ | [1] " | ||
+ | [6] NA " | ||
+ | [11] " | ||
+ | [16] NA NA NA NA "RED LEADER" | ||
+ | [21] NA NA | ||
+ | |||
+ | $color | ||
+ | [1] " | ||
+ | [9] " | ||
+ | [17] " | ||
+ | |||
+ | > | ||
+ | </ | ||
+ | |||
+ | < | ||
+ | par(mar=c(0, | ||
+ | plot(g) | ||
+ | </ | ||
+ | {{: | ||
+ | |||
+ | ===== %in% ===== | ||
+ | |||
+ | < | ||
+ | # what does %in% do? | ||
+ | 1 %in% c(1,2,3,4) | ||
+ | 1 %in% c(2,3,4) | ||
+ | </ | ||
+ | |||
+ | < | ||
+ | > 1 %in% c(1,2,3,4) | ||
+ | [1] TRUE | ||
+ | > 1 %in% c(2,3,4) | ||
+ | [1] FALSE | ||
+ | > | ||
+ | </ | ||
+ | |||
+ | < | ||
+ | dark_side <- c(" | ||
+ | light_side <- c(" | ||
+ | " | ||
+ | "GOLD LEADER", | ||
+ | other <- c(" | ||
+ | # node we'll create a new color variable as a node property | ||
+ | V(g)$color <- NA # V(g)에 color라는 컬럼을 만든다 | ||
+ | V(g)$color[V(g)$name %in% dark_side] <- " | ||
+ | # V(g)$name이 dark_side에 있는 이름이면 color 컬럼에 " | ||
+ | V(g)$color[V(g)$name %in% light_side] <- " | ||
+ | V(g)$color[V(g)$name %in% other] <- " | ||
+ | vertex_attr(g) | ||
+ | </ | ||
+ | |||
+ | < | ||
+ | par(mar=c(0, | ||
+ | plot(g) | ||
+ | legend(x=.75, | ||
+ | | ||
+ | </ | ||
+ | {{: | ||
+ | |||
+ | ===== Edges ===== | ||
+ | < | ||
+ | E(g)$width <- log(E(g)$weight) + 1.5 # create width column in the graph, g | ||
+ | edge_attr(g) | ||
+ | </ | ||
+ | < | ||
+ | > E(g)$width <- log(E(g)$weight) + 1.5 # create width column in the graph, g | ||
+ | > edge_attr(g) | ||
+ | $weight | ||
+ | [1] 17 13 6 5 5 3 1 7 5 16 19 11 1 1 2 2 | ||
+ | [17] 4 1 3 3 2 3 18 2 6 17 1 19 6 1 2 1 | ||
+ | [33] 7 9 26 1 1 6 1 1 13 1 1 1 1 1 1 2 | ||
+ | [49] 1 1 3 3 1 1 3 1 2 1 1 1 | ||
+ | |||
+ | $width | ||
+ | [1] 4.333213 4.064949 3.291759 3.109438 3.109438 | ||
+ | [6] 2.598612 1.500000 3.445910 3.109438 4.272589 | ||
+ | [11] 4.444439 3.897895 1.500000 1.500000 2.193147 | ||
+ | [16] 2.193147 2.886294 1.500000 2.598612 2.598612 | ||
+ | [21] 2.193147 2.598612 4.390372 2.193147 3.291759 | ||
+ | [26] 4.333213 1.500000 4.444439 3.291759 1.500000 | ||
+ | [31] 2.193147 1.500000 3.445910 3.697225 4.758097 | ||
+ | [36] 1.500000 1.500000 3.291759 1.500000 1.500000 | ||
+ | [41] 4.064949 1.500000 1.500000 1.500000 1.500000 | ||
+ | [46] 1.500000 1.500000 2.193147 1.500000 1.500000 | ||
+ | [51] 2.598612 2.598612 1.500000 1.500000 2.598612 | ||
+ | [56] 1.500000 2.193147 1.500000 1.500000 1.500000 | ||
+ | </ | ||
+ | |||
+ | < | ||
+ | par(mar=c(0, | ||
+ | plot(g) | ||
+ | </ | ||
+ | {{: | ||
+ | |||
+ | < | ||
+ | par(mfrow=c(2, | ||
+ | plot(g, layout=layout_randomly, | ||
+ | plot(g, layout=layout_in_circle, | ||
+ | plot(g, layout=layout_as_star, | ||
+ | plot(g, layout=layout_as_tree, | ||
+ | plot(g, layout=layout_on_grid, | ||
+ | plot(g, layout=layout_with_fr, | ||
+ | par(mfrow=c(1, | ||
+ | </ | ||
+ | {{: | ||
+ | |||
+ | |||
+ | < | ||
+ | par(mfrow=c(1, | ||
+ | set.seed(777) | ||
+ | fr <- layout_with_fr(g, | ||
+ | par(mar=c(0, | ||
+ | set.seed(666) | ||
+ | fr <- layout_with_fr(g, | ||
+ | par(mar=c(0, | ||
+ | |||
+ | </ | ||
+ | {{: | ||
+ | |||
+ | |||
+ | ===== Measurements ===== | ||
+ | < | ||
+ | degree(g, mode = " | ||
+ | closeness(g, | ||
+ | betweenness(g, | ||
+ | </ | ||
+ | |||
+ | < | ||
+ | > degree(g, mode = " | ||
+ | R2-D2 | ||
+ | 7 | ||
+ | | ||
+ | | ||
+ | | ||
+ | 1 | ||
+ | GOLD FIVE | ||
+ | 0 | ||
+ | > closeness(g, | ||
+ | R2-D2 | ||
+ | | ||
+ | | ||
+ | | ||
+ | | ||
+ | | ||
+ | GOLD FIVE | ||
+ | | ||
+ | Warning message: | ||
+ | In closeness(g, | ||
+ | At centrality.c: | ||
+ | > betweenness(g, | ||
+ | R2-D2 | ||
+ | 0.011904762 0.035487528 0.057596372 0.297278912 0.011904762 0.000000000 0.032142857 | ||
+ | | ||
+ | 0.217120181 0.005442177 0.000000000 0.014852608 0.000000000 0.000000000 0.176190476 | ||
+ | | ||
+ | 0.000000000 0.000000000 0.011111111 0.003174603 0.003174603 0.032142857 0.000000000 | ||
+ | GOLD FIVE | ||
+ | 0.000000000 | ||
+ | > | ||
+ | </ | ||
+ | |||
+ | < | ||
+ | g.c.deg <- degree(g, mode = " | ||
+ | g.c.clo <- closeness(g, | ||
+ | g.c.bet <- betweenness(g, | ||
+ | tmp <- data.frame(g.c.deg, | ||
+ | tmp[order(-g.c.clo, | ||
+ | </ | ||
+ | |||
+ | < | ||
+ | > g.c.deg <- degree(g, mode = " | ||
+ | > g.c.clo <- closeness(g, | ||
+ | Warning message: | ||
+ | In closeness(g, | ||
+ | At centrality.c: | ||
+ | > g.c.bet <- betweenness(g, | ||
+ | > tmp <- data.frame(g.c.deg, | ||
+ | > | ||
+ | > tmp | ||
+ | g.c.deg | ||
+ | R2-D2 7 0.38181818 0.011904762 | ||
+ | CHEWBACCA | ||
+ | C-3PO 10 0.40384615 0.057596372 | ||
+ | LUKE 15 0.44680851 0.297278912 | ||
+ | DARTH VADER 5 0.32812500 0.011904762 | ||
+ | CAMIE 2 0.32307692 0.000000000 | ||
+ | BIGGS 7 0.36842105 0.032142857 | ||
+ | LEIA 12 0.42000000 0.217120181 | ||
+ | BERU 4 0.35000000 0.005442177 | ||
+ | OWEN 3 0.32812500 0.000000000 | ||
+ | OBI-WAN | ||
+ | MOTTI 3 0.31343284 0.000000000 | ||
+ | TARKIN | ||
+ | HAN 8 0.38888889 0.176190476 | ||
+ | GREEDO | ||
+ | JABBA 1 0.28767123 0.000000000 | ||
+ | DODONNA | ||
+ | GOLD LEADER | ||
+ | WEDGE 5 0.33870968 0.003174603 | ||
+ | RED LEADER | ||
+ | RED TEN 2 0.32307692 0.000000000 | ||
+ | GOLD FIVE 0 0.04545455 0.000000000 | ||
+ | > | ||
+ | </ | ||
+ | |||
+ | < | ||
+ | g.c.bac <- distances(g, | ||
+ | g.c.bac | ||
+ | </ | ||
+ | |||
+ | < | ||
+ | > g.c.bac <- distances(g, | ||
+ | > g.c.bac | ||
+ | R2-D2 CHEWBACCA C-3PO LUKE DARTH VADER CAMIE BIGGS LEIA BERU OWEN OBI-WAN MOTTI | ||
+ | R2-D2 | ||
+ | TARKIN HAN GREEDO JABBA DODONNA GOLD LEADER WEDGE RED LEADER RED TEN GOLD FIVE | ||
+ | R2-D2 2 | ||
+ | </ | ||
+ |
r/social_network_analysis_tutorial.txt · Last modified: 2023/11/23 08:50 by hkimscil