使用R绘制坐标系中的节点和边 [英] Plot nodes and edges in a coordinate system using R

查看:133
本文介绍了使用R绘制坐标系中的节点和边的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我在此处实施了FR测试,现在我想通过在R中可视化生成的最小生成树来测试它。顶点和边应该绘制在坐标系中。

另外,我想将颜色设置为每个点(取决于它属于哪个样本),并通过点的大小表示一个可能的第三维。



这是我到目前为止: (b)(b)(b)(b)b

$ b $ library(ggplot2)


节点< - data.frame(cbind A,A,B,B,B),c(1,2,3,8,2,1),c(6,3,1,4, (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 会是最好的...

 节点<  -  data.frame (a = c(A,A,B,B,B) bd = c(6,3,1,4,5,6))
#cbind制作了你的节点字符,所以我在这里删除了它

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 <颜色>产生颜色 coloring / code>

  library(igraph)

from< - c(rep (edge [,3],3),rep(边缘[,4],2),边缘[,5])
到< ,6],边缘[,5],边缘[,6],边缘[,6])
髓化< - data.frame(from,to)
actors< - data.frame (样本颜色(),7)
尺寸< - 样本(15,7)
coords< -cbind(x = runif(7,0,1),y = runif(7,0,1))


myg< - graph.data。框架(myedges,v (myg)=尺寸(尺寸)

绘图(myg,顶点)。 color = V(myg)$ coloring,vertex.size = V(myg)$ sizes,
layout = coords,edge.color =#55555533)

用于绘制一个spanning也有很多选项,例如

   
E(myg)$着色< - #55555533
E(myg,path = d)$ coloring< - red
V(myg)[d] $着色< - red

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

带轴:

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

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


I implemented the FR test here and now I would like to test it by means of visualizing the resulting minimum spanning trees in R. The vertices and edges should be plotted in a coordinate system.

Moreover I want to set the color for every dot (depending on to which sample it belongs) and express a possible third dimension through the size of the dots.

This is what I have got so far:

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

解决方案

I also think igraph would be best here...

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))

Here is an example using your data as above, to produce the colours colouring with the coordinate layout system coords

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")

for plotting a spanning there are also many options, e.g.

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 )

with axes:

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

and use rescale=FALSE to keep original axes scale

这篇关于使用R绘制坐标系中的节点和边的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆