Dear Community Members:
I wanted to ask if there is a way to choose what nodes to graph on igraph. I am running the following code:
library(igraph)
g <- graph.adjacency(
as.matrix(as.dist(cor(t(POS_testing), method="pearson"))),
mode="undirected",
weighted=TRUE,
diag=FALSE
)
g <- simplify(g, remove.multiple=TRUE, remove.loops=TRUE)
g <- delete_edges(g, E(g)[which(E(g)$weight<0.9)])
Looking at my Vertices:
V(g)$comp <- components(g)$membership
V(g)$comp
[1] 1 2 3 4 3 5 6 7 3 8 9 10 11 12 13 14 15 16 3 3 3 17 18 19 20 15 3 21 3
[30] 3 3 3 22 23 3 3 3 3 24 25 3 26 27 28 3 29 3 3 3 30 31 3 3 3 3 3 3 32
[59] 3 33 3 3 34 35 3 3 28 28 36 37 38 3 39 28 3 3 3 3 40 21 21 3 41 3 3 42 3
[88] 3 3 3 3 3 43 44 3 45 3 46 3 47 48 3 3 3 3 3 3 49 3 3 3 3 50 3 3 3
[117] 3 51 3 3 52 53 52 52 54 55 52 3 56 57 58 55 59 3 60 61 3 62 3 63 64 65 66 67 3
[146] 3 3 3 68 3 69 64 70 71 3 72 3 73 74 3 3 75 3 3 3 3 32 76 3 77 3 78 3 3
[175] 79 80 3 81 82 3 3 73 3 3 3 83 84 85 73 3 3 3 3 3 86 32 87
Now, running this command that I found here: Delete unconnected short paths from a graph in igraph I can get Vertices that are assigned to the number '3'.
g <- induced_subgraph(g, V(g)$comp == 3)
V(g)$comp
[1] 3 3 3 3 3 3 3 3 3 3 3 3 3 3 3 3 3 3 3 3 3 3 3 3 3 3 3 3 3 3 3 3 3 3 3 3 3 3 3 3 3 3 3 3
[45] 3 3 3 3 3 3 3 3 3 3 3 3 3 3 3 3 3 3 3 3 3 3 3 3 3 3 3 3 3 3 3 3 3 3 3 3 3 3 3 3 3 3 3 3
[89] 3 3 3 3 3 3 3 3
However, I would like to choose more than just the vertices assigned to 3. I would like to choose to display 3, 21, 28, 32, 52, and 73.
I ran into this website: http://igraph.org/r/doc/delete_vertices.html So, I ran the code:
t <- delete_vertices(g, comps$Var1)
Where comps$Var1 is a numeric vector of all the numbers (displayed above) that I want removed (i.e. 1, 2, 4, 5, etc.). It did not work. I still have all 197 numbers popping up when I run V(t)$comp
.
Next I tried to manually fill it in instead & ran the following code (3 occurs 96 times, 21 occurs 3 times, etc):
V(g)$comp <- c(rep(3, 96), rep(21, 3), rep(28, 4), rep(32, 3), rep(52, 4), rep(73, 3)).
This did not work either.
Next I tried to add_edges instead (just these to see if it would work):
t <- add_edges(g, c(which(V(g)$comp == 3), which(V(g)$comp == 21), which(V(g)$comp == 28)))
This still produced all numbers above and did not selectively choose these numbers for me to graph.
I've also tried using the & operator like this:
g <- induced_subgraph(g, c(V(g)$comp == 3 & V(g)$comp == 21 & V(g)$comp ==29))
This also kept all my vertices.
I am out of ideas and I am asking this community if this is even possible. Thank you very much for your help.
We need POS_testing
to reproduce your example but try out:
g <- induced_subgraph(g, V(g)$comp %in% c(3, 21, 28, 32, 52, 73))