gpt4 book ai didi

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

转载 作者:行者123 更新时间:2023-12-03 19:36:03 25 4
gpt4 key购买 nike

我有以下数据框:

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 中旋转图像。)

最佳答案

我知道这个问题很老,但我希望这个答案能帮助到别人。

与其使用 layout_with_sugiyama,不如使用自定义布局。这样做并不难。你已经构建了layer 变量的水平位置。要获得垂直位置,我们需要按大小 (vertex_degree) 对顶点排序,然后允许形状与大小成比例,因此我们将在每一层内的 vertex_degrees 上使用 cumsum 设置高度。在我完成布局之后,对 plot 的复杂调用与你的相同,除了我用我的自定义布局交换了你给 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

关于r - 在三方 igraph 上的层内排序顶点,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/48490378/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com