使用R在坐标系中绘制节点和边。

3
我实现了FR测试,详情请参见这里,现在我想通过在R中可视化生成的最小生成树来测试它。应该在坐标系中绘制顶点和边。
此外,我希望为每个点设置颜色(取决于它属于哪个样本),并通过点的大小表达可能的第三维。
这是我目前为止得到的:
library(ggplot2)


nodes <- data.frame(cbind(c("A", "A", "A", "B", "B", "B"), c(1,2,3,8,2,1), c(6,3,1,4,5,6)))
edges <- data.frame(cbind(c("A", "A", "A"), c("A", "B", "B"), c(1,3,2), c(6,1,5), c(2,8,1), c(3,4,6)))


p <- ggplot() + 
    geom_point(nodes, aes(x=nodes[,2], y=nodes[,3])) +
    geom_line(edges)

p

1
我相信ggplot2可以做到,但你是否反对使用scream来完成这种任务的igraph呢? - Tyler Rinker
1个回答

2

我认为在这种情况下igraph是最好的选择...

nodes <- data.frame(a=c("A", "A", "A", "B", "B", "B"), b=c(1,2,3,8,2,1), 
d=c(6,3,1,4,5,6)) 
#cbind made your nodes characters so i have removed it here

edges <- data.frame(a=c("A", "A", "A"), b=c("A", "B", "B"), d=c(1,3,2), 
e=c(6,1,5), f=c(2,8,1), g=c(3,4,6))

以下是您所提供数据的示例,使用坐标布局系统coords生成颜色colouring
library(igraph)

from <- c(rep(edges[,3],3),rep(edges[,4],2),edges[,5])
to <- c(edges[,4],edges[,5],edges[,6],edges[,5],edges[,6],edges[,6])
myedges <- data.frame(from,to)
actors <- data.frame(acts=c(1,2,3,4,5,6,8))
colouring <- sample(colours(), 7) 
sizes <- sample(15,7)
coords<-cbind(x=runif(7,0,1),y=runif(7,0,1))


myg <- graph.data.frame(myedges, vertices=actors, directed=FALSE)
V(myg)$colouring <- colouring
V(myg)$sizes <- sizes

plot(myg,vertex.color=V(myg)$colouring,vertex.size=V(myg)$sizes,
layout=coords,edge.color="#55555533")

为了绘制跨度,有许多选项可供选择,例如:
d <- c(1,2,3)

E(myg)$colouring <- "#55555533"
E(myg, path=d)$colouring <- "red"
V(myg)[ d ]$colouring <- "red"

plot(myg,vertex.color=V(myg)$colouring,vertex.size=V(myg)$sizes
,edge.width=3,layout=coords,edge.color=E(myg)$colouring )

使用轴:

plot(myg,vertex.color=V(myg)$colouring,vertex.size=V(myg)$sizes
,edge.width=3,layout=coords,edge.color=E(myg)$colouring, axes=TRUE )

并使用rescale=FALSE以保持原始轴比例


谢谢您的回复。但是节点应该在一个带有可见坐标轴的坐标系中具有固定位置,而边缘应该是直线。 - Matthias Munz
坐标系可以是任何你想要的,我只是在示例中选择了随机数字。此外,对于轴,请参见 axes=TRUE - user1317221_G

网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接