2
$\begingroup$

Question 1: How can I separate the halves of a PairedBarChart so as to create enough room for long labels, as in this example?

PairedBarChart[{3, 4, 6, 25, 4}, {1, 5, 5, 22, 3},
 ChartLabels -> {"Long label 1", "Long label 2", "Long label 3", "Long label 4", "Long label 5"},
 BarSpacing -> {0, .5},
 AxesLabel -> {Text[
    Style["Headcount", FontFamily -> "Times", 14, Italic]], 
   Text[Style["Function", FontFamily -> "Times", 18, Italic, Black]]}]

Question 2: How can I label the horizontal axes differently, so on the left I have "Headcount A" and on the right "Headcount B"?

Of course, one could kludge together two separate BarCharts using Row and such, but that brings up scaling and alignment problems. I'd like to achieve these within PairedBarChart.

$\endgroup$
3
$\begingroup$

Question 1: BarSpacing takes a three-value form in this case:

With the setting `BarSpacing->{$s_p$, $s_g$, $s_b$}, $s_p$ can be used to control the space between the pair of bar charts. The unit for $s_p$ is in fractions of the width of the resulting graphic.

chart = 
  PairedBarChart[
   {3, 4, 6, 25, 4}, {1, 5, 5, 22, 3},
   ChartLabels -> {"Long label 1", "Long label 2", 
                     "Long label 3", "Long label 4", "Long label 5"},
   BarSpacing -> {35, 0, .5},
   AxesLabel -> {
     Text[Style["Headcount", FontFamily -> "Times", 14, Italic]],
     Text[Style["Function", FontFamily -> "Times", 18, Italic, Black]]}
  ]

Mathematica graphics


Question 2: I found no direct method through options of PairedBarChart. A slightly kludgy workaround is to replace the first occurrence of the formatted label. Below is a helper function to do that replacement. We first find the first occurrence of the $x$-axis label to replace with FirstPosition then replace it with MapAt.

Clear[replaceAxis]
replaceAxis[chart_, rule_, addopts___] :=
 MapAt[
   # /. f_@Style[First@rule, opts___] :> f@Style[Last@rule, opts, addopts] &,
   chart,
   FirstPosition[chart, f_[Style[First@rule, ___]]]
 ]

Here is a usage example on the chart generated above:

replaceAxis[chart, "Headcount" -> "NewLabel"]

relabeled

This also allows to change the Style options for the new label after the chart has been generated. For instance:

replaceAxis[chart, "Headcount" -> "RedLabel", Red, 18]

with larger red label

$\endgroup$
  • $\begingroup$ Thanks so much. I didn't see that third option in my cursory view of the documentation. Any hints on my second question? $\endgroup$ – David G. Stork Jun 26 '18 at 18:27
  • $\begingroup$ @DavidG.Stork I put a helper function together to change one of the two labels (see edited answer). It is not a solution within PairedBarChart, but it should be reasonably effective nonetheless. $\endgroup$ – MarcoB Jun 26 '18 at 19:04
  • $\begingroup$ Thanks. That works (accept). (Wolfram should adopt your hack!) $\endgroup$ – David G. Stork Jun 26 '18 at 19:26
  • $\begingroup$ @DavidG.Stork Glad you like it! It may still be a bit brittle, but it should work in a pinch! $\endgroup$ – MarcoB Jun 26 '18 at 19:39

Your Answer

By clicking "Post Your Answer", you acknowledge that you have read our updated terms of service, privacy policy and cookie policy, and that your continued use of the website is subject to these policies.

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