2
$\begingroup$

Thanks to m_goldberg remarks here, the following BarChart was obtained:

data14GAOF = {{33/36, 3/36}, {11/36, 25/36}, {27/36, 1/36, 
    8/36}, {14/36, 22/36}, {1/36, 13/36, 1/36, 1/36, 2/36, 2/36, 
    10/36, 4/36, 1/36, 1/36}, {8/36, 8/36, 16/36, 4/36}};
labels14GAOF = {{"node 4", "node 9"}, {"node 4", "node 9"}, {"node 4",
     "node 7", "node 9"}, {"node 4", "node 9"}, {"node 3", "node 4", 
    "node 5", "node 6", "node 7", "node 8", "node 9", "node 10", 
    "node 11", "node 13"}, {"node 4", "node 5", "node 9", "node 11"}};
grouplabels14GAOF = 
  Style[#, Black, Bold, FontSize -> 18] & /@ {"OF1(N-1)", "OF1", 
    "OF2(N-1)", "OF2", "OF3(N-1)", "OF3"};
labeleddata14GAOF = 
  MapThread[
   Labeled[#1, #2, Axis] &, {SortBy[-First[#] &] /@ 
     Apply[Labeled[#1, Style[#2, FontSize -> 16, White], Center] &, 
      Transpose /@ Transpose[{data14GAOF, labels14GAOF}], {2}], 
    grouplabels14GAOF}];
BarChart[labeleddata14GAOF, 
 ChartStyle -> {GrayLevel[0.1], GrayLevel[0.2], GrayLevel[0.3], 
   GrayLevel[0.4], GrayLevel[0.5], GrayLevel[0.6], GrayLevel[0.65], 
   GrayLevel[0.7], GrayLevel[0.75], GrayLevel[0.8]}, 
 ChartLayout -> "Stacked", AxesStyle -> Directive[Black, 24], 
 BarSpacing -> {Automatic, .3}, ImageSize -> 1200]

enter image description here

It is correct but it would be better to sowehow group the columns. In other words to separate each two columns OF1(N-1) and OF1, OF2(N-1) and OF2, OF3(N-1) and OF3 from each other a bit because they represent different functions. The idea is to get something like that:

enter image description here

How can it be realised?

$\endgroup$
3
$\begingroup$

Unfortunately BarChart does not support mixing different layouts.

To get the desired result, you can group the input data and use BarChart for each group, and combine the graphics primitives using Translate:

groups = Partition[labeleddata14GAOF, 2]; 
grouplabels = Style[#, 18, Bold] & /@ {"GA", "MOPSO", "NSGA"};
options = Sequence[ChartStyle -> {GrayLevel[0.1], GrayLevel[0.2], 
     GrayLevel[0.3], GrayLevel[0.4], GrayLevel[0.5], GrayLevel[0.6], 
     GrayLevel[0.65], GrayLevel[0.7], GrayLevel[0.75], 
     GrayLevel[0.8]}, ChartLayout -> "Stacked", 
   AxesStyle -> Directive[Black, 24], BarSpacing -> {Automatic, .3}];

bcs = BarChart[Labeled[##], options] /. Line[{{_, 0.}, {_, 0.}}] :> {} & @@@ 
  Transpose[{groups, grouplabels}]

groupspacing = .2;
Graphics[MapIndexed[Translate[#[[1]], {(2 + groupspacing) (#2[[1]] - 1), 0}] &, bcs], 
 AxesOrigin -> {0.5 - groupspacing, 0}, 
 PlotRange -> {{0.5 - groupspacing, 6.5 + 2 groupspacing}, All}, 
 AspectRatio -> 1/GoldenRatio, Axes -> True, PlotRangePadding -> 0, 
 Ticks -> {None, Automatic}, ImageSize -> 800]

enter image description here

With groupspacing = .5; we get

enter image description here

$\endgroup$
3
$\begingroup$

One possibility is to use Invisible and then "splice" an invisible bar into the data with Riffle. Since the labeling is already rather "elaborate" and local, I have added some labeling now by hand for looks:

With[
  {
    groupLabelPosY = -0.06,
    barLabelPosY = -0.02,
    groupLabelStyle = Curry[Style, {3, 1, 2}]["Section"][Black],
    barLabelStyle = Curry[Style, {3, 1, 2}]["Subsection"][Black]
  }
  ,
  BarChart[ Evaluate@(labeleddata14GAOF // Riffle[ #, Invisible @ 0, 3] &)
    , ChartStyle -> {
        GrayLevel[0.1], GrayLevel[0.2], GrayLevel[0.3], GrayLevel[0.4],\
        GrayLevel[0.5], GrayLevel[0.6], GrayLevel[0.65], GrayLevel[0.7],\
        GrayLevel[0.75], GrayLevel[0.8]
      }
    , ChartLayout -> "Stacked"
    , AxesStyle -> Directive[Black, 24]
    , BarSpacing -> {Automatic, .3}
    , ImageSize -> 1200
    , PlotRange -> {All, {-0.05, 1}}
    , Axes -> {False, True}
    , Epilog -> {
        Black
        , Line@{{0.4, 0}, {9, 0}}
        , Text[groupLabelStyle @ "GA", {1.5, groupLabelPosY}]
        , Text[groupLabelStyle @ "MOPSO", {4.5, groupLabelPosY}]
        , Text[groupLabelStyle @ "NSGA", {7.5, groupLabelPosY}]
        , Text[barLabelStyle @ "OF1(N-1)", {1, barLabelPosY}]
        , Text[barLabelStyle @ "OF1", {2, barLabelPosY}]
        , Text[barLabelStyle @ "OF2(N-1)", {4, barLabelPosY}]
        , Text[barLabelStyle @ "OF2", {5, barLabelPosY}]
        , Text[barLabelStyle @ "OF3(N-1)", {7, barLabelPosY}]
        , Text[barLabelStyle @ "OF3", {8, barLabelPosY}]
      }
  ]
]

BarChart with Spacing and Group Labels

$\endgroup$
  • $\begingroup$ Thanks for clarifying! I've never thought about usefulness of the Invisible function. $\endgroup$ – vladpopovcev Feb 12 at 6:39

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

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