【问题标题】:Formatting Graphs in R在 R 中格式化图形
【发布时间】:2021-03-05 16:26:51
【问题描述】:

我正在尝试弄清楚如何轻松访问和操作在 R 中创建的图表。

如果我从以下数据开始。我创建一个图,运行一些图聚类,然后绘制第一个聚类:

#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))

用户 G5W 展示了如何制作一个包含每个集群大小的表:

 my_table =  table(cfg$membership)

我还想出了如何将所有观察结果“浓缩”(收缩,收缩)到它们相应的社区中,然后制作一个情节。

contracted <- simplify(contract(karate,membership(cfg)))
plot(contracted)

似乎有两条“线”将三个集群连接在一起:

有谁知道这条线是否“真的意味着什么”?这条线是自然产生的吗?这条线连接这三个集群的依据是什么?

我模拟了自己的网络数据,运行图聚类,按聚类收缩结果,然后创建了一个图

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)

有些集群仍然相互连接,有些是孤立的。有谁知道这是为什么?

谢谢

【问题讨论】:

    标签: r graph data-visualization cluster-analysis nodes


    【解决方案1】:

    要获取包含每个集群大小的表,请使用:

    table(cfg$membership)
     1  2  3 
    18 11  5 
    

    这些线条表示第 1 组中的一些人与第 2 组中的一些人交谈,第 3 组中的一些人与第 2 组中的人交谈,但第 1 组中的任何人都没有与第 3 组中的任何人交谈。 例如,Hi 先生(第 2 组)与演员 5(第 1 组)和演员 32(第 3 组)交谈。

    您的另一个示例未连接。有多个连接的组件。

    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
    

    当然,在收缩图中,这些组件之间仍然没有链接。

    【讨论】:

    • 感谢您的回复!您对签约社区有任何想法吗?如果你使用 contract() 函数来“浓缩”社区,这些社区之间是否应该有一些“可见的联系”?谢谢!
    • 澄清一下,你的意思是 a = table(cfg$membership) 而不是 table(COMP$membership) ?非常感谢!
    • imgur.com/a/EWmY6Q6 这意味着,社区 1 有 99 个观测值,社区 2 有 88 个观测值,等等?
    • 是的 `table(cfg$membership` 我在我的代码中使用了 COMP,只是复制了它。当我点击你的链接时,我只是得到一个黑屏。
    • 抱歉 - 请试试这个:imgur.com/a/TglQmTz ?
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2015-10-26
    • 1970-01-01
    • 2015-04-10
    • 2019-07-22
    相关资源
    最近更新 更多