是否可以使用ggraph::geom_edge_link()在边缘的中间绘制箭头,如果可以的话,该怎么做?

而不是像这样在边缘的末端绘制箭头:

library(ggraph)
library(tidygraph)
library(dplyr)

create_notable('bull') %>%
  ggraph(layout = 'graphopt') +
  geom_edge_link(arrow = arrow(length = unit(4, 'mm')),
                 end_cap = circle(3, 'mm')) +
  geom_node_point(size = 5) +
  theme_graph()

r - 如何在ggraph的边缘中间绘制箭头-LMLPHP

我希望能够实现以下目标:

r - 如何在ggraph的边缘中间绘制箭头-LMLPHP

我已经检查了ggraph::geom_edge_link()grid::arrow()文档,但看不到有关如何执行此操作的任何明显提示。

最佳答案

我自己没有使用过ggraph包,但是基于对底层grobs的了解,您可以尝试以下操作:

步骤1 。在控制台中运行以下行:

trace(ggraph:::cappedPathGrob, edit = TRUE)

步骤2 。在弹出窗口中,从此更改最后的代码块:
if (is.null(start.cap) && is.null(end.cap)) {
  if (constant) {
    grob(x = x, y = y, id = id, id.lengths = NULL, arrow = arrow,
         name = name, gp = gp, vp = vp, cl = "polyline")
  }
  else {
    grob(x0 = x[!end], y0 = y[!end], x1 = x[!start],
         y1 = y[!start], id = id[!end], arrow = arrow,
         name = name, gp = gp, vp = vp, cl = "segments")
  }
} else {
  gTree(x = x, y = y, id = id, arrow = arrow, constant = constant,
        start = start, end = end, start.cap = start.cap,
        start.cap2 = start.cap2, start.captype = start.captype,
        end.cap = end.cap, end.cap2 = end.cap2, end.captype = end.captype,
        name = name, gp = gp, vp = vp, cl = "cappedpathgrob")
}

对此:
if(is.null(arrow)) {
  # same code as before, if no arrow needs to be drawn
  if (is.null(start.cap) && is.null(end.cap)) {
    if (constant) {
      grob(x = x, y = y, id = id, id.lengths = NULL, arrow = arrow,
           name = name, gp = gp, vp = vp, cl = "polyline")
    }
    else {
      grob(x0 = x[!end], y0 = y[!end],
           x1 = x[!start], y1 = y[!start],
           id = id[!end], arrow = arrow,
           name = name, gp = gp, vp = vp, cl = "segments")
    }
  } else {
    gTree(x = x, y = y, id = id, arrow = arrow, constant = constant,
          start = start, end = end, start.cap = start.cap,
          start.cap2 = start.cap2, start.captype = start.captype,
          end.cap = end.cap, end.cap2 = end.cap2, end.captype = end.captype,
          name = name, gp = gp, vp = vp, cl = "cappedpathgrob")
  }
} else {
  # split x/y/ID values corresponding to each ID into two halves; first half to
  # end with the specified arrow aesthetics; second half (with a repetition of the
  # last value from first half, so that the two halves join up) has arrow set to NULL.
  id.split = split(id, id)
  id.split = lapply(id.split,
                    function(i) c(rep(TRUE, ceiling(length(i)/2)),
                                  rep(FALSE, length(i) - ceiling(length(i)/2))))
  id.split = unsplit(id.split, id)
  id.first.half = which(id.split == TRUE)
  id.second.half = which(id.split == FALSE |
                           (id.split == TRUE & c(id.split[-1], FALSE) == FALSE))

  if (is.null(start.cap) && is.null(end.cap)) {
    if (constant) {
      gList(grob(x = x[id.first.half], y = y[id.first.half], id = id[id.first.half],
                 id.lengths = NULL, arrow = arrow,
                 name = name, gp = gp, vp = vp, cl = "polyline"),
            grob(x = x[id.second.half], y = y[id.second.half], id = id[id.second.half],
                 id.lengths = NULL, arrow = NULL,
                 name = name, gp = gp, vp = vp, cl = "polyline"))

    }
    else {
      # I haven't modified this chunk as I'm not familiar with ggraph,
      # & haven't managed to trigger constant == FALSE condition yet
      # to test out code modifications here
      grob(x0 = x[!end], y0 = y[!end],
           x1 = x[!start], y1 = y[!start],
           id = id[!end], arrow = arrow,
           name = name, gp = gp, vp = vp, cl = "segments")
    }
  } else {
    gList(gTree(x = x[id.first.half], y = y[id.first.half], id = id[id.first.half],
                arrow = arrow, constant = constant,
                start = start, end = end, start.cap = start.cap,
                start.cap2 = start.cap2, start.captype = start.captype,
                end.cap = end.cap, end.cap2 = end.cap2, end.captype = end.captype,
                name = name, gp = gp, vp = vp, cl = "cappedpathgrob"),
          gTree(x = x[id.second.half], y = y[id.second.half], id = id[id.second.half],
                arrow = NULL, constant = constant,
                start = start, end = end, start.cap = start.cap,
                start.cap2 = start.cap2, start.captype = start.captype,
                end.cap = end.cap, end.cap2 = end.cap2, end.captype = end.captype,
                name = name, gp = gp, vp = vp, cl = "cappedpathgrob"))
  }
}

步骤3 。按照正常运行ggraph代码:
set.seed(777) # set seed for reproducibility

create_notable('bull') %>%
  ggraph(layout = 'graphopt') +
  geom_edge_link(arrow = arrow(length = unit(4, 'mm')),
                 end_cap = circle(0, 'mm')) +
  geom_node_point(size = 5) +
  theme_graph()

# end_cap parameter has been set to 0 so that the segments join up;
# you can also refrain from specifying this parameter completely.

r - 如何在ggraph的边缘中间绘制箭头-LMLPHP

在您运行以下行之前,此效果将在您当前的R session 的其余部分中保持不变(即,由ggraph创建的所有带箭头的片段的箭头位于中间而不是结尾):
untrace(ggraph:::cappedPathGrob)

此后,将恢复正常行为。

10-04 10:11