Michael Gruenstaeudl
Michael Gruenstaeudl

Reputation: 1793

Adjusting visualization of igraph plot

Following the discussion here, I am using the igraph to visualize the association between the two variables (s and g).

In shell:

$ cat file
s   g
s1  foo bar
s2  foo bar baz qux
s99 foo
s9999   foo bar baz qux
s99999  foo
s999999 foo

In R:

m <- as.matrix(read.table(file="~/path_to_file/file", sep="\t", header=T))
g <- graph_from_edgelist(m)
V(g)$type <- bipartite.mapping(g)$type
coords <- layout_as_bipartite(g)
plot.igraph(g, layout = -coords[,2:1],
    vertex.shape="rectangle",
    vertex.size=10,
    vertex.size2=1,
    vertex.color=NA,
    vertex.frame.color=NA,
    vertex.label.color="black",
    vertex.label.family="sans",
    edge.label.color="white",
    edge.arrow.mode=0,
    edge.width=3,
    asp=5)

However, the resulting visualization has the vertex labels and the edges overlapping, making the former difficult to read.

enter image description here

I would like to adjust the R code such that all vertex labels are moved away from the vertex center (i.e., vertex labels of s moved to left, vertex labels of g moved to right) and that all vertex labels are left-aligned (and not centered, as is the default case).

Can I do that via igraph and if so, how? (If that were not possible, which alternative visualization strategy in R would you suggest?)

--

EDIT 1: A working solution would be able to handle a file with numerous verteces, such as file2:

In shell:

$ cat file2
s   g
foo1    bar01 baz qux
foo2    bar1 baz qux
foo3    bar1 baz qux
foo4    bar1 baz qux
foo5    bar1 baz qux
foo6    bar1 baz qux
foo7    bar1 baz qux
foo8    bar1 baz qux
foo9    bar1 baz qux
foo10   bar1 baz qux
foo11   bar02 baz
foo12   bar2 baz
foo13   bar2 baz
foo14   bar2 baz
foo15   bar2 baz
foo16   bar2 baz
foo17   bar2 baz
foo18   bar2 baz
foo19   bar2 baz
foo20   bar2 baz
foo21   bar03 baz baz qux
foo22   bar3 baz baz qux
foo23   bar3 baz baz qux
foo24   bar3 baz baz qux
foo25   bar3 baz baz qux
foo26   bar3 baz baz qux
foo27   bar3 baz baz qux
foo28   bar3 baz baz qux
foo29   bar3 baz baz qux
foo30   bar3 baz baz qux
foo31   bar04 baz baz qux quux
foo32   bar4 baz baz qux quux
foo33   bar4 baz baz qux quux
foo34   bar4 baz baz qux quux
foo35   bar4 baz baz qux quux
foo36   bar4 baz baz qux quux
foo37   bar4 baz baz qux quux
foo38   bar4 baz baz qux quux
foo39   bar4 baz baz qux quux
foo40   bar4 baz baz qux quux
foo41   bar05 baz qux quux
foo42   bar5 baz qux quux
foo43   bar5 baz qux quux
foo44   bar5 baz qux quux
foo45   bar5 baz qux quux
foo46   bar5 baz qux quux
foo47   bar5 baz qux quux
foo48   bar5 baz qux quux
foo49   bar5 baz qux quux
foo50   bar5 baz qux quux
foo51   bar06 baz qux
foo52   bar6 baz qux
foo53   bar6 baz qux
foo54   bar6 baz qux
foo55   bar6 baz qux
foo56   bar6 baz qux
foo57   bar6 baz qux
foo58   bar6 baz qux
foo59   bar6 baz qux
foo60   bar6 baz qux
foo61   bar07 baz qux quux
foo62   bar7 baz qux quux
foo63   bar7 baz qux quux
foo64   bar7 baz qux quux
foo65   bar7 baz qux quux
foo66   bar7 baz qux quux
foo67   bar7 baz qux quux
foo68   bar7 baz qux quux
foo69   bar7 baz qux quux
foo70   bar7 baz qux quux

Even when implementing the improvements suggested by G5W (i.e., adjusting the vertex width for each vertex individually; see variable Size1), the resulting graph remains difficult (or almost impossible) to visualize. Specifically, there does not appear to be a sweetspot between the aspect ratio, the vertex height and the vertex width.

In R:

Size1 = 12*nchar(V(g)$name)
plot.igraph(g, layout = -coords[,2:1],
    vertex.shape="rectangle",
    vertex.size=Size1,
    vertex.size2=5,
    vertex.color=NA,
    vertex.frame.color="green",
    vertex.label.color="black",
    vertex.label.family="sans",
    edge.label.color="white",
    edge.arrow.mode=0,
    edge.width=3,
    asp=2.5
    )

enter image description here

Upvotes: 3

Views: 1320

Answers (1)

G5W
G5W

Reputation: 37661

The biggest problem here is your vertex size. You can see that by changing your current plot statement so that it has vertex.frame.color="green". If you do that, you will see that the rectangular vertex is a tiny dot under the text. If there was a full size vertex containing the text, you could use a white background for the vertex to obscure the lines where the text is. When you change the vertex size, you will probably want to change the aspect ratio as well.

To make this work better, I used different size rectangles depending on the amount of text in the name. I am showing the result with the vertices filled in with white and a white frame, but please do try it with a green frame (in the code but commented out) so that you can see where the boxes are going. The boxes are needed to obscure the lines that go to the center of the vertex.

Size1 = 12*nchar(V(g)$name)
plot.igraph(g, layout = -coords[,2:1],
    vertex.shape="rectangle",
    vertex.size=Size1,
    vertex.size2=30,
    vertex.color=NA,
    ## vertex.frame.color="green",
    vertex.frame.color="white",
    vertex.label.color="black",
    vertex.label.family="sans",
    edge.label.color="white",
    edge.arrow.mode=0,
    edge.width=3,
    asp=1.5)

Rescaled

Edit: Based on edited question

With the new and bigger example, I do not think that you can get really good results, because you are trying to squeeze too much onto the screen. You are trying to display 70 nodes stacked on the left, so at best they each get 1/70th of the screen - not a lot of room. Below, I have reduced the font size, line width and the margins. I then readjusted the other parameters to squeeze as much as possible on the screen. This is only marginally satisfactory, but I don't think that you can get in much more without fundamentally redesigning the layout of the nodes. There simply is no more space in the left column.

New code

Size1 = 5.5*nchar(V(g)$name)
par(mar=rep(0.5,4))
plot.igraph(g, layout = -coords[,2:1],
    vertex.shape="rectangle",
    vertex.size=Size1,
    vertex.size2=5,
    vertex.color="white",
#    vertex.frame.color="green",
    vertex.frame.color="white",
    vertex.label.color="black",
    vertex.label.family="sans",
    edge.label.color="white",
    edge.arrow.mode=0,
    edge.width=1,
    asp=2.8,
    vertex.label.cex=0.55, 
    margin=-0.65
    )

Redrawn graph

Upvotes: 1

Related Questions