5
$\begingroup$

I want to label sequentially some charts. Say, for instance, that I had:

BarChart[{{1, 3, 4}, {4, 5}}, ChartLabels -> {{"r1", "r2"}, {"c1", "c2", "c3","c4","c5"}}]

enter image description here

And I want to maintain those groups "r1" and "r2", but I want the columns to go from "c1" to "c5" without restarting. How can I do this?

Alternatively, I could change the coloring of the charts such that each group "r1" and "r2" had a distinct color from eachother (with a label on the right), but I would still have restarting labels "c1", "c2" and "c3".

Thanks!

$\endgroup$

2 Answers 2

5
$\begingroup$

You can define a function to label the data using Labeled as in @David's answer:

lblngF = MapIndexed[Function[{d, p},Labeled[d, #2[[1]][[## & @@ p]]]], #, {#2[[2]]}] &;
lblF = Fold[lblngF, #, Thread[{Reverse@#2, {2, 1}}]] &;

dt = {{1, 3, 4}, {4, 5}};
labels = {{"r1", "r2"}, {{"c1", "c2", "c3"}, {"c4", "c5"}}};

BarChart[lblF[dt, labels]]

Mathematica graphics

Alternatively, you can use the option LabelingFunction:

BarChart[dt, ChartLabels -> {{"\nr1", "\nr2"}, None}, 
 LabelingFunction -> (Placed[labels[[2]][[##]] & @@ #2, Below] &)]
(* same picture *)
$\endgroup$
1
  • $\begingroup$ I'm taking this as the main answer because I feel like the labeling function is a bit more open to different datasets than David's. Still, both accomplish the result perfectly and I would select both answers if I could. $\endgroup$
    – Sos
    Aug 5, 2015 at 11:01
6
$\begingroup$

This isn't pretty, but it works:

BarChart[{
{Labeled[1,"c1"],Labeled[3,"c2"],Labeled[4,"c3"]},{Labeled[4,"c4"], Labeled[5,"c5"]}}, 
ChartLabels -> {{"r1","r2"},None}
] 

enter image description here

$\endgroup$

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.