【问题标题】:Plot nodes and edges in a coordinate system using R使用 R 在坐标系中绘制节点和边
【发布时间】:2012-11-17 16:30:54
【问题描述】:

我实现了 FR 测试 here,现在我想通过在 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

【问题讨论】:

  • 我确信ggplot2 可以做到这一点,但你反对igraph 为这种任务尖叫吗?

标签: r graph ggplot2 nodes


【解决方案1】:

我也认为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 保持原始坐标轴比例

【讨论】:

  • 谢谢。但是节点应该在具有可见轴的坐标系中具有固定的位置,并且边缘应该是直线。
  • ...坐标系可以是任何你想要的,我只是在我的例子中选择了随机数。对于轴,请参阅axes=TRUE
猜你喜欢
  • 2012-06-08
  • 2013-01-05
  • 1970-01-01
  • 2021-12-06
  • 2018-08-25
  • 1970-01-01
  • 2022-01-03
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多