Take the 2-minute tour ×
Mathematica Stack Exchange is a question and answer site for users of Mathematica. It's 100% free, no registration required.

I am facing the following problem: I need to work on several already-designed graphs, accessible in ".DOT" format. Mathematica can perfectly read these graphs, however because the nodes of the graphs only contains "IDs" instead of the actual textual data I'm interrested in. The data are stored in node-labels.

So if I do a something like that:

argGraph = Import["http://www.arg.dundee.ac.uk/AIFdb/dot/4"]
VertexList[argGraph]

I only get a list of IDs: {"234", "235", "236", "237", "238", "239"}

How can I easily reformat the graph so that the labels are assigned as node values ?

The Graph looks like this: Graph Image and is a representation of this argument map: Argument Map Thanks a lot for your help.

share|improve this question

1 Answer 1

up vote 2 down vote accepted

Update: Two methods to get the vertex labels of a Graph object:

  1. PropertyValue: PropertyValue[{grph, #}, VertexLabels] & /@ VertexList[grph]

  2. Options: VertexLabels /. Options[grph] gives the rules mapping vertex names to vertex labels.

Example:

  argGraph = Import["http://www.arg.dundee.ac.uk/AIFdb/dot/4"];

  PropertyValue[{argGraph, #}, VertexLabels] & /@ VertexList[argGraph]
  (* {"The object is red.", "The object looks red.", 
     "Things which look red are normally red.", 
      "The object is illuminated by a red light.", 
      "RA", "CA"} *)
  VertexLabels /. Options[argGraph]
  (* {"239" -> "CA", "237" -> "The object is illuminated by a red light.", 
     "236" -> "Things which look red are normally red.", 
     "235" -> "The object looks red.", "234" -> "The object is red.", 
     "238" -> "RA"} *)

Using Graph

 Graph[EdgeList[argGraph],
 VertexLabels -> (MapAt[Placed[Framed[Style[#], FrameStyle -> None, 
     Background -> Directive[Opacity[.8], LightBlue]],
    {1/2, 1/2}] &, #, {2}] & /@ (VertexLabels /. Options[argGraph])),
 VertexStyle -> Directive[EdgeForm[None], White], 
 GraphLayout -> "RadialEmbedding",
 ImagePadding -> 80, ImageSize -> 500]

enter image description here

Using GraphPlot

  GraphPlot[EdgeList[argGraph] /. (VertexLabels /. Options[argGraph]) /. 
  DirectedEdge -> Rule,   VertexLabeling -> True,
  VertexRenderingFunction -> (Text[#2, #1, Background -> LightBlue] &),
  ImageSize -> 500, Method -> "SpringEmbedding", DirectedEdges -> True]  

enter image description here


Original post: Constructing new graphs by replacing vertices with vertex labels:

 rplcmntRule =   VertexLabels /. Options[argGraph]

 newVertices = VertexList[argGraph] /. rplcmntRule;
 newEdges = EdgeList[argGraph] /. rplcmntRule
 (* {"RA" \[DirectedEdge] "The object is red.", 
  "The object looks red." \[DirectedEdge] "RA", 
  "Things which look red are normally red." \[DirectedEdge] "RA", 
  "CA" \[DirectedEdge] "RA", 
  "The object is illuminated by a red light." \[DirectedEdge] "CA"} *)

 Graph[newEdges, VertexLabels -> Placed["Name", {1/2, 1/2}], 
 VertexShapeFunction -> "RoundedRectangle", 
 VertexSize -> {"Scaled", .1}]

enter image description here

A more "readable" version:

 Graph[newEdges, VertexLabels -> Placed["Name", {1/2, 1/2}],
 VertexStyle -> Directive[EdgeForm[None], White], 
 VertexSize -> {"Scaled", .1},
 ImagePadding -> 70, GraphLayout -> "RadialEmbedding"]

enter image description here

share|improve this answer
    
Works perfectly, thanks so much! –  afentis Dec 28 '12 at 15:56
    
@afentis, my pleasure. –  kguler Dec 28 '12 at 16:03
    
Just a minor issue, if I try with a more complex graph such as the one found at "arg.dundee.ac.uk/AIFdb/dot/3";, I get a Graph::supp: Mixed graphs and multigraphs are not supported. If you have any idea why this error occurs it would be appreciated :) otherwise, it's not a major issue... –  afentis Dec 28 '12 at 16:35
1  
afentis, You can use GraphPlot instead of Graph. But ... it seems to me the most important part of the argGraph logic is the construction of RA and CA nodes and they must have distinct identities as "links" between "args". Compare, for example, GraphPlot[EdgeList[argGraph2] /. DirectedEdge -> Rule, VertexLabeling -> True, ImageSize -> Large] versus GraphPlot[EdgeList[argGraph2] /. rplcmntRule2 /. DirectedEdge -> Rule, VertexLabeling -> True, ImageSize -> Large]. So, I think it is better to keep labels as labels. Perhaps, a work-around could be ... –  kguler Dec 28 '12 at 17:26
1  
... to re-label the RA and CA nodes as RA-1,RA-2 ... CA-1, CA-2 ... etc to preserve their identities. –  kguler Dec 28 '12 at 17:28

Your Answer

 
discard

By posting your answer, you agree to the privacy policy and terms of service.

Not the answer you're looking for? Browse other questions tagged or ask your own question.