Question: generating graph with different rown ames and column names
0
gravatar for tyrone.williams.701
4.4 years ago by
United States
tyrone.williams.7010 wrote:

I am trying to generate a simple graph using this dataset below.

                                   

 Abdominal pain Chest pain Flu-like Liver Damage Nausea Numbness Swelling
           Avandaia                  1          0        0            1      1        1        1
           Warfrin                   0          1        1            0      1        1        1

I converted this to an adjacency matrix.

                        Abdominal pain Chest pain Flu-like Liver Damage Nausea Numbness Swelling
           Avandaia                  1          0        0            1      1        1        1
           Warfrin                   0          1        1            0      1        1        1
           Flu-like                  0          0        0            0      0        0        0
           Liver Damage              0          0        0            0      0        0        0
           Nausea                    0          0        0            0      0        0        0
           Numbness                  0          0        0            0      0        0        0
           Swelling                  0          0        0            0      0        0        0

 

I cannot plot  a graph because my row names and column names dont match.

   library(graph)
   g = as(x4, "graphNEL")
   plot(g, "neato")

Error in asMethod(object) : 'rownames(from)' and ' colnames(from)' must be identical

graph rgraphviz • 771 views
ADD COMMENTlink modified 4.4 years ago by Hervé Pagès ♦♦ 13k • written 4.4 years ago by tyrone.williams.7010
Answer: generating graph with different rown ames and column names
0
gravatar for James W. MacDonald
4.4 years ago by
United States
James W. MacDonald49k wrote:

Does this do what you are expecting?

> wf <- c(0,1,1,0,1,1,1)
> av <- c(1,0,0,1,1,1,1)
> amat <- matrix(0,9,9)
> colnames(amat) <- row.names(amat) <- c("Avandaia","Warfrin","Abdominal pain","Chest pain","Flu-like","Liver damage","Nausea","Numbness","Swelling")
> amat[1:2,3:9] <- rbind(av,wf)
> amat[3:9,1:2] <- cbind(av,wf)
> g <- as(amat, "graphNEL")
> plot(g, "neato")

 

ADD COMMENTlink written 4.4 years ago by James W. MacDonald49k

 

Perfect, now that I know where I went wrong I feel like an idiot. Thanks James your solution was awesome.

ADD REPLYlink written 4.4 years ago by tyrone.williams.7010
Answer: generating graph with different rown ames and column names
0
gravatar for Hervé Pagès
4.4 years ago by
Hervé Pagès ♦♦ 13k
United States
Hervé Pagès ♦♦ 13k wrote:

Hi,

It's not the call to plot() that complains that the rownames and colnames must be identical, it's your attempt at coercing your matrix to a graphNEL object. This coercion expects the input to be an adjacency matrix and in your case it's not, because its rownames and colnames are not the same.

Here is how you can generate the adjacency matrix:

> symptoms
         Abdominal pain Chest pain Flu-like Liver Damage Nausea Numbness
Avandaia              1          0        0            1      1        1
Warfrin               0          1        1            0      1        1
         Swelling
Avandaia        1
Warfrin         1

nodes <- unlist(dimnames(symptoms))
nb_nodes <- length(nodes)
edges <- which(symptoms == 1L, arr.ind=TRUE)
edges <- cbind(rownames(symptoms)[edges[,"row"]],
               colnames(symptoms)[edges[,"col"]])
adjmat <- matrix(integer(nb_nodes^2), nrow=nb_nodes, dimnames=list(nodes, nodes))
adjmat[edges] <- 1L

Then:

> adjmat
               Avandaia Warfrin Abdominal pain Chest pain Flu-like Liver Damage
Avandaia              0       0              1          0        0            1
Warfrin               0       0              0          1        1            0
Abdominal pain        0       0              0          0        0            0
Chest pain            0       0              0          0        0            0
Flu-like              0       0              0          0        0            0
Liver Damage          0       0              0          0        0            0
Nausea                0       0              0          0        0            0
Numbness              0       0              0          0        0            0
Swelling              0       0              0          0        0            0
               Nausea Numbness Swelling
Avandaia            1        1        1
Warfrin             1        1        1
Abdominal pain      0        0        0
Chest pain          0        0        0
Flu-like            0        0        0
Liver Damage        0        0        0
Nausea              0        0        0
Numbness            0        0        0
Swelling            0        0        0

> library(graph)

> g1 <- as(adjmat, "graphNEL")

> g1
A graphNEL graph with directed edges
Number of Nodes = 9
Number of Edges = 10

> plot(g1, "neato")

Another option would be to call the graphNEL() constructor to make the graphNEL object:

g2 <- graphNEL(nodes, split(edges[, 2], edges[, 1]), edgemode="directed")

Unfortunately I get a segfault when trying to plot g2:

> plot(g2, "neato")

 *** caught segfault ***
address 0x40000093, cause 'memory not mapped'

Traceback:
 1: .Call("Rgraphviz_buildEdgeList", aa, edgemode(graph), subGList,     edgeNames, removed, edgeAttrs, defAttrs, PACKAGE = "Rgraphviz")
 2: buildEdgeList(graph, recipEdges, edgeAttrs, subGList, attrs$edge)
 3: agopen(x, name = name, layout = TRUE, layoutType = y, attrs = attrs,     nodeAttrs = nodeAttrs, edgeAttrs = edgeAttrs, subGList = subGList,     recipEdges = recipEdges)
 4: .local(x, y, ...)
 5: plot(g2, "neato")
 6: plot(g2, "neato")

Possible actions:
1: abort (with core dump, if enabled)
2: normal R exit
3: exit R without saving workspace
4: exit R saving workspace
Selection: 

But that's another story and I'll leave it to the graph/Rgraphviz maintainers.

Cheers,
H.

> sessionInfo()
R version 3.1.2 (2014-10-31)
Platform: x86_64-unknown-linux-gnu (64-bit)

locale:
 [1] LC_CTYPE=en_US.UTF-8       LC_NUMERIC=C              
 [3] LC_TIME=en_US.UTF-8        LC_COLLATE=en_US.UTF-8    
 [5] LC_MONETARY=en_US.UTF-8    LC_MESSAGES=en_US.UTF-8   
 [7] LC_PAPER=en_US.UTF-8       LC_NAME=C                 
 [9] LC_ADDRESS=C               LC_TELEPHONE=C            
[11] LC_MEASUREMENT=en_US.UTF-8 LC_IDENTIFICATION=C       

attached base packages:
[1] grid      stats     graphics  grDevices utils     datasets  methods  
[8] base     

other attached packages:
[1] Rgraphviz_2.10.0 graph_1.44.0    

loaded via a namespace (and not attached):
[1] BiocGenerics_0.12.1 parallel_3.1.2      stats4_3.1.2       
[4] tools_3.1.2        

 

 

ADD COMMENTlink written 4.4 years ago by Hervé Pagès ♦♦ 13k
Please log in to add an answer.

Help
Access

Use of this site constitutes acceptance of our User Agreement and Privacy Policy.
Powered by Biostar version 16.09
Traffic: 160 users visited in the last hour