在三方igraph上的层内排序顶点

时间:2018-01-28 19:21:04

标签: r igraph

我有以下数据框:

df<-data.frame(consumed= c("level1_plt1", "level1_plt2", "level1_plt3", "level1_plt3","level1_plt2","level1_plt4","level1_plt5","level1_plt5","level1_plt6","level1_plt7","level1_plt8","level1_plt9","level1_plt10","level1_plt10","level1_plt1","level1_plt1","level1_plt6","level1_plt6","level1_plt9","level1_plt9","level1_plt11","level1_plt11","level1_plt11","level2_lep1","level2_lep4","level2_lep3"),consumer=c("level2_lep1","level2_lep2","level2_lep3","level2_lep2","level2_lep4", "level2_lep4","level2_lep5","level2_lep5","level2_lep6","level2_lep7","level2_lep8","level2_lep9","level2_lep10","level2_lep10","level2_lep8","level2_lep8","level2_lep1","level2_lep1","level2_lep3","level2_lep11","level2_lep12","level2_lep13","level2_lep13", "level3_pst1","level3_pst3","level3_pst4"))

并且已经执行以下步骤以获得igraph三方输出:

links<- 
df%>%
group_by(consumed, consumer) %>% 
summarize(freq=n())

g<- graph_from_data_frame(d=links,directed=FALSE)

layer <- rep(2, length(V(g)$name))
layer[grepl("level1_",V(g)$name)]=1
layer[grepl("level3_",V(g)$name)]=3


names<- V(g)$name
names<-sub("level2_","", names)
names<-sub("level3_","", names)
names<-sub("level1_","", names)
V(g)$name = names


layout = layout_with_sugiyama(g, layers=layer)
E(g)$width <- E(g)$freq
V(g)$vertex_degree <-  degree(g)*7
plot(g,
 layout=cbind(layer,layout$layout[,1]),edge.curved=0,
 vertex.shape=c("square","circle","square")[layer],
 vertex.frame.color = c("darkolivegreen","darkgoldenrod","orange3")
[layer],
 vertex.color=c("olivedrab","goldenrod1","orange1")[layer],
 vertex.label.color="white",
 vertex.label.font=2,
 vertex.size=V(g)$vertex_degree,
 vertex.label.dist=c(0,0,0)[layer],
 vertex.label.degree=0, vertex.label.cex=0.5)

enter image description here

如果可能的话,我想做两件事来调整图片:

  1. 将图层从最大的形状(最高度)到最小的形状(最小的度数)排序。例如,在绿色层中,顺序可以如下:plt9,plt3,plt2,plt11,plt6,plt1,plt7,plt5,plt4,plt10,plt8。

  2. 在形状之间创建空间,使其不重叠(例如lep3和lep4)。我喜欢当前的尺寸/比例,所以我反对将形状缩小以在形状之间创造空间。

  3. 将图形和顶点字体逆时针旋转90度,使其从下到上依次为绿色层 - >黄色层 - >橙色层。 (我想它总是一个旋转顶点文本的选项,我可以用word或ppt旋转图像。)

1 个答案:

答案 0 :(得分:0)

我知道这个问题很旧,但是我希望答案会有所帮助。

与其使用layout_with_sugiyama,不如使用此方法最简单 自定义布局。这样做并不难。您已经构造了 layer变量的水平位置。要获得垂直位置, 我们需要按大小(vertex_degree)对顶点进行排序,然后允许形状与大小成比例,因此我们将在每一层的vertex_degrees上使用cumsum设置高度。在完成布局后,对绘图的复杂调用与您的相同,只不过 我将我的自定义布局换成您打给sugiyama的电话。

MyLO = matrix(0, nrow=vcount(g), ncol=2)

## Horizontal position is determined by layer
MyLO[,1] = layer

## Vertical position is determined by sum of sorted vertex_degree
for(i in 1:3) {
    L  = which(layer ==i)
    OL = order(V(g)$vertex_degree[L], decreasing=TRUE)
    MyLO[L[OL],2] = cumsum(V(g)$vertex_degree[L][OL])
}

plot(g, 
 layout=MyLO,  edge.curved=0,
 vertex.shape=c("square","circle","square")[layer],
 vertex.frame.color = c("darkolivegreen","darkgoldenrod","orange3")[layer],
 vertex.color=c("olivedrab","goldenrod1","orange1")[layer],
 vertex.label.color="white",
 vertex.label.font=2,
 vertex.size=V(g)$vertex_degree,
 vertex.label.dist=0,
 vertex.label.degree=0, vertex.label.cex=0.5)

plot with layers ordered by vertex degree