Message Boards Message Boards

0
|
3055 Views
|
6 Replies
|
5 Total Likes
View groups...
Share
Share this post:

How to place labels in BarChart

Posted 2 years ago

I'd like to display value labels along the inside top of the bars in this stacked bar chart. The help examples say it's possible, but don't give any example of how to do it.

Additionally, I'd like to apply axis labels, "calories" for the y axis, and "time (days)" for the x axis, with each stacked bar labeled "1", "2", "3"... so that the final image looks like so:

enter image description here

6 Replies

Jonathan,

maybe we can convince you to stay with Mathematica and not to use post processing. Rohit's nice solution still can be tweaked simply by:

  • using LabelingFunction -> Center;
  • avoid writing labels '0' at all;

    bch = BarChart[data, ChartLayout -> "Stacked", Axes -> True, 
       Frame -> True, 
       FrameLabel -> {"Food Age {days}", "Consumption {calories}"}, 
       PlotLabel -> "Food Consumption by Age", 
       ChartLegends -> {"Fresh kill", "Canned food", "Kitchen scraps", 
         "Dry food"}, LabelingFunction -> Center];
    bch /. Text[Style[0, __], __] -> Nothing
    

enter image description here

Addendum:

Here is a somewhat cleaner way giving the same result; just remove the value '0' from your data:

data = {{80, 20, 25, 15}, {40, 60, 30, 20}, {10, 55, 20, 50}, {0, 40, 
     10, 50}, {0, 25, 10, 75}, {0, 10, 0, 100}, {0, 0, 0, 120}} /. 0 -> _;
BarChart[data, ChartLayout -> "Stacked", Axes -> True, Frame -> True, 
 FrameLabel -> {"Food Age {days}", "Consumption {calories}"}, 
 PlotLabel -> "Food Consumption by Age", 
 ChartLegends -> {"Fresh kill", "Canned food", "Kitchen scraps", 
   "Dry food"}, LabelingFunction -> Center]
POSTED BY: Henrik Schachner
Posted 2 years ago

Hi Henrik,

Nice, +1. Jonathan also wanted each bar to have x-axis labels of 1 to 7. I tried various ChartLabels settings but failed. Do you know how to do that?

POSTED BY: Rohit Namjoshi

Thanks, Rohit. Well, no, ChartLabels and friends do not seem to work in this case. I only could make it in this clumsy way (notice the "\n" in the first framelabel):

data = {{80, 20, 25, 15}, {40, 60, 30, 20}, {10, 55, 20, 50}, {0, 40, 
     10, 50}, {0, 25, 10, 75}, {0, 10, 0, 100}, {0, 0, 0, 120}} /. 0 -> _;
bch = BarChart[data, ChartLayout -> "Stacked", Axes -> True, 
   Frame -> True, 
   FrameLabel -> {"\nFood Age {days}", "Consumption {calories}"}, 
   PlotLabel -> "Food Consumption by Age", 
   ChartLegends -> {"Fresh kill", "Canned food", "Kitchen scraps", 
     "Dry food"}, LabelingFunction -> Center, ImageSize -> Large];
dummydata = Range[7];
(* "invisible" barchart: *)
dummybch = BarChart[dummydata, LabelingFunction -> Below, ChartElements -> Graphics[{Opacity[0], Circle[]}]];
Show[bch, dummybch]

enter image description here

POSTED BY: Henrik Schachner
Posted 2 years ago

That is a neat workaround. +1

POSTED BY: Rohit Namjoshi

Thanks for demonstrating the use of Frame, FrameLabel, and PlotLabel. Guess I'm going to stick with post-processing via Paint.net instead of LabelingFunction, as it looks like there's probably too much artistic license needed for a generic function.

Posted 2 years ago

Hi Jonathan,

This does most of want you want, but there are issues with the placement of the values, especially for the zero values. Probably have to use a custom LabelingFunction to fix them.

BarChart[data,
 ChartLayout -> "Stacked",
 Axes -> True,
 Frame -> True,
 FrameLabel -> {"Food Age {days}", "Consumption {calories}"},
 PlotLabel -> "Food Consumption by Age",
 ChartLegends -> {"Fresh kill", "Canned food", "Kitchen scraps", "Dry food"},
 LabelingFunction -> Top]

enter image description here

POSTED BY: Rohit Namjoshi
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