【问题标题】:Node Labels cut off in ggraph and ggsave节点标签在 ggraph 和 ggsave 中被截断
【发布时间】:2019-05-28 14:11:03
【问题描述】:

当我使用 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。

【问题讨论】:

    标签: r ggplot2


    【解决方案1】:

    我想通了,在 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 对于保持节点到位很有用,但只是为了避免它们超出边界。

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 2020-11-26
      • 2018-04-20
      • 2021-11-18
      • 1970-01-01
      • 2019-03-24
      • 1970-01-01
      • 1970-01-01
      • 2019-04-12
      相关资源
      最近更新 更多