I have a data.frame
that looks like:
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
and a function link_coordinates
that calculates the from_x/from_y
and to_x/to_y
coordinates for all elements of type link
by looking up the coordinates of linked elements of type var
. Links are indicated by from
and 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)
}
I can precalculate those coordinates and then plot the link:
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))
Now I want to do that precalculation (together with other stuff) inside a custom made Geom
called GeomLink
. When doing this, instead of a plot I get an error:
ggplot(as.data.frame(cld)) + geom_link()
Error in FUN(X[[i]], ...) : object 'from_x' not found
Why is that and how must I change my GeomLink
and geom_link
functions in order they produce the plot from above?
Here is my 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")
and here are my GeomLink
and geom_link
functions:
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, ...))
}