在R中设置图形格式 [英] Formatting Graphs in R

查看:73
本文介绍了在R中设置图形格式的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我试图弄清楚如何轻松地访问和操作在R中创建的图形.

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

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

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?

谢谢

推荐答案

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

To get a table that contains the size of each cluster, use:

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

这些行表示,第1组中的某些人与第2组中的某些人交谈,而第3组中的某些人与第2组中的人交谈,但是第1组中没有人与第3组中的任何人交谈.例如,Hi先生(第2组)与演员5(第1组)和演员32(第3组)交谈.

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.

这篇关于在R中设置图形格式的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆