Mr Alpha's answer is correct. Epilog
(or Prolog
) is the right way to get such annotations onto this or any other kind of graph. One caveat, though: if you are using Epilog
within the function definition to get some other effect, e.g. Tufte-style dots for minimum and maximum data points or whatever it may be, you need to make sure that the function definition you are using can incorporate both the special line you want, and the standard annotations provided in your function.
While perhaps a little complex for this illustration, here is some code I happen to have lying around that does sparkline plots, complete with a range of options.
Options[SparklinePlot] = {ShowMinMax -> True, ShowSDBar -> False,
LastDotColor -> Red, MinMaxColor -> Hue[0.6],
SDBarColor -> {GrayLevel[0.4], Opacity[0.2]},
SparktextStyle -> {FontFamily -> "Gill Sans", FontSize -> 9},
DateStringForm -> {"DayShort", "/", "MonthShort", "/", "YearShort"}};
SparklinePlot[dateddata : {{_List, _?NumericQ} ..}, title_String,
opts:OptionsPattern[{SparklinePlot,DateListPlot}]] :=
With[{data = dateddata[[All, 2]]},
With[{showmax = OptionValue[ShowMinMax], showsdbar = OptionValue[ShowSDBar ],
ldcolor = OptionValue[LastDotColor], mmcolor = OptionValue[MinMaxColor],
sdbcolor = OptionValue[SDBarColor], textstyle = OptionValue[SparktextStyle],
epi = OptionValue[Epilog], datestringform = OptionValue[DateStringForm ],
mind = Min[data], maxd = Max[data], sd = StandardDeviation[data], ave = Mean[data]
}, Grid[{{Graphics[
DateListPlot[dateddata,Epilog :> {epi}~Join~{ldcolor, AbsolutePointSize[4.5],
Point[Last[dateddata]],
Sequence @@ If[showmax, {mmcolor,
Point[dateddata[[Position[data, mind][[1, 1]]]]],
Point[dateddata[[
Position[data, maxd][[1, 1]]]]]}, {mmcolor}],
Sequence @@ If[showsdbar, {Sequence @@ sdbcolor,
Rectangle[{First[First[dateddata]],
ave - sd}, {First[Last[dateddata]],
ave + sd}]}, {mmcolor}]
}, FilterRules[{opts}, Options[DateListPlot]], Joined -> True, Frame -> False,
Axes -> False, PlotRangePadding -> {Scaled[5/Length[data]], 0.4 sd},
AspectRatio -> 0.2, PlotStyle -> {GrayLevel[0], AbsoluteThickness[0.75]},
ImageSize -> 150 ], ImagePadding -> 1, ImageMargins -> 0],
Graphics[
Style[Text[ToString[NumberForm[Last[data], 4]]], ldcolor,
Sequence @@ textstyle], ImageSize -> 30, AspectRatio -> 1/4],
If[showmax,
Graphics[
Style[Text[ToString[NumberForm[mind, 4]]], mmcolor,
Sequence @@ textstyle], ImageSize -> 30, AspectRatio -> 1/4],
Graphics[Text[""], ImageSize -> 0.2]],
If[showmax,
Graphics[
Style[Text[ToString[NumberForm[maxd, 4]]], mmcolor,
Sequence @@ textstyle], ImageSize -> 30, AspectRatio -> 1/4],
Graphics[Text[""], ImageSize -> 0.2]],
Graphics[
Style[Text[title], Black, TextAlignment -> Left,
Sequence @@ textstyle],
ImageSize -> Max[23, StringLength[title]*4.3],
AspectRatio -> 1/StringLength[title]]},
{Graphics[
Text[Style[DateString[First[First[dateddata]], datestringform],
Black, TextAlignment -> Left, Sequence @@ textstyle],
TextAlignment -> Left], ImageSize -> 40, AspectRatio -> 1/6],
Graphics[
Style[Text[DateString[First[Last[dateddata]], datestringform]],
ldcolor, Sequence @@ textstyle], ImageSize -> 40,
AspectRatio -> 1/6],
If[showmax,
Graphics[
Style[Text[
DateString[First[dateddata[[Position[data, mind][[1, 1]]]]],
datestringform]], mmcolor, Sequence @@ textstyle],
ImageSize -> 40, AspectRatio -> 1/4],
Graphics[Text[""], ImageSize -> 0.2]],
If[showmax,
Graphics[
Style[Text[
DateString[First[dateddata[[Position[data, maxd][[1, 1]]]]],
datestringform]], mmcolor, Sequence @@ textstyle],
ImageSize -> 40, AspectRatio -> 1/4],
Graphics[Text[""], ImageSize -> 0.2]],
Graphics[Text[""], ImageSize -> 0.2]}},
Alignment -> {Join[{Left},
If[showmax, Table[Right, {3}], Table[Right, {1}]]],
Join[{Left},
If[showmax, Table[Right, {3}], Table[Right, {1}]], {Left}] },
Spacings -> {0, -0.2}]
]]
Things to note include
- the use of both the name of this function and
DateListPlot
in the pattern opts:OptionsPattern[{SparklinePlot,DateListPlot}]
in the function call definition;
- catching of the
OptionValue[Epilog]
in the local constant epi
, which is then joined to the other things defined to be in the Epilog
;
- the way the
Epilog
option is constructed before the other options passed to DateListPlot
are actually passed. This is important so that you don't inadvertently set the Epilog
option before inserting the elements defined in the body of the function definition, as opposed to passed in the function call by the user.
Here is an illustration of the function:
testdata =
Transpose[{DatePlus[{2000, 1}, {#, "Month"}] & /@ Range[0, 119],
FoldList[0.99 #1 + #2 &, 0., RandomVariate[NormalDistribution[0, 1], 119 ]]}];
SparklinePlot[testdata, "Random Series", ShowSDBar -> True,
Epilog -> Line[{{{2005}, -11}, {{2005}, 11}}]]

GridLines
. $\endgroup$