简体   繁体   English

如何在R中绘制完整的图形?

[英]How to plot a complete graph in R?

I want to plot complete graphs in R. 我想在R中绘制完整的图形。

How can I do that? 我怎样才能做到这一点? I found only one package on CRAN that does have a function to generate complete graphs. 我在CRAN上只发现了一个确实具有生成完整图形功能的程序包。 But this package, namely "RnavGraph", didn't install but exited with error status. 但是此软件包(即“ RnavGraph”)未安装,但退出并显示错误状态。 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? 如何在R中绘制完整的图形

Ps: But I got the following error when I tried to install "RnavGraph": 附:但是,当我尝试安装“ 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 . 使用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. igraph程序包使您可以从边列表中制作图形。 In this case, I used combn to generate all unique combinations of two numbers from the vector 1:n . 在这种情况下,我使用combn从向量1:n生成两个数字的所有唯一组合。 When I feed this into the graph function, it creates a graph where every node is connected to every other node. 当我将其输入到graph函数时,它将创建一个图形,其中每个节点都与其他每个节点相连。 I set directed=false so arrows don't show up when the graph is plotted. 我设置了directed=false因此在绘制图形时箭头不会出现。 The igraph functions adds plotting of graphs to the plot function, so the newly created graph can be plotted as above. igraph函数将图的igraph添加到plot函数中,因此可以如上所述绘制新创建的图。 (It's easier than typing plot.igraph ) (比键入plot.igraph容易)

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM