How to code edge attributes as vertex attributes using igraph in R - r

I am graphing a network and trying to color the vertices using non-overlapping attributes. I want my network diagram to be colored according to different attributes. In this example, if the first three letters of ID 2 are equal to U 50 or U 51, I want this to show up as red. I have 5 attributes I want this graph coded by and any observations that don't fall into one of the categories should be coded in a default color. In this way I will be able to see the intensity of these attributes and better communicate this to other people. So far, I have been unable to get the code to work using a variety of different coding methods. First I tried to create a new variable that assigned the correct attribute to each observation before converting it into an i graph object.
anon.nd$vertexcolor[substr(anon.nd$ID2,1,3)=="U50" | substr(anon.nd$ID2,1,3)=="U51"]<-"O"
anon.nd$vertexcolor[substr(anon.nd$ID2,1,3)=="U54" | substr(anon.nd$ID2,1,3)=="U55"]<-"P"
anon.nd$vertexcolor[anon.nd$INT.type=="K1"]<-"INT.NB"
anon.nd$vertexcolor[anon.nd$Country=="L12"]<-"UK"
anon.nd$vertexcolor[anon.nd$ID2=="U769"]<-"OBL"`
I then specified the colors I wanted to assign to each each attribute. I used the get vertex attribute code and filled in the appropriate colors.
anon.nd1<-graph.data.frame(anon.nd)
vertex_colors=get.vertex.attribute(anon.nd1,"vertexcolor")
colors=c('azure3', 'firebrick1', 'orange1', 'darkblue', 'darkolivegreen', 'gold')
vertex_colors[vertex_colors==0]=colors[1]
vertex_colors[vertex_colors==1]=colors[2]
vertex_colors[vertex_colors==2]=colors[3]
vertex_colors[vertex_colors==3]=colors[4]
vertex_colors[vertex_colors==4]=colors[5]
vertex_colors[vertex_colors==5]=colors[6]
I tried this same method using just:
vertex_colors<-vertex_colors+1
Then to plot, I changed my edge color to black, specified my layout, and change the size of my edges and vertices.
E(anon.nd1)$color="black"
nd.layout<-layout.fruchterman.reingold(anon.nd1)
plot(anon.nd1, layout=nd.layout, vertex.color=vertex_colors, vertex.size=2, edge.arrow.size=.01, vertex.label=NA)
Using this method, no color shows up on the vertices, not even the default color. Using a different method where I set the vertex attribute, I do a little better. The default color shows up, but the colors I want do not.
anon.nd2<-graph.data.frame(anon.nd)
V(anon.nd2)$colors<-"azure3"
V(anon.nd2)$colors[substr(anon.nd2$ID2,1,3)=="U50" | substr(anon.nd2$ID2,1,3)=="U51"]<-"firebrick1"
V(anon.nd2)$colors[substr(anon.nd2$ID2,1,3)=="U54" | substr(anon.nd2$ID2,1,3)=="U55"]<-"orange1"
V(anon.nd2)$colors[anon.nd2$Country=="L12"]<-"darkblue"
V(anon.nd2)$colors[anon.nd2$INT.type=="K1"]<-"darkolivegreen"
V(anon.nd2)$colors[anon.nd2$ID2=="U769"]<-"gold"
E(anon.nd2)$color<-"black"
nd.layout<-layout.fruchterman.reingold(anon.nd2)
windows(width=20, height=16)
plot(anon.nd2, layout=nd.layout, vertex.size=2, edge.arrow.size=.01, vertex.label=NA, vertex.color="vertex_colors")
I think the problem might be that I am trying to code vertex color using multiple (non-overlapping) edge attributes. But I don't know how to convert and edge attribute into a vertex attribute. I also don't know if there is some other, unidentified problem with my code.
Here is the link to my data is copied below as well as a link to my full code file which has one or two other methods I tried using to solve this problem. Any help would be much appreciated!
Data
And here is an R file with my code, which is also above: R-file

I think you are messing up your vertex_color vector, have a look at it with head().
anon.nd$vertexcolor[anon.nd$INT.type=="K1"]<-"INT.NB"
vertex_colors[vertex_colors==0]=colors[1]
You first assign a string and then compare with numbers, so non of them should be true.
plot(anon.nd2, layout=nd.layout, vertex.size=2, edge.arrow.size=.01, vertex.label=NA, vertex.color="vertex_colors")
This contains a typo and returns an error for me since "vertex_colors" isn't a colour name.
Last but not least, does
plot(anon.nd2, vertex.color=colors)
or
plot(anon.nd2, vertex.color=1:8)
result in a colourful plots? If yes, the vertex_colors vector is your problem, if not something else is.

Related

External node color in Gephi

I'm trying to create a graph in Gephi. This graph is undirected and has over 100 nodes. I would like to color the nodes in this graph according to my convenience. Say I'd like to color the nodes in 9 different colors. For doing this, I downloaded a plugin to Gephi which may be found here.
Though the plugin works, I still have to go and enter the color value for every single node manually. There doesn't seem to be a way to do this programatically. And I'd have to create about 10 such graphs, so that means a lot of manual labor.
Can someone help me out with this problem? Does someone know a better way to have custom colors for each node generated programatically? At this point, I'm generating a .gdf file, the format looks something like this.
I also know that Gephi is quite buggy, is there any other graph visualization software that I can use? Out of the other usual things that one does to the graph, I definitely want the capability to set edge weights. In other words, I'd like to make the edges thicker if the edge weight is more and vice-versa.
I can't use D3 coz I don't know Javascript. I looked into using GraphViz but it seems like it requires a lot of manual manipulation of the graph file. I'd like to have something that I'm able to generate programatically.
What seems to be working is to add a column named color VARCHAR where you add the color hex value prepended with the # sign and no quotes.
Example:
nodedef> name,label,color VARCHAR
a,"Apple",#00ffdd
b,"Banana",#00ddff
c,"Cherry",#dd00ff
d,"Did it!",#0012ca
e,"Ed 209",#121212
edgedef> node1,node2,weight
a,b,2
b,c,30
b,d,0.4
d,e,200
Edit:
For a more informed answer take a look at the Gephi documentation. The color values in the link are rgb triples but I tried with hex and it worked

Igraph in R: how to change edge color based on edge attribute

I am trying to generate an Igraph in R.
Now I want the edge color dependent on an edge attribute - department.
I cannot use ifelse statement because department values can be dynamic. I can find the number of unique departments, but I am not sure how to proceed further in creating different edge colors for different departments.
department= unique(edges$department)
department.count=length(department)
Example Code:
gg <- graph.atlas(711)
V(gg)$name=1:7
gg=set_edge_attr(gg,"Department",E(gg)1:10],c("A","B","C","A","E","C","G","B","C","A"))
E(gg)$label=E(gg)$Department
plot(gg)
I want to have different colors for each edge, depending on values of department in edge. All 'A' departments in one color , all B department edges in another color , so on.
Kindly help.
You should provide a small reproducible example when posting. That said, you should be able to do this by setting the color attribute of the edges:
E(testgraph)$color <- as.factor(edges$department)

Specify color for Cluster elements in R

I'm new to R and I would like to get some info.
I've formed three intersecting clusters using clusplot in R.
All the three clusters are of different colors(I have made use of shade and color attribute to do this)
Now,I would like to change color of each element in the cluster. I found that all the elements in the three clusters have the same color which is green
Is there a way that I could specify the color of all the elements for each cluster. Below is the screenshot and code
kmres <- kmeans(data1,centers=3,iter.max=100,nstart=25)
clusplot(data1,kmres$cluster,color=TRUE,shade=TRUE)
Here is my sample data set
I have three columns M,G, PTS.
M G PTS
82 209 521
Is there a way to do this(particularly using clusplot)?
If you look at the help page you can find the col.p attribute that may help you.
data1<-iris[,-5]
kmres <- kmeans(data1,centers=3,iter.max=100,nstart=25)
clusplot(data1,kmres$cluster,color=TRUE,shade=TRUE,col.p = kmres$cluster)
The only problem from my point of view is that the ellipses are colored by their density, and the result may be a bit confusing, but the points colors are different.

Use tkplot in igraph when matching vertex size to label

I am trying to apply the script suggested in another post on this forum for matching vertex size to label: Match vertex size to label size in igraph.
The solution for the plot function works perfectly but the same syntax cannot be used for the tkplot function. Replacing plot by tkplot returns an error message. I need to use the latter one because my figure has many vertexes and too long vertex labels, and i want to be able to readjust the positions of the vertexes manually. Can someone help?
This is not really an answer to your question, but a workaround. Use tkplot() to plot your vertices, adjust the vertex positions, then query the positions with tkplot.getcoords(), and use the returned coordinates in plot().

How are colors for facets in persp() selected?

This question is about graphics::persp . I'm trying to figure out what order the values in the colors argument are applied to the facets. For example, I wanted to do something similar to the drape argument in lattice::wireframe , where the color is a function of the z-values. I tried a simple example:
gairy<- matrix(rep(1,61^2),nr=61)
gairy[20:40,20:40]<-10
fairy<- matrix(nc=61,nr=61)
fairy[,]<-rainbow(20)[gairy]
persp(gairy,col=fairy)
(hope this works -- limited choice of posting from work)
https://plus.google.com/photos/102564725150183579541/albums/5779881398012083153
But the color assignments show up in rather strange places. I'm pretty sure from this and other experiments that the colors are not applied in any row or column-ordered sequence. Can anyone shed some light on this, i.e. how to order my "colors" array to match the data?
My suspicion is that the primary matrix passed to persp denotes the height of the nodes, while the colors refer to facets -- i.e. the dimensions of the color matrix should be one smaller than the dimensions of the node-height matrix. Recycling could give you weird-looking results.
For example, this looks reasonable:
persp(matrix(1:16,nrow=4),col=rainbow(9))

Resources