0

i want plot a graph, while a apply a loop to a dataframe algo2 that contains the nodes and for each set of nodes i affect a color.

this is my dataframe

> algo2
  node                                                         Neighbors
1   34 9, 10, 14, 15, 16, 19, 20, 21, 23, 24, 27, 28, 29, 30, 31, 32, 33
2    1                           2, 3, 4, 5, 6, 7, 8, 11, 12, 13, 18, 22
3   26                                                                25

and i apply this code before plotting:

for(i in 1:nrow(algo2)){ 

    nnn<-as.data.frame(algo2$Neighbors[i])  
    nnn<-as.character(nnn[,1])   
    aa<-as.character(algo2$node[i])  

    V(g)$color <- ifelse(V(g)$name %in% c(aa,nnn), 
                         rainbow(i), 
                         "white")

} 

V(g)$shape<-
    ifelse(V(g)$name %in% AllNeighbors2_algo2[,1], 
           "rectangle", 
           "circle") 
V(g)$size<-ifelse(V(g)$name %in% AllNeighbors2_algo2[,1], 
                  4, 
                  3)
plot.igraph(g, 
            vertex.color=V(g)$color, 
            vertex.size=V(g)$size, 
            vertex.shape=V(g)$shape)

the problem that when i run the plot, it take only the last value, that means, that it colorate only the vertex 26 and 25. i want that each row od dataframe take a color different to others rows. thanks for your helps.

Jaap
  • 81,064
  • 34
  • 182
  • 193
Sasa88
  • 327
  • 1
  • 3
  • 15
  • 2
    Please create a [reproducible example](http://stackoverflow.com/questions/5963269/how-to-make-a-great-r-reproducible-example). I don't believe you've shown how you are creating your `g` object. Also, what's the structure of the `Neighbors` column? Is it a character value with commas pasted in? Because `"3" %in% "3, 4"` returns FALSE. You need a proper vector of character values. `"3" %in% c("3","4")`. Include the `dput()` of `algo2` to make things more clear. – MrFlick Feb 13 '15 at 03:18
  • my graph 'g' i get it from a text file it's a graph with 34 nodes and 78 edges. i apply a process to gather some nodes to each others, according to some criteria. and after, i write it in each row of dataframe 'algo2', the nodes that are together . i want to give to each row a specify color (to form something as groups) – Sasa88 Feb 13 '15 at 09:08
  • By the way, a bit more advice on posting. You can take a variable such as `algo2` you are using, and do a `dput(algo2)` on it. This returns the contents of the data-frame in the console. Then you can copy the result from the console, and you will have `variable <- structure(...)`. Anyone reading your post will then be able to evaluate this line and will have exactly the same data as you. It seems the easiest way to make your question reproducible. I just learned it very recently, it's a very useful command for stackoverflow. – puslet88 Feb 15 '15 at 14:37

1 Answers1

0

Your last line of the loop will overwrite all previous operations.

    V(g)$color <- ifelse(V(g)$name %in% c(aa,nnn), 
                     rainbow(i), 
                     "white")

It will check for correspondence with the last i, and make everything else white. Use the following one instead, it does nothing when if is not answered.

    V(g)$color <-  if(V(g)$name %in% c(aa,nnn)) { rainbow(i)}

Note that I am unable to test this as your example is not reproducible. For a reproducible example you should have the dataframe as a minimal structure that we can just evaluate (instead of a print-out).

If previously it just had the last line coloured then this should work.

Update:

Ok, for future reference, a reproducible data frame would look like this.

g <- graph.full(35, directed = FALSE)#, loops = FALSE)
V(g)$name <- c(1:35)
a <- paste(c(9, 10, 14, 15, 16, 19, 20, 21, 23, 24, 27, 28, 29, 30, 31, 32, 33), sep = "", collapse=", ")
b <- paste(c(2, 3, 4, 5, 6, 7, 8, 11, 12, 13, 18, 22), sep = "", collapse=", ")
c <- as.character(25)
algo2 <- data.frame(node = c(34,1,26), Neighbors = c(1,2,3))
algo2$Neighbors[1] <- a                    
algo2$Neighbors[2] <- b
algo2$Neighbors[3] <- c

This can be done more tidily, but you have to have it as otherwise any commenter will have to make it themselves, which they probably won't. You also have the variable All_Neighbours which I can't comment on. Notice that I had to create the g from scratch as well. We can't run the loop without referring to it.

Your issue is solved by replacing the "white" with V(g)$color in that line. However I'm not sure if your previous formatting worked with it. A working version with the given initialization is.

V(g)$color <- "White"

for(i in 1:nrow(algo2)){ 
  #i=1
  nnn<-as.data.frame(algo2$Neighbors[i])  
  nnn<- strsplit(as.character(nnn[,1]), ", ")[[1]]
  aa<-as.character(algo2$node[i])  

  V(g)$color <- ifelse(V(g)$name %in% c(aa,c(nnn[1:length(nnn)])), 
                       rainbow(i), 
                       V(g)$color)
  print(V(g)$color)

} 


plot.igraph(g, 
            vertex.color=V(g)$color)

You don't include data on the shapes and sizes, so we can't tell what they're about. You can see the print command gradually updating the colours of the network. Next time try to prepare the question more fully to simplify assistance. You might not get so lucky next time.

puslet88
  • 1,288
  • 15
  • 25
  • V(g)$color <- if(V(g)$name %in% c(aa,nnn)){ rainbow(1)} Erreur dans `V<-`(`*tmp*`, value = 1:34) : invalid indexing De plus : Message d'avis : In if (V(g)$name %in% c(aa, nnn)) { : la condition a une longueur > 1 et seul le premier élément est utilisé – Sasa88 Feb 13 '15 at 09:36
  • thanks @puslet88. i'm new in R and in this forum. and i don't speak english well. thanks for your patience. the proposed solution give me the same result. it's plot only the last treated nodes, i.e. the last row of dataframe in color, and the other nodes it draw them in white. i want that for each iteration we color the nodes treated and we save the color for the other one. i.e. that my graph will be drawing with 3 colors. the prob that i deal with a graph of 10000 nodes, that's why i am using rainbow(i). just i do the test in this graph. thanks so much – Sasa88 Feb 13 '15 at 12:15
  • I hope it helps! The trick here is that you make it all white at first, and then add one colour per iteration. The print-command should show how it happens in the console. If you want to plot each of the groups separately, you can add the plot function to inside the loop. Glad to help, I'm also a new writer here, but it has shown to be very helpful for learning especially. Good luck! – puslet88 Feb 13 '15 at 13:28
  • Thanks @puslet88, i will take in consideration your remarks. – Sasa88 Feb 15 '15 at 12:07