自定义`GeomLink`函数找不到在`setup_data`内部计算的列

时间:2019-04-10 15:25:41

标签: r ggplot2 ggproto

我有一个data.frame,看起来像:

  type id   x    y                    label from to polarity group
1 link  3 560 -219                     <NA>    2  1        +     1
8  var  2 426 -276    hours worked per week   NA NA     <NA>     1
7  var  1 610 -226 accomplishments per week   NA NA     <NA>     1

和函数link_coordinates,该函数通过查找类型为{{1}的链接元素的坐标来计算类型为from_x/from_y的所有元素的to_x/to_ylink坐标}。链接由varfrom表示:

to

我可以预先计算这些坐标,然后绘制链接:

link_coordinates <- function(cld) {
  cld$from_x <- NA; cld$from_y <- NA; cld$to_x <- NA; cld$to_y <- NA
  for(i in 1:nrow(cld)) {
    if(cld$type[i] == "link") {
      cld$from_x[i] <- cld$x[cld$id == cld$from[i]]
      cld$from_y[i] <- cld$y[cld$id == cld$from[i]]
      cld$to_x[i] <- cld$x[cld$id == cld$to[i]]
      cld$to_y[i] <- cld$y[cld$id == cld$to[i]]
    }
  }
  return(cld)
}

enter image description here


现在,我想在名为library(ggplot2) cld_linked <- link_coordinates(cld) ggplot(as.data.frame(cld_linked)) + geom_curve(aes(x = from_x, y = from_y, xend = to_x, yend = to_y)) 的定制Geom中进行该预计算(以及其他工作)。这样做时,我得到一个错误,而不是一个图:

GeomLink
  

ggplot(as.data.frame(cld)) + geom_link()


为什么,为什么如何必须更改我的Error in FUN(X[[i]], ...) : object 'from_x' not foundGeomLink函数,以便它们从上方生成图?


这是我的geom_link

data.frame

这是我的cld <- structure(list(type = c("link", "var", "var"), id = c(3, 2, 1 ), x = c(560, 426, 610), y = c(-219, -276, -226), label = c(NA, "hours worked per week", "accomplishments per week"), from = c(2, NA, NA), to = c(1L, NA, NA), polarity = c("+", NA, NA), group = c(1L, 1L, 1L)), .Names = c("type", "id", "x", "y", "label", "from", "to", "polarity", "group"), row.names = c(1L, 8L, 7L), class = "data.frame") GeomLink函数:

geom_link

0 个答案:

没有答案