除了这个帖子 https://stackoverflow.com/questions/51569831/how-to-draw-graph-corresponding-with-its-attributes-in-ggraph-r,在这里我提出新问题如何调整由ggraph
.
以下是生成当前绘图的脚本:
## Packages
library(igraph)
library(tidygraph)
library(ggraph)
library(ggplot2)
library(tidyverse)
## Edge and node
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,1,1,1),
size=c(9,3,1,1),
gr=c(0,1,1,2))
## 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.4,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
## Plot location id
geom_node_label(aes(label=id,hjust=log(size+6),vjust=log(size+6)),repel=TRUE,
label.padding=unit(0.8,"mm"),label.r=unit(0.0,"mm"),label.size=0.1,size=3.5) +
## Circle
geom_node_circle(aes(r=log(size)+1),color="black",fill="white",size=0.4) +
## Plot work activity
geom_node_text(aes(size=w,hjust=log(w)+0.4),label="w",color="red",
position="identity",vjust=0.4,fontface="bold") +
## Plot school activity
geom_node_text(aes(size=s,hjust=-log(s)-0.2),label="s",color="blue",
position="identity",vjust=0.4,fontface="bold") +
## Size scale
scale_size(range=c(0,5),breaks=c(1:100),name="Numberof\nActivities",
guide=guide_legend(override.aes=list(label="a",color="black"))) +
# scale_color() +
## Theme
theme_graph() + coord_fixed()
这就是我想要的结果。
这是上述脚本的结果。
要达到我想要的结果,有两个与图例相关的问题:
-
如何用其他字体替换“活动数量”中重叠的“w”和“s”,例如黑色的“a”(活动),如所需结果所示?
- 如何添加所需结果中显示的另一手动图例“活动类型”?
虽然它写在ggraph
,我想语法似乎与ggplot2
。我很感激你的建议!
==========更新==========
第一个问题已通过更改解决scale_size
参数如更新的脚本中所示。
然而,第二个问题仍然存在,我又发现了一个问题0-value
符号位于圆圈中心(非常小的点)。我找到了一些解决方案来解决该问题,方法是应用 1)subset()
省略0-value
数据框中的记录data=subset(df,x!=0)
, 2) 只需用 NA 填充 0 值即可data=ifelse(x==0,NA,.)
方法,但是正如它所写的那样graph
对象,它可能不适用于这种情况。
========== 更新版本 2 ==========
这确实很棘手,但不知怎的,我可以删除线中心的点并添加一个图例。
我真的不确定原因,但前一个问题已通过 1) 替换解决0 values
in node
数据帧由NA
, 2) 替换NA
in aes(size=...)
geom_node_text 中的语句为aes(size=ifelse(is.na(w),0,w) ...
,然后 3) 指定颜色color=ifelse(w==0,"white","red")
。它不会删除数据,只是改变颜色0-value
变成白色,使其看不见。
我想这不是正式的解决方案,只是我在数十次试验中遇到的方式。有趣的是 1) 和 2) 应该结合起来,否则会返回错误。
因此,这是最终更新:
## Packages
library(igraph)
library(tidygraph)
library(ggraph)
library(ggplot2)
library(tidyverse)
## Edge and node
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,1,1,1),
size=c(9,3,1,1),
gr=c(0,1,1,2))
## Load data frames as tbl_graph class
edge <- edge %>% mutate(from=from+1,to=to+1)
# THIS IS QUITE STRANGE OPERATION FOR CHANGING COLOR
node <- node %>% mutate_at(vars(p,w,s,size),funs(ifelse(.==0,NA,.)))
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.4,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
## Plot location id
geom_node_label(aes(label=id,hjust=log(size+6),vjust=log(size+6)),repel=TRUE,
label.padding=unit(0.8,"mm"),label.r=unit(0.0,"mm"),label.size=0.1,size=3.5) +
## Circle
geom_node_circle(aes(r=log(size)+1),color="black",fill="white",size=0.4) +
## Plot work activity: NOT KNOW WHY IT WORKS!!
geom_node_text(aes(size=ifelse(is.na(w),0,w),hjust=log(w)+0.4,color=ifelse(w==0,"white","red")),label="w",
position="identity",vjust=0.4,fontface="bold") +
## Plot school activity
geom_node_text(aes(size=ifelse(is.na(s),0,s),hjust=-log(s)-0.2,color=ifelse(s==0,"white","blue")),label="s",
position="identity",vjust=0.4,fontface="bold") +
## Edit legend
scale_size_continuous(range=c(0,5),breaks=c(1:100),name="Numberof\nActivities",
guide=guide_legend(override.aes=list(label="a",color="black"))) +
scale_color_manual(name="Type of Activity",guide=guide_legend(override.aes=list(label=c("w","s"))),
values=c("red","blue"),labels=c("Work","School")) +
## Theme
theme_graph() + coord_fixed()
然而,由于它仍然是丑陋的代码,我仍在等待您的建议以进行更合适的操作。