我正在尝试在ggraph
.
igraph
类对象是从下面的数据框中获得的:
edge <- data.frame(from=c(0,0,0,0,1,2,3),
to=c(0,1,2,3,0,0,0),
weight=c(1,3,1,1,3,1,1))
node <- data.frame(id=c(0,1,2,3),
p=c(9,1,0,0),
w=c(0,2,0,0),
s=c(0,0,1,1),
size=c(9,3,1,1),
gr=c(0,1,1,2))
# Convert data frame into igraph class object
net <- graph_from_data_frame(d=edge,vertices=node,directed=TRUE)
但是,当我在以下脚本中绘制图形时,它会产生意想不到的边缘宽度:
## Set arrows
ar <- arrow(angle=30,length=unit(5,"mm"),ends="last",type="closed")
## Plot
ggraph(net,layout="graphopt") +
## Edges
geom_edge_link(aes(width=weight,label=weight,
start_cap=circle(node1.size+1,unit="native"),
end_cap=circle(node2.size+1,unit="native")),
angle_calc="along",force_flip=TRUE,
label_dodge=unit(3.0,"mm"),label_push=unit(-0.4,"mm")) +
## Width scale
scale_edge_width(range=c(0.2,4),breaks=c(1:10),name="Movements\nbetween zones") +
## Add arrows separately
geom_edge_link(arrow=ar,aes(start_cap=circle(node1.size,unit="native"),
end_cap=circle(node2.size,unit="native"))) +
## Nodes
geom_node_circle(aes(r=size)) +
## Plot location id
geom_node_label(aes(label=id),color="red",repel=TRUE,label.r=0.3,position="identity") +
## Plot work activity
geom_node_text(aes(size=w),label="w",color="green",position="identity") +
## Plot school activity
geom_node_text(aes(size=s),label="s",color="blue",position="identity") +
## Theme
theme_graph() + coord_fixed()
有一些基本问题:
in 中定义的边的宽度weight
与节点 ID 不对应(from/to
inedge
和name
innode
)。例如,权重3
赋予数据框0->1
和1->0
数据框,但它出现0<->2
在图表之间,即使边距由and设置,之间的边也会0<->1
进入节点内部,0
start_cap
end_cap
Weight
除了边缘之外的标签不是集中的(它位于节点 0 附近)geom_node_label
由重叠节点 定义的标签- 节点 (
w
和s
) 内的标签大小似乎没问题,但它们的位置不正确dodge
(它们相互重叠)
第一点以外的问题都只是审美问题,但第一点是情节的致命错误。我绘制了它,igraph
其中给出了节点和边缘之间的适当关系。
## Sample in igraph
E(net)$width <- E(net)$weight
plot(net,edge.width=E(net)$weight*5,vertex.size=V(net)$size*5,vertex.label.cex=3)
我非常感谢您提出的解决上述任何问题以获得所需图表的建议。我是新手,ggraph
并找到了一些获取当前图表的解决方案,但是我目前一直在努力解决上述问题。(我更喜欢使用ggraph
而不是igraph
精确控制它的外观)
========== 更新 ==========
大多数问题可以通过以下脚本解决,尽管这是非常手动的调整。一个最重要的关键是去除0<->0
导致标签与边缘实际宽度不适当对应的循环边缘。
## Load data frames as tbl_graph class
edge <- edge %>% mutate(from=from+1,to=to+1)
net <- tbl_graph(nodes=node,edges=edge,directed=TRUE)
## Set arrows
ar <- arrow(angle=30,length=unit(5,"mm"),ends="last",type="closed")
## Plot
ggraph(net,layout="graphopt") +
## Edges
geom_edge_link(aes(start_cap=circle(log(node1.size)+2,unit="native"),
end_cap=circle(log(node2.size)+2,unit="native"),
width=weight,label=weight),
position="identity",angle_calc="along",force_flip=TRUE,
label_dodge=unit(4.0,"mm"),label_push=unit(-0.4,"mm")) +
## Width scale
scale_edge_width(range=c(0.2,4),breaks=c(1:10),name="Movements\nbetween zones") +
## Add arrows separately
geom_edge_link(arrow=ar,aes(start_cap=circle(log(node1.size)+1,unit="native"),
end_cap=circle(log(node2.size)+1,unit="native"))) +
## Nodes
geom_node_circle(aes(r=log(size)+1)) +
## Plot location id
geom_node_label(aes(label=id,hjust=log(size+5),vjust=log(size+5)),repel=TRUE,
label.padding=unit(0.8,"mm"),label.r=unit(0.0,"mm"),label.size=0.1,size=3.5) +
## Plot work activity
geom_node_text(aes(size=w,hjust=log(w)+0.6),label="w",color="red",position="identity",vjust=0.4) +
## Plot school activity
geom_node_text(aes(size=s,hjust=-log(s)-0.3),label="s",color="blue",position="identity",vjust=0.4) +
## Size scale
scale_size(range=c(0,5),breaks=c(1:100),name="Numberof\nActivities",
guide=guide_legend(override.aes=list(rep("a",100)))) +
# scale_color() +
## Theme
theme_graph() + coord_fixed()
然而,关于图例还有两个问题:
- 如何将重叠的“w”和“s”替换为其他字体,例如“a”(活动)?
- 如何在所需结果中再添加一个手动图例“活动类型”?
我把它作为一个新问题发布在这里