generating graph with different rown ames and column names
2
0
Entering edit mode
@tyronewilliams701-7042
Last seen 6.9 years ago
United States

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 • 1.0k views
ADD COMMENT
0
Entering edit mode
@james-w-macdonald-5106
Last seen 1 day ago
United States

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 COMMENT
0
Entering edit mode

 

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

ADD REPLY
0
Entering edit mode
@herve-pages-1542
Last seen 1 day ago
Seattle, WA, United States

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 COMMENT

Login before adding your answer.

Traffic: 192 users visited in the last hour
Help About
FAQ
Access RSS
API
Stats

Use of this site constitutes acceptance of our User Agreement and Privacy Policy.

Powered by the version 2.3.6