除了这篇文章,我在这里提出新的问题,如何调整由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”(活动),如所需结果所示?
- 如何在所需结果中再添加一个手动图例“活动类型”?
虽然是用 in 写的ggraph
,但我想语法似乎与 in 相似ggplot2
。我很欣赏你的建议!!
========== 更新 ==========
第一个问题已通过更改scale_size
参数解决,如更新的脚本所示。
但是,第二个问题仍然存在,我0-value
在圆心(非常小的点)的符号上发现了另一个问题。我找到了一些解决方案来解决这个问题,方法是应用 1)subset()
省略0-value
数据框中的记录data=subset(df,x!=0)
,2) 在方法中简单地用 NA 填充 0 值data=ifelse(x==0,NA,.)
,但是由于它是从graph
对象写入的,因此它可能不适用于这种简单的情况。
========== UPDATE ver.2 ==========
这真的很棘手,但不知何故我可以删除线中心的点以及添加一个图例。
我真的不确定原因,但前一个问题通过 1)0 values
在node
数据框中NA
替换为 2)在 geom_node_textNA
中的语句中替换为,然后 3)通过指定颜色来解决。它不会删除数据,只是将颜色更改为白色以使其不可见。
我想这不是正式的解决方案,而只是我在数十次试验中遇到的方式。有趣的是 1) 和 2) 应该结合起来,否则它会返回错误。 aes(size=...)
aes(size=ifelse(is.na(w),0,w) ...
color=ifelse(w==0,"white","red")
0-value
因此,这是最后的更新:
## 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()
但是,由于它仍然是丑陋的代码,我仍在等待您对更合适的操作的建议。