问题
I am trying to figure out how to easily access and manipulate graphs created in R.
If I start with the following data. I create a graph, run some graph clustering and then plot the first cluster:
#libraries
library(igraph)
library(igraphdata)
data(karate)
#cluster
cfg <- cluster_fast_greedy(karate)
plot(cfg, karate)
cfg
IGRAPH clustering fast greedy, groups: 3, mod: 0.43
+ groups:
$`1`
[1] "Actor 9" "Actor 10" "Actor 15" "Actor 16" "Actor 19" "Actor 21" "Actor 23" "Actor 24" "Actor 25" "Actor 26" "Actor 27"
[12] "Actor 28" "Actor 29" "Actor 30" "Actor 31" "Actor 32" "Actor 33" "John A"
$`2`
[1] "Mr Hi" "Actor 2" "Actor 3" "Actor 4" "Actor 8" "Actor 12" "Actor 13" "Actor 14" "Actor 18" "Actor 20" "Actor 22"
$`3`
[1] "Actor 5" "Actor 6" "Actor 7" "Actor 11" "Actor 17"
#make a plot of the first community
a = induced_subgraph(karate, cfg[[1]])
plot(a)
#biggest graph https://stackoverflow.com/questions/15103744/r-igraph-how-to-find-the-largest-community
x <- which.max(sizes(cfg))
subg <- induced.subgraph(karate, which(membership(cfg) == x))
User G5W showed how to make a table that contains the size of each cluster:
my_table = table(cfg$membership)
I also figured out how to "condense" (contract, shrink) all observations into their corresponding communities, and then make a plot.
contracted <- simplify(contract(karate,membership(cfg)))
plot(contracted)
There appears to be two "lines" connecting the three clusters together :
Does anyone know if this line "really means anything"? Is this line naturally occurring? On what basis does this line connecting these 3 clusters?
I simulated my own network data, ran graph clustering, contracted the results by cluster and then created a plot
library(igraph)
library(dplyr)
library(visNetwork)
set.seed(1234)
#create file from which to sample from
x5 <- sample(1:10000, 10000, replace=T)
#convert to data frame
x5 = as.data.frame(x5)
#create first file (take a random sample from the created file)
a = sample_n(x5, 9000)
#create second file (take a random sample from the created file)
b = sample_n(x5, 9000)
#combine
c = cbind(a,b)
#create dataframe
c = data.frame(c)
#rename column names
colnames(c) <- c("a","b")
#create graph
graph <- graph.data.frame(c, directed=F)
graph <- simplify(graph)
cfg <- cluster_fast_greedy(graph)
#contract clusters
contracted <- simplify(contract(graph, membership(cfg), vertex.attr.comb=toString))
#visnetwork plot
visIgraph(contracted) %>% visOptions (highlightNearest = TRUE) %>% visIgraphLayout(layout = "layout_with_fr") %>%
visOptions(highlightNearest = TRUE, nodesIdSelection = TRUE) %>%
visInteraction(navigationButtons = TRUE)
#without visnetwork
plot(contracted)
Some clusters are still connected to each other, some are isolated. Does anyone know why this is?
Thanks
回答1:
To get a table that contains the size of each cluster, use:
table(cfg$membership)
1 2 3
18 11 5
The lines mean that some people in group 1 talk to some in group 2 and some people in group 3 talk to people in group 2, but no one in group 1 talks to anyone in group 3. For instance, Mr Hi (group 2) talks to Actor 5 (group 1) and to Actor 32 (group 3).
Your other example is not connected. There are multiple connected components.
table(COMP$membership)
1 2 3 4 5 6 7 8 9 10 11
6196 4 7 5 2 2 2 8 2 1 3
13 14 15 16 17 18
2 2 2 2 2 2
Of course, In the contracted graph, there will still be no links across these components.
来源:https://stackoverflow.com/questions/64960236/formatting-graphs-in-r