7
$\begingroup$

I want to create a simple stacked bar chart where the bars start a custom y-axis point and end at another y-axis point.

So far I have:

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

which gives:

Stacked bar chart

However, I want a single bar on the left that originates at 2 and goes up to 4, and on the right a bar that starts at -2 and goes up to 6.

How can I achieve this? I also experimented around with the ChartElementFunction but I can not find how to specify a starting point of the bar.

Thanks for any help in advance.

$\endgroup$
7
$\begingroup$

I'm not sure whether this is documented or not. You can pass additional arguments to the ChartElementFunction like this:

r[{{xmin_, xmax_}, {ymin_, ymax_}}, y_, {origin_}] :=
                           Rectangle[{xmin, ymin + origin}, {xmax, ymax + origin}]

BarChart[{{1} -> 1, {1} -> 2, {2} -> 3}, ChartElementFunction -> r]

Mathematica graphics

edit

Perhaps more elegant and somewhat more obscure:

r[a_, _, {origin_}] := Rectangle @@ Transpose[a + {0, origin}]
$\endgroup$
  • $\begingroup$ Ah, now this is great. Didn't know I can pass meta data into the element function as a rule. Thanks very much! $\endgroup$ – Thomas Fankhauser Nov 19 '15 at 13:37
  • $\begingroup$ me too (missing something*) $\endgroup$ – Kuba Nov 19 '15 at 13:51
  • $\begingroup$ @Kuba He! :D ... $\endgroup$ – Dr. belisarius Nov 19 '15 at 13:52
  • $\begingroup$ Very nice, you can write even {1 -> 1, 1 -> 2, 3 -> 3}! $\endgroup$ – ybeltukov Nov 19 '15 at 14:05
  • $\begingroup$ and {2, 3, 4} -> 1 for a common origin :) $\endgroup$ – ybeltukov Nov 19 '15 at 14:12
0
$\begingroup$

Charting`RangeBarChart

The undocumented function Charting`RangeBarChart does exactly what is required. It takes the same options as BarChart.

Charting`RangeBarChart[{{2, 4}, {-2, 6}},  
  ChartElementFunction->"GlassRectangle", ChartStyle->97, ChartLabels->{"A","B"}]

enter image description here

Charting`RangeBarChart[RandomInteger[{-10, 10}, {20, 2}], 
  ChartElementFunction -> "GlassRectangle", ChartStyle-> 97, 
  ChartLabels -> CharacterRange["A","T"]]

enter image description here

The same result can be obtained using a custom ChartElementFunction as in belisarius's answer. However, this function needs to be modified to work properly with a non-default BarOrigin. Charting`RangeBarChart takes care of necessary changes automatically. For example, in the first example above, with the options BarOrigin -> Left and BarOrigin -> Right we get

enter image description here enter image description here

The disadvantage of this alternative is that it works only for single data set of pairs. With multiple data sets, using a custom ChartElementFunction with appropriate modifications to account for the BarOrigin setting seems to be the only way. The function ceF in this answer in a closely related Q/A does exactly that.

See also: closely related Q/A: Floating Bar Chart

$\endgroup$

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.