当我使用ggraph绘制带有节点标签的网络图时,标签经常被截断。
library(igraph)
library(ggraph)
#generate a sample network
tstGr <- erdos.renyi.game(6, 0.5, type=c("gnp", "gnm"), directed = TRUE, loops = FALSE)
#Assign labels to the nodes
V(tstGr)$name = c("LongName1", "LongName2", "LongName3", "LongName4", "LongName5", "LongName6")
#Plotting function
PfuncLite = function(Gr){
p = ggraph(Gr, layout = "circle")+
geom_edge_fan(color = "#971b2f", alpha = 0.5, arrow = arrow(ends = "last", length = unit(0.15, "inches")), start_cap = circle(6, 'mm'), end_cap = circle(6, 'mm')) +
geom_node_label(aes(label = V(Gr)$name), color = "#002f6c") +
theme_graph()+
ggtitle("Interaction Network")
return(p)
}
PlotTstGr = PfuncLite(tstGr)
PlotTstGr如果我在ggsave中改变绘图的宽度,我最终可以得到一个足够宽的绘图来包含整个标签。
ggsave("TestPlot1.jpg", plot = PlotTstGr)
ggsave("TestPlot2.jpg", plot = PlotTstGr, width = 15)然后拉伸输出图TestPlot2.jpg。有没有办法防止节点标签被剪掉?我不知道问题出在ggraph还是ggplot2上。
发布于 2019-06-18 22:33:15
我解决了这个问题,在geom_node_label中,您可以使用选项repel =T
PfuncLite = function(Gr){
p = ggraph(Gr, layout = "circle")+
geom_edge_fan(color = "#971b2f", alpha = 0.5, arrow = arrow(ends = "last", length = unit(0.15, "inches")), start_cap = circle(6, 'mm'), end_cap = circle(6, 'mm')) +
geom_node_label(aes(label = V(Gr)$name), color = "#002f6c", repel = T) +
theme_graph()+
ggtitle("Interaction Network")
return(p)
}这是对ggrepel的调用,因此您可以在那里使用其他选项。我发现point.padding = NA、box.padding = 0、force = 0.1对于保持节点在适当的位置很有用,但只是为了避免它们超出边界。
https://stackoverflow.com/questions/56344154
复制相似问题