基于聚类分析创建新图,边是来自已创建组的连接器
Creating new graph basing on cluster anylise with edges which are connectors from created group
library(network)
library(networkD3)
library(igraph)
library(visNetwork)
df <- read.table(header = TRUE,
text = "src target
cllient1 cllient2
cllient1 cllient4
cllient1 cllient6
cllient2 cllient3
cllient4 cllient1
cllient4 cllient3
cllient5 cllient6
cllient6 cllient5")
df_graph <- graph_from_data_frame(df)
simpleNetwork(df,zoom = T,fontSize = 9)
wc <- cluster_walktrap(df_graph)
members <- membership(wc)
df_graph_cntrctd <- contract(df_graph, members, vertex.attr.comb = toString)
df_graph_cntrctd <-as.undirected(df_graph_cntrctd)
df_graph_cntrctd <- as_long_data_frame(df_graph_cntrctd)
idLabel <- df_graph_cntrctd[,c(2,4)]
idLabel <- idLabel[!duplicated(df_graph_cntrctd[c("to","ver2[el[, 2], ]")]),]
colnames(idLabel)[1] <- "id"
colnames(idLabel)[2] <- "title"
idLabel['label'] <- idLabel$id
FromTo <-df_graph_cntrctd[,c(1,2)]
FromTo <- FromTo[!duplicated(FromTo[c("from","to")]),]
nodes <- data.frame(id = idLabel$id,
label = idLabel$label,
title = idLabel$title)
edges <- data.frame(from = FromTo$from, to = FromTo$to)
network<-(visNetwork(nodes, edges, width = "100%",height = 900 ) %>%
visOptions(highlightNearest = TRUE, nodesIdSelection = TRUE))
到目前为止,我们得到了一个网络 :) 并且有可能获得一个客户列表,这些客户在由 walktrap 创建的组之间建立了联系。这个想法是在边缘显示这些客户。创建的是下面的代码行,它们按降序显示所有连接的列表
V(df_graph)$name <- members
x <- as_edgelist(df_graph, names = T)
V(df_graph)$name <- 1:vcount(df_graph)
E(df_graph)[x[,1] != x[,2]]
在结果中我们得到
+ 1/8 edge from c92bcba (vertex names):
[1] 1->5
这意味着据我所知,标签为“1”的组通过 ID 号 1 的 "client1" 和 ID 号 5 的 "client6" 与组(标签“2”)连接。
我的问题 是如何得到像这里这样的结果,毕竟我们可以在这里创建 table 像这样:
from to label
1 1 NA
1 2 Client1,Client6
2 2 NA
其中 "from" 和 "to" 是从集群分析创建的组的名称,Client1 和 Client6 是连接这两个组的客户端
这有效但非常不优雅:
df <- read.table(header = TRUE,
text = "src target
cllient1 cllient2
cllient1 cllient4
cllient1 cllient6
cllient2 cllient3
cllient4 cllient1
cllient4 cllient3
cllient5 cllient6
cllient6 cllient5")
df_graph <- graph_from_data_frame(df)
wc <- cluster_walktrap(df_graph)
df_graph0 <- df_graph
V(df_graph)$name <- membership(wc)
根据成员资格获取边缘列表,这在您的请求中转化为 from
和 to
。
x <- as_edgelist(df_graph, names = T)
communities <- ends(df_graph, E(df_graph))
将名称重置为顶点 ID(未请求但可能有用)
V(df_graph)$name <- 1:vcount(df_graph)
ids <- ends(df_graph, E(df_graph))
设置与客户端对应的顶点名称(label
)
V(df_graph)$name <- V(df_graph0)$name
label <- ends(df_graph, E(df_graph))
存储在dataframe
df_result <- data.frame(from = communities[,1], to = communities[,2],
label1 = label[,1], label2 = label[,2], ids1 = ids[,1], ids2 = ids[,2])
结果是:
from to label1 label2 ids1 ids2
1 1 1 cllient1 cllient2 1 2
2 1 1 cllient1 cllient4 1 3
3 1 2 cllient1 cllient6 1 5
4 1 1 cllient2 cllient3 2 6
5 1 1 cllient4 cllient1 3 1
6 1 1 cllient4 cllient3 3 6
7 2 2 cllient5 cllient6 4 5
8 2 2 cllient6 cllient5 5 4
此外,您可以粘贴 label1
和 label2
以使 label
列以逗号分隔。
编辑:为了"contract"标签,你可以这样做:
library(tidyr)
library(dplyr)
df_result$label <- paste(df_result$label1, df_result$label2, sep = ",")
df_nested <- df_result %>% select(from, to, label) %>% nest(-from, -to)
要将这些嵌套标签用作边缘标签或将它们粘贴在一起的字符串:
df_nested$data <- sapply(1:nrow(df_nested),
function(x) paste(unlist(df_nested$data[[x]]), collapse = " "))
使用 Ben Nutzer 的代码:
df_result$label <- paste(df_result$label1, df_result$label2, sep = ",")
我们可能会发现社区之间有多少连接使用这样的功能:
library(plyr)
ddply(df_result,.(from,to),nrow)
并得到:
from to V1
1 1 1 5
2 1 2 1
3 2 2 2
这告诉我们组之间只有一个联系
library(network)
library(networkD3)
library(igraph)
library(visNetwork)
df <- read.table(header = TRUE,
text = "src target
cllient1 cllient2
cllient1 cllient4
cllient1 cllient6
cllient2 cllient3
cllient4 cllient1
cllient4 cllient3
cllient5 cllient6
cllient6 cllient5")
df_graph <- graph_from_data_frame(df)
simpleNetwork(df,zoom = T,fontSize = 9)
wc <- cluster_walktrap(df_graph)
members <- membership(wc)
df_graph_cntrctd <- contract(df_graph, members, vertex.attr.comb = toString)
df_graph_cntrctd <-as.undirected(df_graph_cntrctd)
df_graph_cntrctd <- as_long_data_frame(df_graph_cntrctd)
idLabel <- df_graph_cntrctd[,c(2,4)]
idLabel <- idLabel[!duplicated(df_graph_cntrctd[c("to","ver2[el[, 2], ]")]),]
colnames(idLabel)[1] <- "id"
colnames(idLabel)[2] <- "title"
idLabel['label'] <- idLabel$id
FromTo <-df_graph_cntrctd[,c(1,2)]
FromTo <- FromTo[!duplicated(FromTo[c("from","to")]),]
nodes <- data.frame(id = idLabel$id,
label = idLabel$label,
title = idLabel$title)
edges <- data.frame(from = FromTo$from, to = FromTo$to)
network<-(visNetwork(nodes, edges, width = "100%",height = 900 ) %>%
visOptions(highlightNearest = TRUE, nodesIdSelection = TRUE))
V(df_graph)$name <- members
x <- as_edgelist(df_graph, names = T)
V(df_graph)$name <- 1:vcount(df_graph)
E(df_graph)[x[,1] != x[,2]]
在结果中我们得到
+ 1/8 edge from c92bcba (vertex names):
[1] 1->5
这意味着据我所知,标签为“1”的组通过 ID 号 1 的 "client1" 和 ID 号 5 的 "client6" 与组(标签“2”)连接。 我的问题 是如何得到像这里这样的结果,毕竟我们可以在这里创建 table 像这样:
from to label
1 1 NA
1 2 Client1,Client6
2 2 NA
其中 "from" 和 "to" 是从集群分析创建的组的名称,Client1 和 Client6 是连接这两个组的客户端
这有效但非常不优雅:
df <- read.table(header = TRUE,
text = "src target
cllient1 cllient2
cllient1 cllient4
cllient1 cllient6
cllient2 cllient3
cllient4 cllient1
cllient4 cllient3
cllient5 cllient6
cllient6 cllient5")
df_graph <- graph_from_data_frame(df)
wc <- cluster_walktrap(df_graph)
df_graph0 <- df_graph
V(df_graph)$name <- membership(wc)
根据成员资格获取边缘列表,这在您的请求中转化为 from
和 to
。
x <- as_edgelist(df_graph, names = T)
communities <- ends(df_graph, E(df_graph))
将名称重置为顶点 ID(未请求但可能有用)
V(df_graph)$name <- 1:vcount(df_graph)
ids <- ends(df_graph, E(df_graph))
设置与客户端对应的顶点名称(label
)
V(df_graph)$name <- V(df_graph0)$name
label <- ends(df_graph, E(df_graph))
存储在dataframe
df_result <- data.frame(from = communities[,1], to = communities[,2],
label1 = label[,1], label2 = label[,2], ids1 = ids[,1], ids2 = ids[,2])
结果是:
from to label1 label2 ids1 ids2
1 1 1 cllient1 cllient2 1 2
2 1 1 cllient1 cllient4 1 3
3 1 2 cllient1 cllient6 1 5
4 1 1 cllient2 cllient3 2 6
5 1 1 cllient4 cllient1 3 1
6 1 1 cllient4 cllient3 3 6
7 2 2 cllient5 cllient6 4 5
8 2 2 cllient6 cllient5 5 4
此外,您可以粘贴 label1
和 label2
以使 label
列以逗号分隔。
编辑:为了"contract"标签,你可以这样做:
library(tidyr)
library(dplyr)
df_result$label <- paste(df_result$label1, df_result$label2, sep = ",")
df_nested <- df_result %>% select(from, to, label) %>% nest(-from, -to)
要将这些嵌套标签用作边缘标签或将它们粘贴在一起的字符串:
df_nested$data <- sapply(1:nrow(df_nested),
function(x) paste(unlist(df_nested$data[[x]]), collapse = " "))
使用 Ben Nutzer 的代码:
df_result$label <- paste(df_result$label1, df_result$label2, sep = ",")
我们可能会发现社区之间有多少连接使用这样的功能:
library(plyr)
ddply(df_result,.(from,to),nrow)
并得到:
from to V1
1 1 1 5
2 1 2 1
3 2 2 2
这告诉我们组之间只有一个联系