【发布时间】:2013-02-01 14:02:00
【问题描述】:
(注意 - 这与 using multiple size scales in a ggplot 的工作相同,但我问的是不同的问题)
我正在尝试构建一个显示从一个类到另一个类的转换的图。我想让圆圈代表每个班级,而从一个班级到另一个班级的箭头代表过渡。
我正在使用 geom_segment 和 arrow() 来绘制箭头。有什么办法:
- 让箭头在到达圆圈之前停止
- 调整位置,这样如果两个方向都有箭头,它们会被“躲避”而不是重叠。
我无法让 position="dodge" 在这里做任何有用的事情。
举个例子:
library(ggplot2)
points <- data.frame( x=runif(10), y=runif(10),class=1:10, size=runif(10,min=1000,max=100000) )
trans <- data.frame( from=rep(1:10,times=10), to=rep(1:10,each=10), amount=runif(100)^3 )
trans <- merge( trans, points, by.x="from", by.y="class" )
trans <- merge( trans, points, by.x="to", by.y="class", suffixes=c(".to",".from") )
ggplot( points, aes( x=x, y=y ) ) + geom_point(aes(size=size),color="red",shape=1) +
scale_size_continuous(range=c(4,20)) +
geom_segment( data=trans[trans$amount>0.6,], aes( x=x.from, y=y.from, xend=x.to, yend=y.to ),lineend="round",arrow=arrow(),alpha=0.5, size=0.3)
【问题讨论】:
-
有趣的问题。如果不首先转换基础数据,这在 ggplot 中是不可能直接实现的,但我建议在其他地方使用bending in the lines 来躲避倒数箭头。 Here is an example 在 R 中制作这样的弯曲箭头。虽然它仍然是一个复杂的情节,但我可能会建议您在初始阶段分面的小倍数,所以每个方面只显示流出。
标签: r ggplot2 data-visualization