【问题标题】:How to draw an arrowhead on a specific position on an edge?如何在边缘的特定位置绘制箭头?
【发布时间】:2022-12-17 04:53:15
【问题描述】:

我有一个图表,其中每条边都是其节点之间的所有权分布。例如,“A”和“B”之间的边,“A”拥有 90%,“B”仅拥有 10%。我想通过在该所有权的相对位置的边缘放置一个圆弧来形象化这一点。我怎样才能做到这一点?我更喜欢使用 ggraph 并使用箭头来可视化相对所有权,但我愿意接受其他建议。

默认情况下,圆弧位于边的末端。例如下面创建下图。

library(ggraph)
library(ggplot2)

# make edges
edges = data.frame(from = c("A", "B", "C"),
                   to = c("C","A", "B"),
                   relative_position = c(.6,.1, .4))

# create graph
graph <- as_tbl_graph(edges)

# plot using ggraph
ggraph(graph) + 
  geom_edge_link(
    arrow = arrow()
  ) + 
  geom_node_label(aes(label = name))

我想要的是像下面这样的东西。我发现 this 讨论将箭头移动到边缘的中心,但据我所知,这种方法不适用于设置相对位置。

【问题讨论】:

    标签: r ggplot2 ggraph


    【解决方案1】:

    我建议叠加两个 geom_link_edges - 第一个具有节点之间的完整链接,第二个具有末端带有箭头的部分链接。我能够像这样用你的例子实现这个:

    library(ggraph)
    library(ggplot2)
    
    # make edges
    edges = data.frame(from = c("A", "B", "C"),
                       to = c("C","A", "B"),
                       relative_position = c(.6,.1, .4))
    
    # create graph
    graph <- as_tbl_graph(edges)
    
    # plot using ggraph
    ggraph(graph) + 
      geom_edge_link() +
      #this the partial link with the arrow - calculate new end coordinates
      geom_edge_link(aes(xend=((xend-x)*relative_position)+x,
                         yend=((yend-y)*relative_position)+y)
                      arrow = arrow()) + 
      geom_node_label(aes(label = name))
    

    【讨论】:

      最近更新 更多