iganea9
(Ion Ganea)
September 12, 2020, 5:35pm
1
I’m doing a statistical exercise and I need to group all the numbers in groups of 2 (4 groups totaling 8 numbers) with all 8 labels following the exact same model so to speak. As you can see below, each of these numbers has to have the label with the same order number. For example: under 7.43 should be Knop, under 6.06 should be Knop+R and so on and so forth. However, as I’ve done it, the last 2 labels are used for every bar. Am I not doing this right and if so, what’s the proper way to do it?
Here’s the code:
BarChart[{{7.43, 6.06}, {5.34, 12.43}, {6.1, 7.4}, {6.76, 6.56}},
ChartLabels -> {{"Knop", "Knop+R"} , {"Knop-N",
"Knop-N+R" }, {"Knop-P", "Knop-P+R"}, {"Knop-K", "Knop-K+R"}},
ChartElementFunction -> "GlassRectangle", ChartStyle -> "Pastel",
ImageSize -> 700, ChartLegends -> {"Knop", "Reglalg"},
LabelStyle -> {14, Bold},
PlotLabel -> "Lungimea rădăcinii principale tratate cu Reglalg",
AxesLabel -> {"", "cm"}]
The results:
I do not really have a solution, but would this be an option you can live with?
BarChart[{{7.43, 6.06}, {5.34, 12.43}, {6.1, 7.4}, {6.76, 6.56}},
ChartLabels -> {{"Knop", "Knop-N", "Knop-P", "Knop-K"}, {"", "+R"}},
ChartElementFunction -> "GlassRectangle", ChartStyle -> "Pastel",
ImageSize -> 700, ChartLegends -> {"Knop", "Reglalg"},
LabelStyle -> {14, Bold},
PlotLabel -> "Lungimea rădăcinii principale tratate cu Reglalg",
AxesLabel -> {"", "cm"}]
iganea9
(Ion Ganea)
September 12, 2020, 8:10pm
3
It’s very good. Thanks a lot.
Hi Ion,
BarChart accepts the Labeled wrapper, so one option would be to label the data.
data = {{7.43, 6.06}, {5.34, 12.43}, {6.1, 7.4}, {6.76, 6.56}};
labels = {{"Knop", "Knop+R"}, {"Knop-N", "Knop-N+R"}, {"Knop-P", "Knop-P+R"}, {"Knop-K", "Knop-K+R"}};
barData = MapThread[Labeled, #] & /@ Transpose[{data, labels}];
BarChart[barData,
ChartElementFunction -> "GlassRectangle",
ChartStyle -> "Pastel",
ImageSize -> 700,
ChartLegends -> {"Knop", "Reglalg"},
LabelStyle -> {14, Bold},
PlotLabel -> "Lungimea rădăcinii principale tratate cu Reglalg",
AxesLabel -> {"", "cm"}]
iganea9
(Ion Ganea)
September 13, 2020, 8:11am
5
Ty. It’s just what I need.
Perfect solution! - Thanks!
rnachbar
(Robert Nachbar)
September 13, 2020, 7:45pm
7
MapThread takes a third argument for level specification that makes the data preparation a bit easier in this case. So instead of mapping MapThread over the threaded (transposed) data, just use
In[139]:= barData = MapThread[Labeled, {data, labels}, 2]
Out[139]= {{Labeled[7.43, "Knop"] ,
Labeled[6.06, "Knop+R"]}, {Labeled[5.34, "Knop-N"] ,
Labeled[12.43, "Knop-N+R"]}, {Labeled[6.1, "Knop-P"] ,
Labeled[7.4, "Knop-P+R"]}, {Labeled[6.76, "Knop-K"] ,
Labeled[6.56, "Knop-K+R"]}}
and the same bar chart is produced.
TIL. Thanks Robert.
I also learned a lot from your presentations at the Wolfram Summer School Lectures 2018