3
$\begingroup$

I have the following.

dataMarch2016 =incomeByDay[Select[
    DateValue[#Date, {"Year", "Month"}] == {2016, 3} &]]

March 2016

And the following.

dataMarch2017 =incomeByDay[Select[
    DateValue[#Date, {"Year", "Month"}] == {2017, 3} &]]

March 2017

The question is how can I plot both sets on the same plot such that each point on the horizontal axes be common to both lines? It would be nice if I can put an indicator on the peaks of the points to tell me how much March 2017 increased or decreased from March 2016.

Thank you.

$\endgroup$
1
$\begingroup$

First some fake data:

dates = With[{first = {2010, 1, 1}}, DateRange[first, DatePlus[first, {3, "Year"}]]];
values = 100 + RandomFunction[ARProcess[{0.7}, 1], {1, Length@dates}]["States"][[1]];
data = Transpose[{dates, values}];
months = {"January", "February", "March", "April", "May", "June", 
   "July", "August", "September", "October", "November", "December"};

Organize the data using GatherBy (gather first by month and then by year), to generate some data with components similar to your dataMarch2016 and dataMarch2017.

marchdata = {dataMarch2013, dataMarch2014, dataMarch2015} = 
   Select[GatherBy[data, {#[[1, 2]] &, #[[1, 1]] &}], #[[1, 1, 1, 2]] == 3 &][[1]];

Use the option setting DateFunction :> (Join[{someyear}, #[[2 ;;]]] &) for some arbitrary someyear to force DateListPlot to ignore different years in the data to be plotted:

DateListPlot[marchdata, Joined -> True, Filling -> Axis, 
 DateFunction :> (Join[{2000}, #[[2 ;;]]] &), 
 PlotLegends -> LineLegend[{"2013", "2014", "2015"}]]

enter image description here

Manipulate[md = Select[GatherBy[data, {#[[1, 2]] &, #[[1, 1]] &}], 
    #[[1, 1, 1, 2]] == month &][[1]];
  DateListPlot[md, Joined -> True, Filling -> Axis, 
    DateFunction :> (Join[{2000}, #[[2 ;;]]] &), 
    PlotLegends -> LineLegend[{"2014", "2015", "2016"}]],
  {{month, 3, ""}, Thread[Range[12] -> months], ControlType -> SetterBar}, {md, None}]

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.