1
votes

Firstly I'm very new to R so apologies if this is a simple question.

I have a .csv with an edge network of groups, people associated with them and some attributes of both the people and groups

e.g.

Group Person PersonGame GroupGame
A Jane Doe Snooker Tennis
B John Doe Football Football
A Bill Smith Tennis Tennis
B Francis Underwood Football Football
A Francis Underwood Football Tennis

I've drawn a bipartite network:

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

Currently I've coloured the Group and Person vertexes differently as follows:

V(df.network)$color <- ifelse(V(df.network)$type, "grey", "orange")

what I really want to do though is colour the Person vertexes differently depending on the "Person Game" field, but this doesn't seem to be an attribute that I can access for a Vertex, only an Edge. So this works to colour the edges:

E(df.network)$color <- ifelse(E(df.network)$PersonGame=='Snooker', 
 "red", ifelse(E(df.network)$PersonGame=='Football', "blue", "orange"))

but it doesn't work if I apply to vertexes instead of edge as I can't get the PersonGame attribute to apply to a vertex.

Can anyone help?

1

1 Answers

0
votes

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)

Network with colored nodes