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

标签 r graph ggplot2 nodes

我实现了 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

最佳答案

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

关于r - 使用 R 在坐标系中绘制节点和边,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/13432444/

相关文章:

r - 如何根据因素在 R 中创建一个运行总计?

c# - 您的计算机中缺少 Rlapack.dll - c# 和 R

date - 如何在一张 ZingChart 图表上绘制 WeekOnWeek 图表?

algorithm - 如何表示分子和比较相等性

graph - 如何使用neo4j和gremlin存储树结构

R 包 ggpmisc : Putting hat on y in Regression Equation

r - 从 ggplot 中消除 NA

R:为什么 mco 的 nsga2 函数的优化解决方案不满足约束?

python - 热图显示每个单元格两个变量

r - 如何在 ggplot2 中为使用 stat_summary 制作的线条添加图例?