我有 3 个顶点表示为与三个边相连的图中的点。我想将边缘向三角形(c(.5, .35)
)的中心弯曲。如何在 ggplot2 中将图 1 转换为图 2(我假设这个答案也可以推广到基础?尽管顶点保持稳定,但弯曲边缘有一些理想的抖动。我假设这意味着某种线性变换有某种稍微随机的常数。
图 1
图 2(仅用于突出显示所需输出的颜色)
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)