6
$\begingroup$

How do I label a bar chart such that a list produced by Mathematica such as {{7, 0.0446108}, {5, 0.074924}, {2, 0.0778787}, {3, 0.0778787}, {1, 0.0798332}, {9, 0.0805987}, {10, 0.0805987}, {e, 0.0821453}, {8, 0.11312}, {4, 0.14265}, {6, 0.145762}} has the bar chart labeled from smallest to largest bar with numbers 7,5,2,3,1,9,10,e,8,4,6 appear in that order from left to right on the horizontal axis with the heights of the bars, e.g., 0.0778787 along the vertical axes.

Clear[AA, BB]
edges = {1 <-> 2, 1 <-> 3, 2 <-> 4, 3 <-> 4, 4 <-> 5, 4 <-> 6, 
   5 <-> 6, 7 <-> 6, 6 <-> 8, 8 <-> 9, 8 <-> 10, 9 <-> e, 10 <-> e};
AA = PageRankCentrality[edges]
BB = Sort[AA]
SortBy[{VertexList[edges], 
   PageRankCentrality[edges]}\[Transpose], Last]
PieChart[BB, ChartStyle -> "DarkRainbow", ChartLabels -> Automatic];
BarChart[BB, ChartStyle -> "DarkRainbow", ChartLabels -> Automatic] 
$\endgroup$

3 Answers 3

6
$\begingroup$
edges = {1 <-> 2, 1 <-> 3, 2 <-> 4, 3 <-> 4, 4 <-> 5, 4 <-> 6, 
  5 <-> 6, 7 <-> 6, 6 <-> 8, 8 <-> 9, 8 <-> 10, 9 <-> e, 10 <-> e};
prc = PageRankCentrality[edges]

{0.0798,0.0778,0.0778,0.1426,0.0749,0.1457,0.04461,0.1131,0.08059,0.08059,0.08214}

ordering = Ordering @ prc;

GraphPropertyChart

GraphComputation`GraphPropertyChart gives a layout that combines a pie chart of vertex properties with a graph of edges:

gpc = GraphComputation`GraphPropertyChart[
 Graph[VertexList[edges][[ordering]], edges], 
 Automatic -> prc[[ordering]], EdgeStyle -> Directive[CapForm["Butt"], Thickness[.05]], 
 ChartStyle->"DarkRainbow"]

enter image description here

Click on a vertex to get the incident edges highlighted: enter image description here

Or use the function explode from this answer by Simon Woods to highlight incident edges for selected vertices:

explode[pc_, i_] := ReplacePart[pc, Position[pc, False][[i]] -> True]
explode[gpc, VertexIndex[g, #] & /@ {10,3, 6}] 

enter image description here

Additional examples:

GraphComputation`GraphPropertyChart[Graph[VertexList[edges][[ordering]], edges], 
  Automatic -> prc[[ordering]],
  ChartElementFunction -> ChartElementDataFunction["NoiseSector", 
   "AngularFrequency" -> 5, "RadialAmplitude" -> .2], 
  ChartLabels -> (Style[#, 16] & /@ VertexList[edges][[ordering]]), ChartStyle -> 63] /. 
 {False -> True , Text[x_, pos_] :> {FaceForm[White], Disk[pos, .7], Text[x, pos]}}

enter image description here

With a different ChartElementFunction:

ChartElementFunction -> ChartElementDataFunction["TriangleWaveSector", 
   "AngularFrequency" -> 10, "RadialAmplitude" -> .5]

enter image description here

GraphComputation`GraphPropertyChart[Graph[edges], 
  Automatic -> ConstantArray[1, VertexCount[edges]], 
  EdgeStyle -> Directive[CapForm["Butt"], Thickness[.05]]]

enter image description here

bwc = BetweennessCentrality[Graph[edges]];
GraphComputation`GraphPropertyChart[
  Graph[VertexList[edges][[Ordering[bwc]]], edges], Automatic -> Sort[bwc], 
    EdgeStyle -> Directive[CapForm["Butt"], Thickness[.05]]]

enter image description here

PieChart

PieChart[prc[[ordering]], ChartStyle -> "DarkRainbow", 
 SectorOrigin -> {{2 Pi, "Counterclockwise"}, 1},
 ChartLabels -> Placed[Style[#, 16] & /@ VertexList[edges][[ordering]], "RadialCallout"]]

enter image description here

BarChart

BarChart[prc[[ordering]], ChartStyle -> "DarkRainbow", 
 ChartLabels -> VertexList[edges][[ordering]]]

Mathematica graphics

Update: Combining BarChart with Graph to produce something similar to the output of GraphPropertyChart:

gr1 = Graph[VertexList[edges][[ordering]], edges];
graph = SetProperty[gr1, 
  {ImageSize -> 500, GraphLayout -> {"LinearEmbedding", Method -> "SpectralOrdering"}, 
    VertexCoordinates -> Thread[{Range[VertexCount@gr1], 0}]}];

bc = BarChart[20 prc[[ordering]], 
   ChartLabels -> Placed[Style[#, 20] & /@ VertexList[gr1], Top], 
   ChartStyle -> "DarkRainbow", BarSpacing -> .2, BarOrigin -> Top, 
   Axes -> False, PerformanceGoal -> "Speed", ImageSize -> 500];
Show[bc, graph]

enter image description here

$\endgroup$
3
$\begingroup$
data = SortBy[{VertexList[edges],PageRankCentrality[edges]}\[Transpose], N@*Last]
BarChart[BB, ChartStyle -> "DarkRainbow",ChartLabels -> First /@ data]
$\endgroup$
1
  • $\begingroup$ Alan ... Many thanks for your quick and very helpful response ... prg $\endgroup$
    – user42700
    Commented May 3, 2017 at 20:03
3
$\begingroup$

Use associations:

BarChart[
 Sort@AssociationThread[VertexList[edges], PageRankCentrality[edges]],
 ChartLabels -> Automatic
]
$\endgroup$
2
  • $\begingroup$ Szabolcs: Very nice idea to use the sort by association; I appreciate your approach too and it works well ... many thanks ... prg $\endgroup$
    – user42700
    Commented May 3, 2017 at 20:35
  • 1
    $\begingroup$ @PRG, please take a tour. To learn that you can express your gratitude with upvotes :) $\endgroup$
    – Kuba
    Commented May 3, 2017 at 20:56

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.