Ex handouts igraph solution
Part 1 - network from AP/MS data
Interaction data from exercise 5 and edge scores from exercise 6
library(igraph) library(ggraph) ex5_interactions <- data.frame(from = c("A", "B", "B", "B", "B", "B", "D", "D", "E", "F", "F"), to = c("B", "C", "D", "E", "F", "H", "F", "G", "H", "G", "H"), score = c(-0.155, -0.234, -0.222, -0.183, -0.183, -0.183, -0.301, -0.194, -0.301, -0.301, -0.301)) g <- graph_from_data_frame(ex5_interactions, directed = FALSE) ggraph(g) + geom_edge_link() + geom_node_point() + geom_node_text(aes(label = names(V(g))), repel = TRUE)
NOTE that since we have not supplied a node attribute table with the "vertices" variable in the "graph_from_data_frame" function, igraph extraxts the names of the nodes from the interactions table. You therefore need to use names(V(g)) to extract names.
NOTE also that if you set "repel = TRUE" in "geom_node_text" when plotting, labels won't overlap with nodes or each other.
Part 2 - network from Y2H data
Interaction data from exercise 7
ex7_interactions <- data.frame(from = c("A", "A", "A", "B", "B", "B", "C", "E", "F", "F", "G"), to = c("B", "D", "G", "C", "D", "G", "F", "H", "G", "H", "H"), score = c(-0.301, -0.301, -0.778, -0.903, -0.477, -0.954, -0.778, -0.477, -0.778, -0.602, -0.778)) g <- graph_from_data_frame(ex7_interactions, directed = FALSE) ggraph(g, layout = "linear", circular = TRUE) + geom_edge_link() + geom_node_point() + geom_node_text(aes(label = names(V(g))), repel = TRUE)
NOTE that I chose a circular layout here, since this layout is independent of which edges the network contains. This enables me to quickly compare with the following.
g2 <- delete_edges(g, which(E(g)$score < -0.4)) ggraph(g2, layout = "linear", circular = TRUE) + geom_edge_link() + geom_node_point() + geom_node_text(aes(label = names(V(g))), repel = TRUE)
NOTE instead of making a new graph, manually selecting edges over the threshold score, I simply made a new graph by deleting the edges that does not satisfy the condition "score > -0.4".
I can then do the same for "score > -0.65", which speeds things up quite a bit.
g3 <- delete_edges(g, which(E(g)$score < -0.65)) ggraph(g3, layout = "linear", circular = TRUE) + geom_edge_link() + geom_node_point() + geom_node_text(aes(label = names(V(g))), repel = TRUE)