-
Notifications
You must be signed in to change notification settings - Fork 2
/
Copy pathtest_updated_with_community_detection_and_cliques.R
132 lines (94 loc) · 4.36 KB
/
test_updated_with_community_detection_and_cliques.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
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
library(dplyr)
library(readr)
library(igraph)
library(tidytext)
insta <- read_csv("InstaOutputlist.csv")
# not sure what the proper column names should be.
names(insta) <- c("subject", "tag")
unique_subj <- unique(insta$subject)
# There are 225 unique subjects
length(unique_subj)
subj_unigram = insta %>% unnest_tokens(tokens, tag, to_lower = TRUE)
places <- read_csv("http://simplemaps.com/static/demos/resources/us-cities/cities.csv")
place_names = tolower(places$city)
rm(places)
# Because people normally don't put spaces in instagram hash tags.
place_names_no_spaces <- gsub(" ", "", place_names)
subj_tokens <- subj_unigram %>% filter(tokens %in% place_names_no_spaces)
create_adj_list = function(df){
# Input: a dataframe with a column "tokens"
# Output: all possible 2-combinations (sorted) of the unique tokens
unique_tokens = unique(df$tokens)
adj_list = data.frame()
if(length(unique_tokens) >= 2) {
all_combins = t(combn(unique_tokens, 2))
all_combins = t(apply(all_combins, 1, sort))
adj_list = data.frame(all_combins, stringsAsFactors = FALSE)
}
return(adj_list)
}
# create unweighted and weighted adjacency lists
adj_list <- subj_tokens %>% group_by(subject) %>% do(create_adj_list(.))
adj_list_weighted = data.frame(adj_list) %>% group_by(X1, X2) %>% summarise(weight = n())
places_graph <- graph.data.frame(adj_list[, c("X1", "X2")], directed = FALSE)
plot(simplify(places_graph))
place_graph_weighted = graph.data.frame(adj_list_weighted[, c("X1", "X2")], directed = FALSE)
E(place_graph_weighted)$weight = adj_list_weighted$weight
l <- layout_in_circle(place_graph_weighted)
plot(place_graph_weighted, layout = l,
edge.color="#d3d3d3")
##################### community1 ###################################
g=place_graph_weighted
community = edge.betweenness.community(g, directed=F)
community$membership
community2 = fastgreedy.community(g)
community2$membership
set.seed(1) # Since layout.fruchterman.reingold is a random algorithm, set seed can 'fix' the layout
plot(g,
vertex.color = community$membership, vertex.size = log(degree(g) + 1),
mark.groups = by(seq_along(community$membership), community$membership, invisible),
layout=layout.fruchterman.reingold)
set.seed(1)
plot(g,
vertex.color = community2$membership, vertex.size = log(degree(g) + 1),
mark.groups = by(seq_along(community2$membership), community2$membership, invisible),
layout=layout.gem)
#combine the nodes to meta-nodes accroding to communities
comm.graph = contract.vertices(g, community$membership, vertex.attr.comb=list(size="sum", "ignore"))
comm.graph = simplify(comm.graph)
plot(comm.graph)
############################################################################################################
community = edge.betweenness.community(g, directed=F)
community$membership
community2 = fastgreedy.community(g)
community2$membership
set.seed(1) # Since layout.fruchterman.reingold is a random algorithm, set seed can 'fix' the layout
plot(g,
vertex.color = community$membership, vertex.size = log(degree(g) + 1),
mark.groups = by(seq_along(community$membership), community$membership, invisible),
layout=layout.fruchterman.reingold)
set.seed(1)
plot(g,
vertex.color = community2$membership, vertex.size = log(degree(g) + 1),
mark.groups = by(seq_along(community2$membership), community2$membership, invisible),
layout=layout.fruchterman.reingold)
#combine the nodes to meta-nodes accroding to communities
comm.graph = contract.vertices(g, community$membership, vertex.attr.comb=list(size="sum", "ignore"))
comm.graph = simplify(comm.graph)
plot(comm.graph) ##### image 1 ####
################################################### Cliques ################################
cliques(g, min = 6)
sapply(cliques(g, min = 6), length) # clique sizes
largest_cliques(g) # cliques with max number of nodes
vcol <- rep("grey80", vcount(g))
vcol[unlist(largest_cliques(g))] <- "gold"
plot(as.undirected(g), vertex.label=V(g)$name, vertex.color=vcol) ##### image 2 ####
####################### Community Detection ################
ceb <- cluster_edge_betweenness(g)
dendPlot(ceb, mode="hclust") ##### image 3 ####
plot(ceb, g) ##### image 4 ####
#######
V(g)$community <- cfg$membership
colrs <- adjustcolor( c("gray50", "tomato", "gold", "yellowgreen"), alpha=.6)
plot(g, vertex.color=colrs[V(g)$community])
##################