How to get with RectangleChart the same x-axis labelling as with GeneralizedBarChart?

I am still using GeneralizedBarChart,although Mathematica documentation says:

As of Version 7.0, GeneralizedBarChart has been superseded by RectangleChart.

However, I could so far not figure out, how to conveniently get the same output, especially of the x-axis labelling, from RectangleChart or from any other Mathematica function.

GeneralizedBarChart is used by giving for each bar the position, height, and widths:

GeneralizedBarChart[{ {pos1,height1,widths1} ..} ]

For example:

Needs["BarCharts"]

data={{-0.125, 1225, 0.15}, {0.025, 2007, 0.15}, {0.175, 1017,
0.15}, {0.325, 1508, 0.15}, {0.475, 2878, 0.15}, {0.625, 2785,
0.15}, {0.775, 2042, 0.15}, {0.925, 4257, 0.15}, {1.075, 651, 0.15}};

GeneralizedBarChart[data, Frame -> True, Axes -> False]


My question is: How do I get a similar output with a labeled x-axis using RectangleChart, BarChart or any other Mathematica function? The output of Mathematica's Histogram function would be also ok. But as far as I see Histogram needs as input the raw data and does not accept already binned data.

Daniel

You can use RectangleChart with a custom ChartElementFunction that modifies the built-in chart element functions to re-center the rectangles based on the first column of input data.

ClearAll[cF]
cF[f_: "Rectangle", o : OptionsPattern[]] :=
Module[{ori = ChartingChartStyleInformation["BarOrigin"], box},
box = Switch[ori, Top | Bottom, {#3[[1]] + {-1, 1}/2 Subtract @@ #[[1]], #[[2]]},
Right | Left, {#[[1]], #3[[1]] + {-1, 1}/2 Subtract @@ #[[2]]}];
ChartElementDataFunction[f, o][box, ##2]] &


You also need a function to transform your 3D data to a form that can be used with RectangleChart:

ClearAll[gbcToRc]
gbcToRc = Thread[#[[All, {3, 2}]] -> #[[All, 1]]] &;


or

gbcToRc  = {#3, #2} -> # & @@@#& (* thanks: Mr.Wizard *)


Examples:

data = {{-0.125, 1225, 0.15}, {0.025, 2007, 0.15}, {0.175, 1017,  0.15},
{0.325, 1508, 0.15}, {0.475, 2878, 0.15}, {0.625, 2785,  0.15},
{0.775, 2042, 0.15}, {0.925, 4257, 0.15}, {1.075, 651, 0.15}};

RectangleChart[gbcToRc @ data,
Frame -> True, Axes -> False, BarSpacing -> 0, ChartStyle -> 3,
ChartElementFunction -> cF[]]


RectangleChart[gbcToRc @ data,
Frame -> True, Axes -> False, BarSpacing -> 0, ChartStyle -> 3,


RectangleChart[gbcToRc @ data,
Frame -> True, Axes -> False, BarSpacing -> 0, ChartStyle -> 3, BarOrigin -> Top,


RectangleChart[gbcToRc @ data,
Frame -> True, Axes -> False, BarSpacing -> 0, ChartStyle -> 3, BarOrigin -> Left,


RectangleChart[gbcToRc @ data,
Frame -> True, Axes -> False, BarSpacing -> 0, ChartStyle -> 3, BarOrigin -> Right,


• This is a nice answer, which clearly shows that one has to do a lot of gymnastics to achieve with RectangleChart the same result as with GeneralizedBarChart. Thanks.
– dnet
Feb 28, 2017 at 17:23
• Great answer! {#3, #2} -> # & @@@ data also works, and though it may be a little less efficient that shouldn't matter here, especially since plotting functions have become rather slow. Feb 28, 2017 at 18:33
• Thank you @Mr.Wizard. I updated with the alternative you suggested.
– kglr
Feb 28, 2017 at 18:45
• Can one in this aproach also show two overlapping histograms with two monochrom bar colors? Similar to my answer below using Histogram? RectangleChart[gbcToRc /@ {data, data2},ChartStyle -> {Red, Green},...]does not work.
– dnet
Mar 3, 2017 at 8:56
• @dnet, i will look into how to modify cF to handle multiple data sets.
– kglr
Mar 3, 2017 at 9:25

It's relatively easy to put together a function to generate your own primitives from the data formatted as you already have it:

Clear[bchart]
bchart[data_, filling_: Blue, border_: Black] := Graphics[{
EdgeForm[border], FaceForm[filling],
Rectangle[{#1 - #3/2, 0}, {#1 + #3/2, #2}] & @@@ data},
Frame -> True, AspectRatio -> 1/GoldenRatio
]

bchart[data]


The colors default to blue filling with black borders, but you can indicate your own:

bchart[data, Darker@Green, Red]


• Thank you for your answer! However, with this approach one cannot use the many options that Histogram or RectangleChart offer out of the box.
– dnet
Feb 28, 2017 at 17:27

I found now the following solution combining Histogram and WeightedData, which is good enough for the problem at hand, where all histogram bars are of identical width, not overlapping and without gaps.

plothisto[dat : {{_, _, _} ..}, opts : OptionsPattern[]] := plothisto[{dat}, opts];

plothisto[dat : {{{_, _, _}..} ..},
opts : OptionsPattern[]] := Module[{xmin, xmax, dx},
dx = dat[[1, 1, 3]];
xmin = dat[[1, 1, 1]] - dx/2;
xmax = dat[[1, -1, 1]] + dx/2;
Histogram[
WeightedData, {dat[[All, All, 1]], dat[[All, All, 2]]}], {xmin,
xmax, dx}, FilterRules[{opts}, Options[Histogram]]]
]


For example:

data = {{-0.125, 1225, 0.15}, {0.025, 2007, 0.15}, {0.175, 1017,
0.15}, {0.325, 1508, 0.15}, {0.475, 2878, 0.15}, {0.625, 2785,
0.15}, {0.775, 2042, 0.15}, {0.925, 4257, 0.15}, {1.075, 651,
0.15}};
data2 = data;
data2[[All, 2]] = RandomReal[{0, 2000}, Length[data]];
plothisto[{data, data2}, ChartStyle -> Opacity[1], Frame -> True,
Axes -> False]


The advantage for me is that the graphical output of Histogram is close to my needs.

However, this is surely not a general replacement of GeneralizedBarChart!