2
$\begingroup$

I want to construct a bar chart for which the last bar is two bars stacked, while the others are not stacked. My thought had been to create two bar charts and overlay them. The challenge I have is that the two charts do not use the same scale for the vertical axis, as you can see below. How could I construct both charts to have the same scale?

chart1 = BarChart[
  Join[Table[{0, 0}, {7}], {{1, 3}}],
  ChartLabels -> {Placed[Append[Table["", {7}], "word"], Below], None},
  ChartLegends -> {None, {"top", "bottom"}},
  ChartLayout -> "Stacked",
  ImagePadding -> 10
  ]
chart2 = BarChart[
  Append[Range[7], 0],
  ChartLabels -> Append[Range[7], ""],
  ImagePadding -> 10
  ]
Overlay[{chart1, chart2}]

enter image description here

$\endgroup$
1
  • $\begingroup$ Show[chart1, chart2] $\endgroup$
    – Bob Hanlon
    Commented Oct 18, 2017 at 23:54

2 Answers 2

2
$\begingroup$
BarChart[Append[List /@ Range[7], {1, 3}], 
 ChartLayout -> "Stacked", 
 ChartLabels -> {Append[Range[7], "word"], None}]

enter image description here

$\endgroup$
2
  • $\begingroup$ It was right under my nose. Thanks again. $\endgroup$ Commented Oct 19, 2017 at 1:50
  • $\begingroup$ By using grouping of 3, rather than 2, this allows me to get the coloring to differ between the first 7 bars and the last 2, which I hadn't mentioned but also wanted to do. $\endgroup$ Commented Oct 19, 2017 at 2:07
3
$\begingroup$

It seems that a stacked bar chart will automatically determine stacking based on the size of each sub list, so the following seems to work fine for combining these:

BarChart[{{1}, {2}, {3}, {4}, {5}, {6}, {1, 3}}, ChartLayout -> "Stacked"]

If you wish to overlay two charts directly with Show, it would probably be best to make sure they have the same PlotRange, or to completely disable the axes on the smaller one (Axes -> False). Show will preserve the absolute position of the elements, but will not inform the various plotting functions of the overall plot area.

$\endgroup$
1
  • $\begingroup$ That is helpful to know. $\endgroup$ Commented Oct 19, 2017 at 1:51

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.