首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >在ggraph和ggsave中截断的节点标签

在ggraph和ggsave中截断的节点标签
EN

Stack Overflow用户
提问于 2019-05-28 22:11:04
回答 1查看 566关注 0票数 0

当我使用ggraph绘制带有节点标签的网络图时,标签经常被截断。

代码语言:javascript
复制
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中改变绘图的宽度,我最终可以得到一个足够宽的绘图来包含整个标签。

代码语言:javascript
复制
ggsave("TestPlot1.jpg", plot = PlotTstGr)
ggsave("TestPlot2.jpg", plot = PlotTstGr, width = 15)

然后拉伸输出图TestPlot2.jpg。有没有办法防止节点标签被剪掉?我不知道问题出在ggraph还是ggplot2上。

EN

回答 1

Stack Overflow用户

发布于 2019-06-18 22:33:15

我解决了这个问题,在geom_node_label中,您可以使用选项repel =T

代码语言:javascript
复制
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对于保持节点在适当的位置很有用,但只是为了避免它们超出边界。

票数 0
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/56344154

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档