Setting subgraph attributes in Rgraphviz - r

I'm trying to set a fill color in Rgraphviz for a subgraph. In the documentation, it is implied that you can set subgraph attributes where you set whether or not the subgraph is a cluster, like so:
subGList = list()
subGList[[1]] = list(graph=sg.iip, attrs=c(fillcolor="lightgreen", style = "filled",
shape = "rectangle"), cluster = TRUE)
Unfortunately, setting attributes in this manner doesn't seem to have any effect on the plot.
Because my subgraphs contain 20-30 nodes (where the full graph has 250 nodes), it's not really reasonable to code it with the nodewise method where you have to use each node name and set it to the color.
edit My current workaround is to do:
vc <- as.list(rep("lightgreen", length(subgraphNames)))
names(vc) <- subgraphNames
plot(fG.iip.NEL, 'neato', subGList=subGList, nodeAttrs = list(fillcolor=vc))

Related

Mark.groups in Igraph Error: unknown vertex.names selected

I am trying to create a network plot in igraph where
communities are marked by a color overlay as created by mark.groups
nodes are colored by a node attribute: deu
nodes are shaped a node attribute: topic_type
For this, I created an igraph object
And now, I try the following code:
set.seed(2)
plot(graph_deu,
mark.groups=list(c(33,1,34,2,36,53,54,56,42,43,55,57,18), c(35,48,50,27), c(38,45,46,47,49,28,25)),
mark.col=c("lemonchiffon", "slategray1", "thistle1"),
mark.border = NA,
edge.width =E(graph_deu)$weight,
vertex.size = deu_deg,
vertex.color = deu,
vertex.shape = topic_type,
vertex.label = node_labels,
vertex.label.cex=1.5
)
And I get the error:
Fehler in simple_vs_index(x, ii, na_ok) : Unknown vertex selected.
This seems to be igraph not finding the vertices as specified in mark.groups, but I have actually no idea why it would not find these vertices, as they are all correctly numbered.
Then, to avoid mark.groups, I tried another option - I directly plot the community object (mod2), however, in this case, nodes get colored according to community and not according to attribute deu:
plot(mod2, graph_deu,
edge.width =E(graph_deu)$weight,
vertex.size = deu_deg,
vertex.color = deu,
vertex.shape = topic_type,
vertex.label = node_labels,
vertex.label.cex=1.5)
This produces a network where vertices are colored by community, not by deu-attribute. What I would like it to look: I would like the communities to be circled by the semi-transparent overlay, but the nodes should be individually colored by deu-attribute.
Your help will be very appreciated. this is my first post on stackoverflow so if I should provide more pieces of code to reproduce I am happy to share it, I hope though that my igraph object is enough for the problem at hand.
Your graph has only 24 nodes, but you are referring to nodes using number higher than that, e.g. 36,53,54. If you use a number, igraph assumes that is the number of the node, so these number don't make sense for this graph. What you mean is the nodes with the names "36","53","54". The names are strings, not numbers. What you need to do is find the node numbers that correspond to these names. I show one way to do that below. Also, your plot statement refers to a number of variables that you did not provide so I commented them out here.
graph_deu = upgrade_graph(graph_deu)
plot(graph_deu)
Group1 = as.numeric(V(graph_deu)[as.character(c(33,1,34,2,36,53,54,56,42,43,55,57,18))])
Group2 = as.numeric(V(graph_deu)[as.character(c(35,48,50,27))])
Group3 = as.numeric(V(graph_deu)[as.character(c(38,45,46,47,49,28,25))])
set.seed(2)
plot(graph_deu,
mark.groups=list(Group1, Group2, Group3),
mark.col=c("lemonchiffon", "slategray1", "thistle1"),
mark.border = NA,
edge.width =E(graph_deu)$weight,
# vertex.size = deu_deg,
# vertex.color = deu,
# vertex.shape = topic_type,
# vertex.label = node_labels,
vertex.label.cex=1.5
)

How to retain the general shape of a network in igraph?

I currently have a network graph in igraph with which I am running simulations to see how the frequency of traits change over time like so:
g <- erdos.renyi.game(1000, 1/1000)
V(g)$nice <- sample(c(0, 1), vcount(g), replace = TRUE, prob = c(0.1, 0.9)
Following this I have a working code that modifies the the network across several "turns". The problem arises when I graph the network. I initially graph the network at t = 0 and once more at t = 20 or so to compare the two and see how they have changed. However, the location of the nodes have changed from the initial to the final. Is there a way that I can retain the location of the nodes in the actual graph? (i.e. so that node 4 will remain at some coordinate (a, b) despite changes in the network)
You can repeat the same layout by using the layout argument to plot. First, you create a layout using one of the many layout_ arguments, then just call plot specifying the layout. If you plot again with the same layout, the nodes will be in the same place.
LO_FR = layout_with_fr(g)
plot(g, layout=LO_FR, vertex.size=4, vertex.label=NA,
main="layout_with_fr")
LO_N = layout_nicely(g)
plot(g, layout=LO_N, vertex.size=4, vertex.label=NA,
main="layout_nicely")
Type help(package=igraph) and then scroll down to the functions whose names start with layout_. Try several and pick one that you like.

Visualizing relation between two objects using R and export to HTML

I am using R to visualize relation between, say, 5-6 different nodes. Now, a graph is probably the best way to do it. The issue is, the edges are way too many. There can be a hundred edges between two vertexes. That makes the graph look very clumsy. I want the edge name to be displayed. With a hundred edge name being displayed, they overlap over each other and hence not comprehensible.
So, I have two questions-
Is there any other way in which I can represent the data? Some kind of chart probably?
I want to export the final output to HTML, which uses d3.js or any other similar library, keeping the edge name and a few other similar information intact. What will be the best plugin to use in that case?
I am using the igraph library to create the graph in R.
I also tried using the networkD3 library to export it to an HTML and make it interactive.
graph <- graph.data.frame(edges, directed = TRUE, vertices = vertex)
plot(graph, edge.label = E(graph)$name)
wc <- cluster_walktrap(graph)
members <- membership(wc)
graph_d3 <- igraph_to_networkD3(graph, group = members)
graph_forceNetwork <- forceNetwork(Links = graph_d3$links, Nodes = graph_d3$nodes,
Source = 'source',
Target = 'target',
NodeID = 'name',
Group = 'group',
zoom = TRUE,
fontSize = 20)
Right now, it is a graph with only two vertex and about 60-70 edges between them. So, I did not use any particular layout.

Color edges of a tree

Using vegan to create minimum spanning tree:
library(vegan)
MST <- spantree(D)
where D is a distance / dissimilarity matrix amongst N entities.
I then plot using:
plot(MST, type = "t")
I now want to color the different nodes of the tree, depending on a label I have in DataSet$SubGroups, indicating which subgroup each of the N entities belong to. So that for instance, for all entities for which DataSet$SubGroups == SubGroup1, I want their label to be Green, for Subgroup2 Blue etc.
I tried with nodelabels from the ape package, but no luck as I get:
Error in get("last_plot.phylo", envir = .PlotPhyloEnv) : object
'last_plot.phylo' not found
Anybody any idea on how to get the color right?
Is it also possible to specify RGB color codes?

Colors and a plotting term document matrix

Following the example of plotting a term-document matrix below,
library("tm")
data("crude")
tdm <- TermDocumentMatrix(crude, control = list(removePunctuation = TRUE,
removeNumbers = TRUE,
stopwords = TRUE))
plot(tdm, terms = findFreqTerms(tdm, lowfreq = 6)[1:25], corThreshold = 0.5)
Is there a way to colorize the nodes based on how many vertices they have? Is there an example of making the nodes with more vertices larger or something to that effect as well?
I appears that the nodes that end up being plotted are of the class AgNode. The properties that you can set of the AgNode are listed on the ?AgNode help page. Once you know what properties you would like to set, you can pass a list to a nodeAttrs parameter to your plotting command. (EDIT: actually a better list is probably the node attributes description in the Rgraphviz documentation)
The nodeAttrs parameter take a list where each named element of that list corresponds to one of the properties of AgNode. At each position, you store a named vector where the name of the vector corresponds to the node name (ie the word in your term matrix) and the value represents the value for that attribute. For example
list(
color=c(futures="blue", demand="red"),
shape=c(crude="ellipse", budget="circle"),
)
So when you wanted to color the terms by the number of vertexes they have, i'm going to assume you mean edges as each word is a single vertex in the graph. So, using your tdm object
freqterms <- findFreqTerms(tdm, lowfreq = 6)[1:25]
vtxcnt <- rowSums(cor(as.matrix(t(tdm[freqterms,])))>.5)-1
I save the terms you wanted, and then I basically copied the code inside the plot command to calculate the correlations with your cutoff of 0.5 to see how many other words each word in this subset is connected to. That's the vtxcnt variable. (There may be a more efficient way to extract this but I could not find it). Now I'm ready to assign colors
mycols<-c("#f7fbff","#deebf7","#c6dbef",
"#9ecae1","#6baed6","#4292c6",
"#2171b5", "#084594")
vc <- mycols[vtxcnt+1]
names(vc) <- names(vtxcnt)
Here I grabbed some colors from ColorBrewer. I have 8 values because the values of vtxcnt range from 0-8. If you had a wider range or wanted to collapsed categories, you could use the cut() command to categorize them. Then I created a named vector vc that matches up each word to the appropriate color. vc should look like this
head(vc)
# ability accord agreement ali also analysts
# "#084594" "#c6dbef" "#2171b5" "#9ecae1" "#f7fbff" "#4292c6"
And now we are ready to make the plot
pp <- plot(tdm,
terms = freqterms,
corThreshold = 0.5,
nodeAttrs=list(fillcolor=vc))
So as you can see the customizing of nodes is pretty flexible. You can color them how every you like if you pass the correct values to nodeAttrs.

Resources