如何在R中绘制完整的图形? [英] How to plot a complete graph in R?

查看:155
本文介绍了如何在R中绘制完整的图形?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我想在R中绘制完整的图表。

我该怎么做?我在CRAN上只发现了一个包,它具有生成完整图的功能。但是这个软件包,即RnavGraph,没有安装,而是以错误状态退出。进一步搜索似乎很困难,因为图的不同意义,这不仅与图结构有关,而且还与图相关。



如何绘制完整图表 in R?



Ps:
但是当我尝试安装RnavGraph时,出现以下错误:

 错误:依赖关系'graph','RBGL'不能用于包'RnavGraph'
*去除'/ home / steve /R/x86_64-unknown-linux-gnu-library/3.0/RnavGraph'

下载的源文件包位于
'/ tmp / RtmpIW4p30 / downloaded_pa​​ckages'
警告消息:
在install.packages(RnavGraph)中:
安装包'RnavGraph'具有非零退出状态


解决方案

使用 igraph 。这里有一个简单的方法:

$ p $ lt; code> library(igraph)

CompleteGraph< - function(n){
myEdges< - combn(1:n,2)
myGraph< - graph(myEdges,directed = FALSE)
return(myGraph)
}

myGraph< - CompleteGraph(10)

plot(myGraph)

igraph 包可以让您从边缘列表中绘制图形。在这种情况下,我使用 combn 从矢量 1:n 中生成两个数字的所有唯一组合。当我将这个提供给函数时,它会创建一个图,其中每个节点都连接到每个其他节点。我设置了 directed = false ,因此绘制图形时不会显示箭头。 igraph 函数将图的绘图添加到函数中,因此新创建的图可以如上绘制。 (比输入 plot.igraph 更简单)


I want to plot complete graphs in R.

How can I do that? I found only one package on CRAN that does have a function to generate complete graphs. But this package, namely "RnavGraph", didn't install but exited with error status. Searching further seems to be difficult, because of the different meanings of graph, which is not soley associated with graph structures but also with plots.

How can I plot a complete graph in R?

Ps: But I got the following error when I tried to install "RnavGraph":

ERROR: dependencies ‘graph’, ‘RBGL’ are not available for package ‘RnavGraph’
* removing ‘/home/steve/R/x86_64-unknown-linux-gnu-library/3.0/RnavGraph’

The downloaded source packages are in
    ‘/tmp/RtmpIW4p30/downloaded_packages’
Warning message:
In install.packages("RnavGraph") :
  installation of package ‘RnavGraph’ had non-zero exit status

解决方案

Use igraph. Here's a simple way:

library(igraph)

CompleteGraph <- function(n) {
  myEdges <- combn(1:n,2)
  myGraph <- graph(myEdges, directed=FALSE)
  return(myGraph)
}

myGraph <- CompleteGraph(10)

plot(myGraph)

The igraph package lets you make a graph from a list of edges. In this case, I used combn to generate all unique combinations of two numbers from the vector 1:n. When I feed this into the graph function, it creates a graph where every node is connected to every other node. I set directed=false so arrows don't show up when the graph is plotted. The igraph functions adds plotting of graphs to the plot function, so the newly created graph can be plotted as above. (It's easier than typing plot.igraph)

这篇关于如何在R中绘制完整的图形?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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