3

I'm trying to visualize the connections between the institutions in a medical faculty and just can't get the edges to be weighted and displayed thicker or thinner depending on the number of connections.

I've tried to combine the answers I found here playing around with edge.width = E(g)$weight and trying graph.strength(g). But honestly I have no idea what I'm doing. This is the first time I have to use R and I have no experience in programming whatsoever.

library(igraph)
D3 <- read.csv(file.choose(),header=TRUE,row.names = 1)
g <- graph.data.frame(D3, directed=FALSE)
plot(g, 
     vertex.size=20, 
     vertex.label.dist=1, 
     vertex.label.degree=-pi/2, 
     layout=layout_with_kk)

Igraph plots a network where every single connection is shown. Some institutions have multiple connections between each other which make the graph quite unattractive to look at. Only a Part of the table was used for this picture

My data looks like this and has about 1500 rows:

"1","NEUROLOGIE","MEDINF" 

my data

Any help is much appreciated!

Jay
  • 33
  • 4
  • 1
    [See here](https://stackoverflow.com/questions/5963269/how-to-make-a-great-r-reproducible-example) on making an R question that folks can help with. That includes a sample of data, all necessary code, and a clear explanation of what you're trying to do and what hasn't worked. Right now we have neither your data (1 row isn't much to do graph analysis with) nor your output ("unattractive" is subjective, and we can't see it anyway). – camille May 18 '19 at 14:46

2 Answers2

1

Using edge.width = E(g)$weight is the right idea, but you need to get the right weight. graph.strength(g) is a property of the vertices, but you need a weight for the edges. I don't know of a function that directly calculates how many edges there are between two vertices, but it is not hard to write one.

First, get a version of the graph with just one edge between each pair of connected vertices.

g2 = simplify(g)

Now we need to get the right weight for the edges of g2. If an edge connects two vertices, all shortest paths connecting those two vertices will be single edges, so for each edge of the simplified g2, we need to find the number of shortest paths (edges) between those vertices in the original g. Then we can plot.

E(g2)$weight = sapply(E(g2), function(e) { 
    length(all_shortest_paths(g, from=ends(g2, e)[1], to=ends(g2, e)[2])$res) } )
plot(g2, 
     vertex.size=15, 
     vertex.label.dist=0.5, 
     vertex.label.cex=0.8,
     vertex.label.degree=-pi/2, 
     edge.width=E(g2)$weight,
     layout=layout_with_kk,
     margin=-0.2)

Graph with varying edge width

(I have slightly modified your plot statement to improve readability.)

G5W
  • 36,531
  • 10
  • 47
  • 80
0

Thank you so much for your help!! I was nowhere close to that.. To make it more readble I reduced the thickness of the edges and replaced the names with number, this is the code:

library(igraph)
D3 <- read.csv(file.choose(),header=TRUE,row.names = 1)
g <- graph.data.frame(D3, directed=FALSE)
g2 = simplify(g)

E(g2)$weight = sapply(E(g2), function(e) { 
    length(all_shortest_paths(g, from=ends(g2, e)[1], to=ends(g2, e)[2])$res) } )

tkplot(g2,
       vertex.color= "gold",
       vertex.label.color="red",
       vertex.size=10, 

       vertex.label.cex=1,


       edge.width=E(g2)$weight*0.15,
       edge.color="grey",
       layout=layout.reingold.tilford,
       asp = .5,
       margin=-0.95)

Creating: Reingold.tilford

I find this visualization quite fine because the graph is interactive. Are there other ways to make it even more readable?

Thanks again for the help! All the best, Jay

Jay
  • 33
  • 4