如何在igraph&中创建极坐标网络图(多个环)[R [英] How to create a polar network graph (multiple rings) in igraph & R

查看:69
本文介绍了如何在igraph&中创建极坐标网络图(多个环)[R的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个< 100节点的图,有几个类别.我希望属于一个类别的节点位于中心,而其他节点围绕外部均匀地排列成圆形-像星形图,但在中心具有多个节点.NodeXL将此称为极坐标图(请参阅:

I have a graph with <100 nodes, with several categories. I would like the nodes belonging to one category to be in the center, with the other nodes arranged evenly in a circle around the outside - like a star graph, but with multiple nodes in the center. NodeXL calls this a polar graph (see: http://www.connectedaction.net/2013/03/03/how-to-plot-a-network-in-a-polar-layout-using-nodexl/) Given this data from the manual for graphs from dataframes:

actors<-data.frame(name=c("Alice", "Bob", "Cecil", "David",
                        "Esmeralda"),
                 age=c(48,33,45,34,21),
                 gender=c("F","M","F","M","F"))
relations <- data.frame(from=c("Bob", "Cecil", "Cecil", "David",
                           "David", "Esmeralda"),
                    to=c("Alice", "Bob", "Alice", "Alice", "Bob", "Alice"),
                    same.dept=c(FALSE,FALSE,TRUE,FALSE,FALSE,TRUE),
                    friendship=c(4,5,5,2,1,1), advice=c(4,5,5,4,2,3))
g <- graph.data.frame(relations, directed=TRUE, vertices=actors)

如果我要让雌性居中,雄性排列成一圈怎么办?我可以将图和图分别分开,但是在思考如何将它们重新组合在一起并寻找另一个答案时遇到了麻烦.

What if I want the females in the center and the males arranged in a circle around? I can divide up the graph and graph each separately, but I'm having trouble thinking through how to put them back together and am looking for another answer.

gsubf<-induced.subgraph(g,V(g)$gender=="F")
gsubm<-induced.subgraph(g,V(g)$gender=="M")
gsubfcoords<-layout.fruchterman.reingold(gsubf, xlim=c(-2,2), ylim=c(-2,2))
gsubmcoords<-layout.circle(gsubm)

然后我可以将它们分配给V(gsubf)$ x,V(gsubf)$ y ...,但是我正在努力将它们重新组合在一起.有没有更简单的方法?还是另一个包装做极性包装?

Then I could assign them to V(gsubf)$x, V(gsubf)$y... but I'm struggling how to put it all back together. There may be an easier way? or maybe another package to do polar?

推荐答案

我最近在邮件列表中回答了这个问题,但出于完整性考虑,我还将在此处提供答案.

I have responded to this question on the mailing list recently, but for sake of completeness I'll also include the answer here.

igraph布局是简单的矩阵,每个顶点有2列和1行,因此,最简单的方法可能是自己生成一个矩阵.如果你想将一个顶点放置在距中心半径r处且角度为alpha(in弧度),那么您必须使用以下公式来计算X和Y坐标:

igraph layouts are simply matrices with 2 columns and one row for each vertex, so the easiest is probably if you generate such a matrix yourself. If you want to place a vertex at radius r from the center with an angle of alpha (in radians), then you have to use the following formulae to figure out the X and Y coordinates:

X = r * cos(alpha)
Y = -r * sin(alpha)

否定Y坐标仅是因为该坐标的Y轴屏幕的系统从上到下.因此,您可以在R中创建这样的函数:

where the Y coordinate is negated only because the Y axis of the coordinate system of the screen is oriented from top to bottom. So you can create a function like this in R:

polar.layout <- function(radii, angles) {
    cbind(radii*cos(angles), -radii*sin(angles))        
}

必须使用两个列表来调用 polar.layout 函数:一个用于指定每个顶点的半径和一个指定每个顶点角度的半径.它然后将返回一个矩阵对象,该对象可以按如下方式传递给 plot():

The polar.layout function has to be called with two lists: one that specifies the radius of each vertex and one that specifies the angle of each vertex. It will then return a matrix object that can be passed to plot() as follows:

layout <- polar.layout(radii, angles)
plot(graph, layout=layout)

因此,您只需要两个向量:一个用于半径,一个用于角度.您可以按照以下步骤从性​​别中构建这些内容:

So all you need is two vectors: one for the radii and one for the angles. You can construct these from the genders as follows:

males <- which(V(g)$gender == "M")
females <- which(V(g)$gender == "F")
radii <- ifelse(V(g)$gender == "F", 1, 2)
angles <- rep.int(0, vcount(g))
angles[males] <- (1:length(males)-1) * 2 * pi / length(males)
angles[females] <- (1:length(females)-1) * 2 * pi / length(females)
layout <- polar.layout(radii, angles)
plot(g, layout=layout)

这篇关于如何在igraph&amp;中创建极坐标网络图(多个环)[R的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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