【问题标题】:Generate a visual representation from a table with relation weight从具有关系权重的表中生成视觉表示
【发布时间】:2012-02-21 17:41:51
【问题描述】:

我有一个格式如下的表格:

Item A | Item B | Weight
   X   |   Y    |   2
   X   |   Z    |   5
   Y   |   Z    |   3
   Y   |   W    |   2
  ...  |  ...   |  ...

我想生成一些图表,其中每个字母(W,X,Y,Z) 是一个节点,并且根据项目 B 的权重有一个具有一定宽度的链接。

问题是我可以用什么来生成这个图表?可以是工具、Java 或 R 库或其他语言。方式无所谓,我只需要生成图表即可。

【问题讨论】:

标签: java r language-agnostic graph nodes


【解决方案1】:

借用 digEmAll 的代码,我会在 qgraph 中做同样的事情:

data <- read.table(text=
"Item A,Item B,Weight
X,Y,2
X,Z,5
Y,Z,3
Y,W,2", sep=',',header=TRUE)

library(qgraph)
qgraph(data)

【讨论】:

  • 这个包似乎比其他包有更好的文档和功能。
【解决方案2】:

R 中的另一种方式是使用plot.igraph(有关参数的信息可以在here 找到)。

您可以在下面找到一个工作示例(基于您的数据):

library(igraph)

data <- read.table(text=
"Item A,Item B,Weight
X,Y,2
X,Z,5
Y,Z,3
Y,W,2", sep=',',header=TRUE)

g <- graph.data.frame(data,directed=TRUE)

vColors <- 'MediumSeaGreen'
vSizes <- 40 
vShapes <- 'circle' 
vLabels <- V(g)$name
vFontSizes <- 1.5

eColors <- 'blue'
eArrowSizes <- 1
eWidths <- 1
eLabels <- as.character(E(g)$Weight)
eLTypes <- 'dashed'
eFontSizes <- 1.5

plot(g, layout=layout.fruchterman.reingold,
        vertex.color=vColors, vertex.size=vSizes, vertex.shape=vShapes, 
        vertex.label=vLabels, vertex.label.dist=0, vertex.label.cex=vFontSizes,
        edge.color=eColors, edge.width=eWidths, edge.arrow.size=eArrowSizes,  
        edge.label=eLabels, edge.lty=eLTypes, edge.label.cex=eFontSizes)

编辑:

就像在基本 R plot() 函数中一样,您可以通过在前面代码的末尾添加以下行来显示图例:

legend(x=-1,c('X - Foo','Y - Bar','Z - Foo2','W - Bar2'))

更多信息请参考this documentation

【讨论】:

  • 我可以在图中使用图例而不是圆圈内的名称吗?
【解决方案3】:

在 R 中,您可以使用 diagram::plotweb

library(diagram)
#sample data
nodes <- LETTERS[23:26]
dat <- expand.grid(nodes,nodes)
dat$Weight <- rpois(16,5)+1

#put data in format for plotweb
datMat <- xtabs(Weight~Var1+Var2,dat)
#no loops
diag(datMat)<-0

#plot
plotweb(datMat)

【讨论】:

    猜你喜欢
    • 2020-09-06
    • 2021-02-16
    • 1970-01-01
    • 1970-01-01
    • 2012-06-05
    • 2022-09-23
    • 2018-02-09
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多