問題描述
二分網絡igraph中的頂點屬性 (Vertex attributes in bipartite network igraph)
我真正想做的是根據“人物遊戲”字段對人物頂點進行不同的著色,但這似乎不是我可以訪問頂點的屬性,只能訪問邊緣。所以這可以為邊緣著色:</p>
df <‑ read.csv("file.csv", header = TRUE, sep = ",")
df.network <‑ graph.data.frame(df, directed = F)
V(df.network)$type <‑ bipartite.mapping(df.network)$type
但是如果我應用到頂點而不是邊緣,它就不起作用,因為我無法將 PersonGame 屬性應用到頂點。
誰能幫忙?
任何人都可以幫忙嗎?</p> 任何人都可以幫忙嗎?</p>參考解法
方法 1:
I recommend transferring the games to the nodes. Since some nodes are Groups and others are Persons, I will just call it Game (rather than PersonGame and GroupGame), but I will transfer the PersonGames to the Persons and the GroupGames to the Groups.
PA = unique(cbind(ends(df.network, E(df.network))[,2], E(df.network)$PersonGame))
GA = unique(cbind(ends(df.network, E(df.network))[,1], E(df.network)$GroupGame))
V(df.network)$Game = ""
V(df.network)[PA[,1]]$Game = PA[,2]
V(df.network)[GA[,1]]$Game = GA[,2]
Now, every node has a Game. We can modify your statement that created an edge color to create a vertex color. I added a line to color the Groups differently
V(df.network)$color <‑ ifelse(V(df.network)$Game=='Snooker',
"red", ifelse(V(df.network)$Game=='Football', "blue", "orange"))
V(df.network)[!V(df.network)$type]$color = "lightgray"
Now we can plot with node colors.
LO = layout_as_bipartite(df.network)
plot(df.network, layout=LO)