开发者

how to make two networks connected with selected members

I have a data like this

df<- structure(list(Core = c("Bestman", "Tetra"), member1 = c("Tera1", 
"Brownie1"), member2 = c("Tera2", "Brownie2"), member3 = c("Tera3", 
"Brownie3"), member4 = c("Tera4", "Brownie4"), member5 = c("Tera5", 
"Brownie5"), member6 = c("", "Brownie6"), me开发者_JAVA技巧mber7 = c("", "Brownie7"
)), class = "data.frame", row.names = c(NA, -2L))

I want to connect all the members to their Core. for example if you look at the first row, you can see there are 5 members , I want to connect them to their Core The same for the second row

Then I connect both Core together

Here is what I have done

mydf <- crossprod(table(cbind(df[1], stack(df[-1]))[-3]))
graph_from_adjacency_matrix(mydf, diag = F, weighted = T, mode = "undirected") %>% 
  plot(edge.width = E(.)$weight) 


If i understood correctly, what you want is:

library(igraph)

df<- data.frame(Core = c("Bestman", "Tetra"), member1 = c("Tera1", 
"Brownie1"), member2 = c("Tera2", "Brownie2"), member3 = c("Tera3", 
"Brownie3"), member4 = c("Tera4", "Brownie4"), member5 = c("Tera5", 
"Brownie5"), member6 = c("", "Brownie6"), member7 = c("", "Brownie7"))


edges <- t(do.call(rbind, apply(
    df, 1, function(x)  cbind(x[1], x[x!=""][-1])))) 

core_edges <- if(nrow(df)>1) combn(df$Core,2) else c()

g<-graph(c(edges,core_edges), directed=F )
plot(g , edge.width = E(g)$weight) 

how to make two networks connected with selected members

EDIT

To colorize and resize nodes:

V(g)$color <- apply(df, 1, \(x) names(V(g)) %in% x) |> apply(1,which)
V(g)$size <- 15 
V(g)[df$Core]$size <- degree(g, V(g)[df$Core]) + 15
plot(g)

how to make two networks connected with selected members

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜