5
$\begingroup$

I am trying to plot multiple data sets on a bar chart with unrelated column labels for each data set...

Suppose my data is a list comprised of three different sub-lists of varying size, i.e., they have the form

{<Data Set 1 (2 Items)>, <Data Set 2 (4 Items)>, <Data Set 3 (8 Items)>}

I want to present the data in a bar chart, where I label each of the sub-lists its own label: Data Set 1 should be labeled {0, 1}, Data Set 2 should be labeled {00, 01, 10, 11} and Data Set 3 should be labeled {000, 001, 010, ..., 111}

How can I accomplish this?

In my current situation, I would like to add the binary expansion listed above to the following bar chart. The last bit of code is obviously wrong, but that's the problem I'm trying to fix.

BarChart[
  {{.45, .55}, 
   {0.25, 0.26, 0.27, 0.22}, 
   {0.125, 0.125, 0, 0.25, 0.25, 0, 0.25, 0}},
  ChartElementFunction -> "GlassRectangle", 
  ChartStyle -> "Pastel",
  ChartLabels -> 
    {{"Order 1", "Order 2", "Order 3"}, 
     {"0", "1", "00", "01", "10", "11", "000", "001", 
      "010", "011", "100", "101", "110", "111"}}]

Ienter image description here

Any help with this issue would be much appreciated, Thanks!

$\endgroup$
0

2 Answers 2

8
$\begingroup$
data = {{.45, .55}, {0.25, 0.26, 0.27, 0.22}, {0.125, 0.125, 0, 0.25,  0.25, 0, 0.25, 0}}; 
collabels = {{"0", "1"}, {"00", "01", "10", "11"}, 
    {"000", "001", "010", "011", "100", "101", "110", "111"}};

labeleddata = MapThread[Labeled, #] & /@ Transpose[{data, collabels}] (*thanks: @eldo*)

BarChart[labeleddata, ChartElementFunction -> "GlassRectangle", ChartStyle -> "Pastel", 
 ChartLabels -> {{"Order 1", "Order 2", "Order 3"}, None}]

Mathematica graphics

Alternatively,

rowlabels = {"Order 1", "Order 2", "Order 3"};

BarChart[Labeled @@@ Transpose[{labeleddata, rowlabels}], 
 ChartElementFunction -> "GlassRectangle", ChartStyle -> "Pastel"]

Mathematica graphics

$\endgroup$
4
  • $\begingroup$ Thanks for the help! That's exactly what I needed! $\endgroup$ Jun 26, 2017 at 0:47
  • $\begingroup$ @EngineeringSquirrel, my pleasure, Welcome to mma.se. $\endgroup$
    – kglr
    Jun 26, 2017 at 0:54
  • 1
    $\begingroup$ A little bit clearer maybe: MapThread[Labeled, #] & /@ Transpose... - Something very nice and new to me $\endgroup$
    – eldo
    Jun 26, 2017 at 1:01
  • $\begingroup$ @eldo, thank you. Updated with your suggestion. $\endgroup$
    – kglr
    Jun 26, 2017 at 1:05
4
$\begingroup$

Horrible and kludgy in comparison to @kglr's answer, but may provide a workaround in similar situations. Epilog can often be used to crowbar things in at the last minute:

data = {.45, .55, 0.25, 0.26, 0.27, 0.22, 0.125, 0.125, 0, 0.25, 0.25, 0, 0.25, 0};
barlabels = {"0", "1", "00", "01", "10", "11", "000", "001", "010", "011", "100", "101", "110", "111"};
epilogtext = Text[#[[1]], Scaled[#[[2]]]] & /@ 
   Transpose[{{"Order 1", "Order 2", "Order 3"}, {{0.1, -0.1}, {0.3, -0.1}, {0.7, -0.1}}}];
BarChart[data, ChartElementFunction -> "GlassRectangle", ChartStyle -> "Pastel", 
ChartLabels -> barlabels, Epilog -> epilogtext, ImagePadding -> {{Automatic, Automatic}, {Scaled[0.025], Automatic}}]

Which gives:

enter image description here

$\endgroup$
1
  • $\begingroup$ Also a useful answer! I appreciate the additional help! $\endgroup$ Jul 7, 2017 at 1:01

Your Answer

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

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