简体   繁体   中英

Generate a visual representation from a table with relation weight

I have a table in the following format:

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

I want to generate some graph where each letter(W,X,Y,Z) is a node and have a link with some width according to the weight to the Item B.

The question is what I can use to generate this graph? Can be a tool, a Java or R library or another language. The way doesn't matter, I only need to generate the graph.

Another way in R is using plot.igraph (infos about parameters can be found here ).

Below you can find a working example (based on your data):

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)

在此输入图像描述

EDIT :

Exactly as in the base R plot() function, you can show a legend by adding the following line at the end of previous code:

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

Please refer to this documentation for further information.

Borrowing the code of digEmAll I'll do the same in 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)

在此输入图像描述

In R, you could use 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)

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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