R 自定义“GeomLink”函数找不到在“setup_data”中计算的列

R 自定义“GeomLink”函数找不到在“setup_data”中计算的列,r,ggplot2,ggproto,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 -22

我有一个
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
我可以预先计算这些坐标,然后绘制链接:

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))
FUN中的错误(X[[i]],…):未找到对象“from_X”


这是为什么?我必须如何更改我的
GeomLink
GeomLink
函数才能从上面生成绘图?


这是我的
数据框

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")

geom\u link
不知道
cld
。只需尝试调试(geom_链接);ggplot(cld)+geom_link()@Christoph:Thx供您评论。这实际上是问题中的一个输入错误。我抛出错误的真正代码是:
ggplot(as.data.frame(cld))+geom_link()
。刚刚编辑。因此,问题出在其他地方,因为
cld
不再是
cld
,而只是一个
数据帧
。如果出现问题,
geom\u link
不知道
cld
。只需尝试调试(geom_链接);ggplot(cld)+geom_link()@Christoph:Thx供您评论。这实际上是问题中的一个输入错误。我抛出错误的真正代码是:
ggplot(as.data.frame(cld))+geom_link()
。刚刚编辑。因此,问题出在其他地方,因为
cld
不再是
cld
,而只是一个
数据帧
。有那个问题吗
ggplot(as.data.frame(cld)) + geom_link() 
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 <- ggplot2::ggproto("GeomLink", ggplot2::GeomCurve,
                             required_aes = c("id", "x", "y", "from", "to", "polarity", "type", "group"),
                             default_aes = ggplot2::aes(id = id, x = x, y = y, xend = x, yend = y, from = from, to = to, polarity = polarity, type = type, group = group, colour = "black",
                                                        size = 4, angle = 0, hjust = 0.5, vjust = 0.5,
                                                        alpha = NA, fontface = 1,
                                                        lineheight = 1.2, length = 10),
                             setup_data = function(data, params){
                               data <- link_coordinates(data)
                               data <- data[data$type == "link", ]
                               print(data)
                             })


geom_link <- function(mapping = ggplot2::aes(x = from_x, y = from_y, xend = to_x, yend = to_y, id = id, from = from, to = to, polarity = polarity, type = type), data = NULL, position = "identity", stat = "identity",
                      ..., curvature = 0.2, angle = 90, ncp = 5, arrow = NULL,
                      arrow.fill = NULL, lineend = "butt", na.rm = FALSE, show.legend = NA,
                      inherit.aes = TRUE)
{
  ggplot2::layer(data = data, mapping = mapping, stat = stat, geom = GeomLink,
                 position = position, show.legend = show.legend, inherit.aes = inherit.aes,
                 params = list(arrow = arrow, arrow.fill = arrow.fill,
                               curvature = curvature, angle = angle, ncp = ncp,
                               lineend = lineend, na.rm = na.rm, ...))
}