3
$\begingroup$

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:

and is a representation of this argument map: Argument Map Thanks a lot for your help.

$\endgroup$

1 Answer 1

2
$\begingroup$

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

$\endgroup$
9
  • $\begingroup$ Works perfectly, thanks so much! $\endgroup$
    – afentis
    Dec 28, 2012 at 15:56
  • $\begingroup$ @afentis, my pleasure. $\endgroup$
    – kglr
    Dec 28, 2012 at 16:03
  • 1
    $\begingroup$ 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 ... $\endgroup$
    – kglr
    Dec 28, 2012 at 17:26
  • 1
    $\begingroup$ ... to re-label the RA and CA nodes as RA-1,RA-2 ... CA-1, CA-2 ... etc to preserve their identities. $\endgroup$
    – kglr
    Dec 28, 2012 at 17:28
  • 1
    $\begingroup$ Note that as of version 12.1 PropertyValue has been superseded by AnnotationValue. $\endgroup$ Jun 29, 2021 at 16:16

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

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