[英]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
). 在这里进行讨论之后,我使用
igraph
可视化两个变量( s
和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
: 在
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. 但是,所得到的可视化效果具有顶点标签和边缘重叠,从而使前者难以阅读。
I would like to adjust the R code such that all vertex labels are moved away from the vertex center (ie, 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). 我想调整R代码,以使所有顶点标签都移离顶点中心(即
s
顶点标签移到左侧, g
顶点标签移到右侧),并且所有顶点标签都向左对齐(并且不居中(默认情况下)。
Can I do that via igraph
and if so, how? 我可以通过
igraph
吗? (If that were not possible, which alternative visualization strategy in R
would you suggest?) (如果不可能的话,您会建议使用
R
另一种可视化策略吗?)
-- -
EDIT 1 : A working solution would be able to handle a file with numerous verteces, such as file2
: 编辑1 :一个有效的解决方案将能够处理具有众多顶点的文件,例如
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 (ie, adjusting the vertex width for each vertex individually; see variable Size1
), the resulting graph remains difficult (or almost impossible) to visualize. 即使实施G5W建议的改进(即,分别调整每个顶点的顶点宽度;请参见变量
Size1
),结果图仍然很难(或几乎不可能)可视化。 Specifically, there does not appear to be a sweetspot between the aspect ratio, the vertex height and the vertex width. 具体而言,在长宽比,顶点高度和顶点宽度之间似乎没有最佳点。
In R
: 在
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
)
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"
. 您可以通过更改当前的plot语句使其具有
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)
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.
您试图显示堆叠在左侧的70个节点,因此充其量最多只能获得屏幕的1/70-而不是很大的空间。 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.
左栏中根本没有更多空间。
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
)
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.