mo-*_*eph 5 r data-visualization ggplot2
(注意 - 这是与在 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)
Run Code Online (Sandbox Code Playgroud)

我想既然没有人给出解决方案,我会提供一个更针对此类问题的包示例:
vecs <- data.frame(vecs =1:6,size=sample(1:100,6))
edges <- data.frame(from=sample(1:6,9,replace=TRUE), to=sample(1:6,9,replace=TRUE))
library(igraph)
g <- graph.data.frame(edges, vertices = vecs, directed = TRUE)
coords <- cbind(sample(1:20,6), sample(1:20,6))
plot(g, vertex.size=V(g)$size,vertex.color="white",layout=coords,axes=TRUE)
Run Code Online (Sandbox Code Playgroud)
这至少会解决圆问题之前的箭头,并且当存在倒数箭头时,它会用曲线调整它们,如下所示2<->5:

(箭头大小、线宽、颜色等当然可以修改)
我已经整理了 geom_segment 的一个简单扩展,它允许指定
它在pastebin上:geom_segment_plus。
我使用了类似这样的代码:
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_plus( data=trans[trans$amount>0.3,],
aes( x=x.from, y=y.from, xend=x.to, yend=y.to ),
lineend="round",arrow=arrow(length=unit(0.15, "inches")),
alpha=0.5, size=1.3,
offset=0.01, shorten.start=0.03, shorten.end=0.03)
Run Code Online (Sandbox Code Playgroud)
它绝对不完美,但它有效 - 您可以在此处看到一个指向左下角的双箭头。
offset、shorten.start和shorten.end是添加的aes元素。它们可以设置为数据点,但我还没有弄清楚如何正确缩放它们。

| 归档时间: |
|
| 查看次数: |
4684 次 |
| 最近记录: |