【发布时间】:2020-03-04 07:26:50
【问题描述】:
我有一个这样的数据框:
structure(list(x = c(65.11, 65.11, 65.11, 43.72, 43.72, 43.72,
43.72, 43.72, 43.72, 43.72, 43.72, 59.89, 59.89, 59.89, 59.89,
36.24, 36.24, 36.24, 36.24, 67.88, 37.89, 37.89, 37.89, 56.05,
56.05, 56.05, 60.16, 60.16, 60.16, 30.92, 30.92, 30.92, 47.55,
47.55, 47.55), y = c(32.17, 32.17, 32.17, 56.09, 56.09, 56.09,
56.09, 56.09, 56.09, 56.09, 56.09, 15.64, 15.64, 15.64, 15.64,
81.61, 81.61, 81.61, 81.61, 56.96, 21.69, 21.69, 21.69, 86.47,
86.47, 86.47, 68.31, 68.31, 68.31, 51.56, 51.56, 51.56, 43.44,
43.44, 43.44), xend = c(59.89, 60.16, 43.72, 59.89, 37.89, 56.05,
60.16, 65.11, 36.24, 30.92, 47.55, 37.89, 65.11, 67.88, 30.92,
37.89, 56.05, 30.92, 47.55, 60.16, 43.72, 59.89, 30.92, 43.72,
36.24, 60.16, 43.72, 67.88, 47.55, 43.72, 36.24, 37.89, 59.89,
37.89, 43.72), yend = c(15.64, 68.31, 56.09, 15.64, 21.69, 86.47,
68.31, 32.17, 81.61, 51.56, 43.44, 21.69, 32.17, 56.96, 51.56,
21.69, 86.47, 51.56, 43.44, 68.31, 56.09, 15.64, 51.56, 56.09,
81.61, 68.31, 56.09, 56.96, 43.44, 56.09, 81.61, 21.69, 15.64,
21.69, 56.09), node = c("484", "484", "484", "309", "309", "309",
"309", "309", "309", "309", "309", "740", "740", "740", "740",
"151", "151", "151", "151", "11", "26", "26", "26", "991", "991",
"991", "731", "731", "731", "714", "714", "714", "99", "99",
"99")), class = c("tbl_df", "tbl", "data.frame"), row.names = c(NA,
-35L))
我可以绘制如下图:
df %>%
ggplot() +
geom_point(aes(x, y), shape = 21, size = 5, stroke = 1, colour = 'black', fill = 'blue') +
geom_segment(aes(x = x, y = y, xend = xend, yend = yend),
colour = 'red')
但这实际上是有向图,所以每个段都有两个有向分量重叠。我尝试使用position_dodge2 修复它,但以下尝试并不像需要的那样简洁:
df %>%
ggplot() +
geom_point(aes(x, y), shape = 21, size = 5, stroke = 1, colour = 'black', fill = 'blue') +
geom_segment(aes(x = x, y = y, xend = xend, yend = yend),
colour = 'red',
position = position_dodge2(width = 3))
我想让重叠的部分平行,可能会从节点位置稍微避开它们的末端。我怎样才能做到这一点?
【问题讨论】: