I have 3 vertices represented as points in a plot connected with three edges. I'd like to bend the the edges towards the center of the triangle (c(.5, .35)
). How can I turn graph 1 into graph 2 in ggplot2 (I assume this answer would be generalizable to base as well? There is some desirable jitter in the curved edges though the vertices remain stable. I assume this would mean some sort of linear transformation that has some sort of slightly randomized constant.
Graph 1
Graph 2 (color only used to highlight desired output)
library(ggplot2); library(scales)
## The vertices/points data
point x y
1 A 0.25 0.45
2 B 0.50 0.25
3 C 0.75 0.45
## The edges data
out.x out.y receiver.x receiver.y
1 0.25 0.45 0.50 0.25
2 0.50 0.25 0.75 0.45
3 0.75 0.45 0.25 0.45
4 0.25 0.45 0.50 0.25
5 0.50 0.25 0.75 0.45
6 0.75 0.45 0.25 0.45
## Edges and vertices/points data in dput form for ease
so <- structure(list(out.x = c(0.25, 0.5, 0.75, 0.25, 0.5, 0.75), out.y = c(0.45,
0.25, 0.45, 0.45, 0.25, 0.45), receiver.x = c(0.5, 0.75, 0.25,
0.5, 0.75, 0.25), receiver.y = c(0.25, 0.45, 0.45, 0.25, 0.45,
0.45)), .Names = c("out.x", "out.y", "receiver.x", "receiver.y"
), row.names = c(NA, -6L), class = "data.frame")
the_points <- data.frame(point=factor(LETTERS[1:3]),
x = c(.25, .5, .75),
y = c(.45, .25, .45)
)
## Plot the base graph minus the edges
root <- ggplot() +
geom_point(data=the_points, aes(x=x, y=y), size=12, inherit.aes = FALSE) +
geom_text(data=the_points, aes(x=x, y=y, label=as.character(point)),
inherit.aes = FALSE, color="white") +
ylim(c(.20, .75)) + xlim(c(.25, .75)) +
ylab("") + xlab("")
## Add the edges
root + geom_segment(aes(x= out.x, y= out.y, xend = receiver.x,
yend = receiver.y), alpha = .7, size = 3, data = so)
Here is an approach working on
bezier
curves from Hmisc (motivated by http://is-r.tumblr.com/post/38459242505/beautiful-network-diagrams-with-ggplot2)I think using the
igraph
library could be really helpful here, e.g:Resulting in: