Как нарисовать стрелку в середине края в ggraph - PullRequest
2 голосов
/ 04 октября 2019

Можно ли нарисовать стрелку в середине ребра, используя 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()

Rplot.png

Я бы хотел добиться чего-то подобного:

Rplot01.png

Я проверил документацию ggraph::geom_edge_link() и grid::arrow(), но не увидел ничего очевидного в том, как это сделать.

1 Ответ

2 голосов
/ 04 октября 2019

Я сам не использовал пакет ggraph, но, основываясь на моем понимании основных гробов, вы можете попробовать следующее:

Шаг 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.

result

Этот эффект останется в силе до конца текущего сеанса R (т.е. все сегменты со стрелкамисозданные ggraph имеют стрелки в середине, а не в конце), пока не запустите следующую строку:

untrace(ggraph:::cappedPathGrob)

После этого возобновится нормальное поведение.

...