  C RUBY-ON-RAILS MYSQL ASP.NET DEVELOPMENT RUBY .NET LINUX SQL-SERVER REGEX WINDOWS ALGORITHM ECLIPSE VISUAL-STUDIO STRING SVN PERFORMANCE APACHE-FLEX UNIT-TESTING SECURITY LINQ UNIX MATH EMAIL OOP LANGUAGE-AGNOSTIC VB6 MSBUILD # assign multiple color to each vertex in igraph  » r » assign multiple color to each vertex in igraph

By : John Anderson
Date : November 22 2020, 09:00 AM
I hope this helps . (EDIT) Because the edges were specified as integers the vertices were not in order in the graph. I changed the initial graph.data.frame call to specify the order, then everything should work. code :
``````g<-graph.data.frame(d,directed = F, vertices = 1:10)
``````
``````# Put in vertex order
m <- m[order(m\$vertex), ]

# Create a palette (any palette would do)
pal <- rainbow(n=length(unique(m\$affilation)))

# Get each node's first affiliation
oneAffil <- m\$affilation[!duplicated(m\$vertex)]

# Assign to the vertices as a color attribute
V(g)\$color <- pal[oneAffil]

plot(g)
``````
``````# Use a cast to split out affiliation in each group as a column
library(reshape2)
am <- acast(m, formula = vertex ~ affilation)

# Turn this into a list
values <- lapply(seq_len(nrow(am)), function(i) am[i,])

plot(g, vertex.shape="pie", vertex.pie=values, vertex.pie.color=list(pal))
`````` ## igraph plot conditional vertex color

By : Bakhtiyor Begmatov
Date : March 29 2020, 07:55 AM
wish help you to fix your issue So it seems (from the comments) that the goal is to set the vertex color based on whether the vertex appears in the first column of the dat data frame. Here is a way:
code :
``````V(g)\$color <- ifelse(V(g)\$name %in% dat[,1], "red", "blue")
plot(g)
`````` ## igraph assign a vector as an attribute for a vertex

By : Allchix
Date : March 29 2020, 07:55 AM
this will help I am trying to assign a vector as an attribute for a vertex, but without any luck: , This works fine:
code :
``````## replace c(3,1) by list(c(3,1))
g <- set.vertex.attribute(g, 'checked', 2, list(c(3, 1)))
V(g)\$checked
 3 1
``````
``````    g<-set.vertex.attribute(g, 'checked', 2, c(3, 1))
``````
``````number of items to replace is not a multiple of replacement length
``````
`````` length(list(c(3,1)))
 1
> length(data.frame(c(3,1)))
 1
`````` ## How to add color to part of igraph vertex?

By : robinsen
Date : March 29 2020, 07:55 AM
With these it helps I have two data files. One file contains all the date relationship like "A B", which means A has connection with B. I use this relationship to create the network with igraph. The other one has the data which I want to color these nodes in the igraph and of course all of these data exist in the first file. I wrote these codes below but I'm not sure how to color them. , You can color the nodes that are in Color.txt like this:
code :
``````library(igraph)
A B
B C
D A
A C
A
B
g <- make_graph(unlist(strsplit(dat, " ", T)))
plot(g, vertex.color = V(g)\$name %in% col)
`````` ## Adding Color to Vertex in VisNetwork Igraph

By : Hiếu Trần
Date : March 29 2020, 07:55 AM
this will help You can add it as the "group" attribute of your igraph which informs the node coloring by default. Drawing on code from your other post :
code :
``````col = c("#80FF00FF", "#FF0000FF", "#FF0000FF", "#00FFFFFF",
"#FF0000FF", "#8000FFFF", "#FF0000FF", "#FF0000FF",
"#FF0000FF", "#FF0000FF")
i96e <- set.vertex.attribute(i96e, name = "group",value = col)
visIgraph(i96e, idToLabel = TRUE, layout = "layout_nicely")
`````` ## Set igraph vertex color based on vertex attribute using RColorBrewer

By : Gopinath
Date : March 29 2020, 07:55 AM
help you fix your problem Typo? At least in my version of RColorBrewer it is brewer.pal not brewer_pal
We want to use the value of the group attribute to select a color from the palette that you generated, but those values are strings, not numbers. One way to get by this is to convert the strings to factors and then use those as numbers. That will convert each unique string value to a unique numeric value. We can use that to select the color.
code :
``````plot(g, vertex.color = pal[as.numeric(as.factor(vertex_attr(g, "group")))])
`````` 