Message Boards Message Boards

0
|
4427 Views
|
7 Replies
|
4 Total Likes
View groups...
Share
Share this post:

[Solved] Set labels for every bar in BarChart?

Posted 3 years ago

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: the results of my code

POSTED BY: Ion Ganea
7 Replies
Posted 3 years ago

Ty. It's just what I need.

POSTED BY: Ion Ganea

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.

POSTED BY: Robert Nachbar
Posted 3 years ago

TIL. Thanks Robert.

I also learned a lot from your presentations at the Wolfram Summer School Lectures 2018

POSTED BY: Rohit Namjoshi
Posted 3 years ago

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"}]

enter image description here

POSTED BY: Rohit Namjoshi

Perfect solution! - Thanks!

POSTED BY: Henrik Schachner
Posted 3 years ago

It's very good. Thanks a lot.

POSTED BY: Ion Ganea

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"}]
POSTED BY: Henrik Schachner
Reply to this discussion
Community posts can be styled and formatted using the Markdown syntax.
Reply Preview
Attachments
Remove
or Discard

Group Abstract Group Abstract