如何在边缘的特定位置绘制箭头?

问题描述 投票:0回答:1

我有一个图,其中每条边都是其节点之间的所有权分布。例如,在“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个回答
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))
© www.soinside.com 2019 - 2024. All rights reserved.