It's my first time working with igraph - so please excuse my basic questions. I'm doing a stakeholder analysis and have different actors on different administrative scales.
I'm trying to plot my network in a hierarchical way, because I want to show the interaction between administrative boundaries.
My vertices have the attributes "name", "Label" and "Scale".
I want to plot the graph with Scale as the layers.
edges <- data.table::fread("Master_edges.csv")
nodes <- data.table::fread("Master_nodes.csv")
V(g1_2)$name <- nodes$Label
V(g1_2)$Scale <-nodes$Scale
l <- layout_with_sugiyama(layers=V(g1_2)$Scale) #this is wrong, how do I define the layers?
plot(g1_2, layout=l, vertex.label=V(g1_2), vertex.color=NA)
Also I have different types of edges - how can I plot the different edge-types in different plots?
I have a weighted directed adjacency matrix and I am trying to plot a graph visualization using PlotRecipes.jl. I can't seem to find a way to show both the nodes with labels and somehow represent the edge weights on the edges. I have tried...
graphplot(G, weights=weights,names=names)
where G is the adjacency matrix, weights are the edge weights and names is a list of names for the nodes, but I get this output
using just
graphplot(G,names=names)
I get the same output.
Is there any way to do this with PlotRecipes.jl or is there possibly another library?
You can represent the weights with color (using the line_z keyword), but not with line thickness at the moment. cf https://github.com/JuliaPlots/PlotRecipes.jl/issues/39
I am trying to reproduce the first figure of this paper on graph clustering:
Here is a sample of my adjacency matrix:
data=cbind(c(48,0,0,0,0,1,3,0,1,0),c(0,75,0,0,3,2,1,0,0,1),c(0,0,34,1,16,0,3,0,1,1),c(0,0,1,58,0,1,3,1,0,0),c(0,3,16,0,181,6,6,0,2,2),c(1,2,0,1,6,56,2,1,0,1),c(3,1,3,3,6,2,129,0,0,1),c(0,0,0,1,0,1,0,13,0,1),c(1,0,1,0,2,0,0,0,70,0),c(0,1,1,0,2,1,1,1,0,85))
colnames(data)=letters[1:nrow(data)]
rownames(data)=colnames(data)
And with these commands I obtain the following heatmap:
library(reshape)
library(ggplot2)
data.m=melt(data)
data.m[,"rescale"]=round(rescale(data.m[,"value"]),3)
p=ggplot(data.m,aes(X1, X2))+geom_tile(aes(fill=rescale),colour="white")
p=p+scale_fill_gradient(low="white",high="black")
p+theme(text=element_text(size=10),axis.text.x=element_text(angle=90,vjust=0))
This is very similar to the plot on the left of Figure 1 above. The only differences are that (1) the nodes are not ordered randomly but alphabetically, and (2) instead of just having binary black/white pixels, I am using a "shades of grey" palette to be able to show the strength of the co-occurrence between nodes.
But the point is that it is very hard to distinguish any cluster structure (and this would be even more true with the full set of 100 nodes). So, I want to order my vertices by clusters on the heatmap. I have this membership vector from a community detection algorithm:
membership=c(1,2,4,2,5,3,1,2,2,3)
Now, how can I obtain a heatmap similar to the plot on the right of Figure 1 above?
Thanks a lot in advance for any help
PS: I have experimented R draw kmeans clustering with heatmap and R: How do I display clustered matrix heatmap (similar color patterns are grouped) but could not get what I want.
Turned out this was extremely easy. I am still posting the solution so others in my case don't waste time on that like I did.
The first part is exactly the same as before:
data.m=melt(data)
data.m[,"rescale"]=round(rescale(data.m[,"value"]),3)
Now, the trick is that the levels of the factors of the melted data.frame have to be ordered by membership:
data.m[,"X1"]=factor(data.m[,"X1"],levels=levels(data.m[,"X1"])[order(membership)])
data.m[,"X2"]=factor(data.m[,"X2"],levels=levels(data.m[,"X2"])[order(membership)])
Then, plot the heat map (same as before):
p=ggplot(data.m,aes(X1, X2))+geom_tile(aes(fill=rescale),colour="white")
p=p+scale_fill_gradient(low="white",high="black")
p+theme(text=element_text(size=10),axis.text.x=element_text(angle=90,vjust=0))
This time, the cluster is clearly visible.
I have generated a connectivity matrix representing a network of geographical points connected by ocean currents. Each point releases particles that are received by the others. The number of particles released and received by each point is summarized in this square matrix. For example an element Aij of the matrix correspond to the amount of particles emitted by the ith point and received by the jth.
My purpose is to be able to plot this as a network such that each point constitutes a vertex and the connections between two points constitute an edge. I would like those edges to be of different colors according to the amount of particles exchanged. Those have to be marked by an arrow.
I could plot those points according to their geographic coordinates and I could plot those edges the way I wanted. My only concern is now how to add a legend relating the color of the edges with the amount of particles they represent.
Can anyone help me with that? Here is my code so far:
library(ggplot2)
library(plyr)
library(sp)
library(statnet)
connectivityMatrix <- as.matrix(read.table(file='settlementMatrix001920.dat'))
coordinates <- as.matrix(read.table(file='NoTakeReefs_center_LonLat.dat'))
net <- as.network(connectivityMatrix, matrix.type = "adjacency", directed = TRUE)
minX<-min(coordinates[,1])#-0.5
maxX<-max(coordinates[,1])#+0.5
minY<-min(coordinates[,2])#-0.5
maxY<-max(coordinates[,2])#+0.5
p<-plot(net, coord=coordinates,xlim=c(minX,maxX),ylim=c(minY,maxY),edge.col=connectivityMatrix,object.scale=0.01)
without having your real data, here as a sample example
matrixValues<-matrix(c(0,1,2,3,
0,0,0,0,
0,0,0,0,
0,0,0,0),ncol=4)
net<-as.network(matrixValues)
plot(net,edge.col=matrixValues)
# plot legend using non-zero values from matrix
legend(1,1,fill = unique(as.vector(matrixValues[matrixValues>0])),
legend=unique(as.vector(matrixValues[matrixValues>0])))
you may have to adjust the first two coordinate values in legend to draw it where you need on the plot. You could also construct your network slightly differently so that the values were loaded in from the matrix (see the ignore.eval argument to as.network(). In which case you would use edge.col='myValueName' for the plot command and get.edge.attribute(net,'myValueName') to feed the values into legend.
I am trying to change the color of the vertexes in an igraph generated graph.
To be more specific, I have a 95 nodes graph created from an adjacency matrix and I would like to color them according to their degree/betweenness/eigenvalue centrality/closeness but I'm guessing that after I know how to do it with one, I'll be able to do it with others.
So I've coded the basics of graph generation until now:
dataset <- read.csv("~/Google Drive/Cours M2/Network Economics/Data/Collabs_2013.csv", sep=";")
matrix<-as.matrix(dataset)
adj<-graph.adjacency(matrix)
plot(adj)
btw<-betweenness(adj,directed = FALSE)
I now have a vector of 95 values of betweennesses and I would like to plot a graph with a gradient of colors that follows the betweenness values (e.g. from red for the lowest value to green to the highest). I'm guessing I have to mess with vertex's attributes but I have no idea how to input the vector as a color attribute.
Seems like you already did most of the work. All you have to know is colorRamppalette and setting the vertex.color for the network. Assuming you have to change the colors linearly,
just do
fine = 500 # this will adjust the resolving power.
pal = colorRampPalette(c('red','green'))
#this gives you the colors you want for every point
graphCol = pal(fine)[as.numeric(cut(btw,breaks = fine))]
# now you just need to plot it with those colors
plot(adj, vertex.color=graphCol)
credits to this. I was using a much more inefficient method to assign the colors before answering this.
Just a note:
It can be problematic to define
palette = colorRampPalette(c('blue','green'))
as the 'palette' function, is also used by igraph, and so igraph later produces as error.
See problem Color pallette for vertices in igraph network in R