1
$\begingroup$

I have the following data, which shows the number of events by day of the week (Sunday through Saturday) and hour of the day (00 through 23):

  data={<|0 -> 2200, 1 -> 300, 2 -> 100, 3 -> 50, 4 -> 150, 5 -> 1850, 
  6 -> 2450, 7 -> 2600, 8 -> 3050, 9 -> 4400, 10 -> 5550, 11 -> 8450, 
  12 -> 7650, 13 -> 7750, 14 -> 6750, 15 -> 6350, 16 -> 5250, 
  17 -> 4600, 18 -> 3700, 19 -> 2800, 20 -> 2700, 21 -> 2650, 
  22 -> 2500, 23 -> 1950|>, <|0 -> 1750, 1 -> 100, 4 -> 300, 
  5 -> 2500, 6 -> 3100, 7 -> 7350, 8 -> 10550, 9 -> 5900, 10 -> 7150, 
  11 -> 7350, 12 -> 7850, 13 -> 7500, 14 -> 6300, 15 -> 6900, 
  16 -> 9050, 17 -> 8150, 18 -> 3800, 19 -> 3000, 20 -> 2450, 
  21 -> 2450, 22 -> 2250, 23 -> 2150|>, <|0 -> 1450, 1 -> 50, 2 -> 50,
   4 -> 650, 5 -> 2500, 6 -> 3050, 7 -> 7700, 8 -> 10450, 9 -> 6400, 
  10 -> 8250, 11 -> 8050, 12 -> 8450, 13 -> 8250, 14 -> 7200, 
  15 -> 7800, 16 -> 9600, 17 -> 9100, 18 -> 4400, 19 -> 3200, 
  20 -> 2700, 21 -> 2550, 22 -> 2450, 23 -> 2200|>, <|0 -> 1650, 
  1 -> 150, 2 -> 100, 4 -> 750, 5 -> 2550, 6 -> 3050, 7 -> 7650, 
  8 -> 11300, 9 -> 7100, 10 -> 7750, 11 -> 9350, 12 -> 9250, 
  13 -> 8000, 14 -> 7950, 15 -> 7900, 16 -> 9500, 17 -> 9150, 
  18 -> 4900, 19 -> 3600, 20 -> 2950, 21 -> 2650, 22 -> 2550, 
  23 -> 2350|>, <|0 -> 1650, 1 -> 100, 2 -> 50, 3 -> 200, 4 -> 700, 
  5 -> 2550, 6 -> 3050, 7 -> 7350, 8 -> 11150, 9 -> 7200, 10 -> 8150, 
  11 -> 8150, 12 -> 8400, 13 -> 8100, 14 -> 7400, 15 -> 7900, 
  16 -> 9800, 17 -> 9100, 18 -> 4750, 19 -> 3550, 20 -> 2850, 
  21 -> 2750, 22 -> 2450, 23 -> 2100|>, <|0 -> 1800, 1 -> 50, 
  2 -> 100, 4 -> 900, 5 -> 2500, 6 -> 3050, 7 -> 7050, 8 -> 9800, 
  9 -> 6450, 10 -> 7750, 11 -> 8650, 12 -> 9350, 13 -> 8750, 
  14 -> 7450, 15 -> 8250, 16 -> 9100, 17 -> 9550, 18 -> 6100, 
  19 -> 4800, 20 -> 3800, 21 -> 3450, 22 -> 3100, 
  23 -> 2550|>, <|0 -> 2250, 1 -> 350, 2 -> 150, 3 -> 100, 4 -> 150, 
  5 -> 2250, 6 -> 2550, 7 -> 2500, 8 -> 3500, 9 -> 4350, 10 -> 5200, 
  11 -> 6900, 12 -> 6600, 13 -> 6600, 14 -> 6150, 15 -> 6350, 
  16 -> 5650, 17 -> 6200, 18 -> 5400, 19 -> 4850, 20 -> 3750, 
  21 -> 3850, 22 -> 3400, 23 -> 2950|>}

I've created the following stacked BarChart. Each bar is a day of the week.
ChartLabels->Automatic labels the hours within each bar, but I'd also like to label the individual bars themselves as Sunday, Monday, Tuesday, etc.

BarChart[data, ChartLabels -> Automatic, 
 ChartLayout -> "Stacked", Joined -> True, ImageSize -> Large, 
 FrameLabel -> {Style["Day of Week", Bold, 12, Blue], 
   Style["Volume", Bold, 12, Blue]}, Frame -> True]

enter image description here

I.e., I'd like labels that are specifically associated with each bar, as seen below. But if I use ChartLabels to specify the days of the week, I loose the hour labeling. I want to keep both. How do I do that?

dow = System`DateObjectDump`$DaysOfTheWeek;
BarChart[data, ChartLabels -> {dow, None}, 
 ChartLayout -> "Stacked", Joined -> True, ImageSize -> Large, 
 FrameLabel -> {Style["Day of Week", Bold, 12, Blue], 
   Style["Volume", Bold, 12, Blue]}, Frame -> True]

enter image description here

$\endgroup$
6
  • $\begingroup$ how do you get distSort? $\endgroup$
    – kglr
    May 15, 2021 at 22:57
  • 2
    $\begingroup$ Try ChartLabels -> {dow, Automatic} $\endgroup$
    – MelaGo
    May 15, 2021 at 22:57
  • $\begingroup$ @kglr Sorry, that was supposed to be data instead of distSort. I just corrected it. $\endgroup$
    – theorist
    May 15, 2021 at 23:01
  • $\begingroup$ @MelaGo Beautiful, that works! $\endgroup$
    – theorist
    May 15, 2021 at 23:04
  • 1
    $\begingroup$ @theorist I think it's supposed to match the dimensions of the data. The first dimension is days, and the second dimension is hours. Example (modified from documentation for ChartLabels): BarChart[{Range[3], Range[3]}, ChartLabels -> {{"1", "2"}, {"a", "b", "c"}}] $\endgroup$
    – MelaGo
    May 16, 2021 at 0:32

1 Answer 1

1
$\begingroup$
days = {Sunday, Monday, Tuesday, Wednesday,
   Thursday, Friday, Saturday};

BarChart[data,
 ChartLabels -> {Placed[days, Axis], Automatic},
 ChartLayout -> "Stacked",
 Joined -> True,
 ImageSize -> Large,
 Frame -> True,
 FrameLabel -> {None, Style["Volume", Bold, 12, Blue]}]

enter image description here

$\endgroup$
2
  • $\begingroup$ Bob, may I ask what does usingPlaced[days, Axis] in ChartLabels (ChartLabels -> {Placed[days, Axis], Automatic}) give us vs. the simpler syntax MelaGo offered (ChartLabels -> {days, Automatic})? [They appear to give identical results.] Is it just more options for placement? $\endgroup$
    – theorist
    May 16, 2021 at 3:47
  • 1
    $\begingroup$ It is just what came to mind. The shorter syntax is generally better unless you want the added clarity. $\endgroup$
    – Bob Hanlon
    May 16, 2021 at 3:50

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge that you have read and understand our privacy policy and code of conduct.

Not the answer you're looking for? Browse other questions tagged or ask your own question.