4
$\begingroup$

I have a histogram like the one presented here by @kglr (Combine BarChart and TimeLinePlot). For the sake of brevity, I excluded the issue with timelineplot:

(*some data preprocessing:*)

data = {{2000, 2, 0, 6, 4}, {2001, 0, 0, 8, 14}, {2002, 1, 0, 10, 
    13}, {2003, 4, 0, 8, 9}, {2004, 3, 0, 13, 11}, {2005, 0, 1, 9, 
    7}, {2006, 3, 2, 2, 8}, {2007, 0, 0, 11, 7}, {2008, 6, 1, 8, 
    8}, {2009, 2, 5, 2, 7}, {2010, 4, 2, 11, 14}, {2011, 4, 0, 5, 
    11}, {2012, 6, 2, 11, 10}, {2013, 5, 0, 8, 11}, {2014, 1, 3, 10, 
    7}, {2015, 0, 0, 9, 6}};
makeshares[{year_, amb_, neg_, neu_, pos_}] := 
  Module[{total, shares}, total = Total[{amb, neg, neu, pos}]; 
   shares = {year, pos/total // N, neu/total // N, amb/total // N, 
     neg/total // N}];
data2 = makeshares[#] & /@ data;
data2shares = 
  WeightedData[data2[[All, {1}]], data2[[All, #]]] & /@ {2, 3, 4, 5};

The histogram

data2histogramm = 
 DateHistogram[data2shares, "Year", ChartLayout -> "Stacked", 
  ChartStyle -> {Opacity[0.9, Darker[Blue]], Opacity[0.7, Pink], 
    Opacity[0.7, Orange], Opacity[0.9, Darker[Yellow]]}, 
  ImageSize -> {700, 500}, 
  Ticks -> {AbsoluteTime /@ 
     Thread[{tradeevaluationsdata2[[All, 1]], 7, 15}], Automatic}, 
  Frame -> {{True, False}, {True, False}}, 
  DateTicksFormat -> {"Year"}, ChartBaseStyle -> Opacity[1], 
  PlotRangeClipping -> False, 
  ImagePadding -> {{Automatic, 50}, {140, 10}}]

yields

enter image description here

Now, inspired by a custom ticks function presented here: How to rotate TickLabels in DateListPlot, I tried to rotate the ticks:

rotatedDateTicksF[n_] := 
  MapAt[Rotate[#, Pi/2] &, 
    System`DateListPlotDump`DateTicks[{##}, n, {"Year"}], {All, 2}] &;

the new histogram

data2histogramm2 = 
 DateHistogram[data2shares, "Year", ChartLayout -> "Stacked", 
  ChartStyle -> {Opacity[0.9, Darker[Blue]], Opacity[0.7, Pink], 
    Opacity[0.7, Orange], Opacity[0.9, Darker[Yellow]]}, 
  ImageSize -> {700, 500}, 
  FrameTicks -> {{Automatic, 
     Automatic}, {rotatedDateTicksF[15][{2000}, 
      DatePlus[{2000}, {15, "Year"}]], Automatic}}, 
  Frame -> {{True, False}, {True, False}}, 
  ChartBaseStyle -> Opacity[1], PlotRangeClipping -> False, 
  ImagePadding -> {{Automatic, 50}, {120, 10}}]

yields:enter image description here

Unfortunately, the ticks are on the left edge below each bar, so how can I put them into the middle (below the bar)?

$\endgroup$
  • $\begingroup$ Ticks functionality needs an update soon! $\endgroup$ – M.R. Jan 31 at 22:21
  • 1
    $\begingroup$ @M.R. Yes, I sure hope they give it some TLC soon. It's easily my highest friction interaction with Mathematica, though perhaps this is not so for others. CustomTicks usually does everything I need, but it's a pain when collaborating or working in the Cloud. $\endgroup$ – MassDefect Feb 1 at 5:42
5
$\begingroup$

Use

FrameTicks -> {{Automatic, Automatic},
 {MapAt[DatePlus[#, {6, "Month"}] &, 
    rotatedDateTicksF[15][{2000}, DatePlus[{2000}, {15, "Year"}]], {All, 1}], 
  Automatic}}

to get

enter image description here

$\endgroup$

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

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