# Trend line with pgfplots and dateplots

Is there an easy way to avoid the following pretty straightforward error message at compile time: Could not parse input '2010-01-01' as a floating point number, sorry. The unreadable part was near '-01-01' when trying to compile:

\documentclass{minimal}
\usepackage{pgfplots}
\usepgfplotslibrary{dateplot}
\begin{filecontents}{data.dat}
date            value1
2010-01-01      2
2010-01-02      10
2010-01-03      8
2010-01-04      15
\end{filecontents}
\usepackage{pgfplotstable}
\begin{document}
\begin{tikzpicture}

\begin{axis}[
date coordinates in=x,
xticklabel={\day.\month.\year},
xmin={2010-01-01},
xmax={2010-01-05},x tick label style={yshift=-3pt,xshift=-4pt,rotate=-30,anchor=west}]
\addplot table [x=date,y=value1] {data.dat};
\addplot table [x=date, y={create col/linear regression={y=value1}}] {data.dat};
\end{axis}

\end{tikzpicture}
\end{document}


I tried x=\ticknum, but that is not working (plus it is not very safe in case I'm skipping some dates in data.dat).

-
I'd say that is a bug (or missing feature) in pgfplots. Apparently, the input coordinate transformation is not being applied while computing the regression line. The only solution at hand might be to not use the dateplot library here, i.e. to use numeric coordinates and to adjust the xticklabels afterwards. –  Christian Feuersänger Jan 3 '13 at 19:39
Unfortunately I can't choose the format of my input datum. I'm willing not to use dateplot, but I still don't clearly see how I can efficiently convert the input date into numeric coordinates, do the regression, and then change the xticklabels (what I have in mind involve creating a column to the table data.dat, and then plotting the new column + modifying the labels, but I'm hoping there is a more efficient "one-liner". –  BigDawg Jan 3 '13 at 20:00

In general, you can always customize your tick labels manually, without resorting to date coordinates in or symbolic coords. For the records here, I will show how (see my second example in this answer) even though I have something which might fit better (my first example).

But since you asked for some "more efficient one-liner", I found one for you by combining the available styles for xticklabels from table with date coordinates in:

\documentclass{minimal}
\usepackage{pgfplots}
\usepgfplotslibrary{dateplot}
\begin{filecontents}{data.dat}
date            value1
2010-01-01      2
2010-01-02      10
2010-01-03      8
2010-01-04      15
\end{filecontents}
\usepackage{pgfplotstable}

% formats row \ticknum of the input table using '#3'
% #1: table name
% #2: column name
% #3: formatting instruction like \day.\month.\year
\newcommand\datelabelsfromtable[3]{%
\begingroup
\countdef\result=\count200 %
\pgfplotstablegetelem{\ticknum}{#2}\of{#1}%
\pgfcalendardatetojulian{\pgfplotsretval}{\result}%
\pgfcalendarjuliantodate{\result}\year\month\day
%
#3%
\endgroup
}

\begin{document}
\begin{tikzpicture}

\begin{axis}[
x tick label style={yshift=-3pt,xshift=-4pt,rotate=-30,anchor=west},
xticklabel={\datelabelsfromtable{data.dat}{date}{\day.\month.\year}},
xtick=data,
]
\addplot table [x expr=\coordindex,y=value1] {data.dat};
\addplot table [x expr=\coordindex, y={create col/linear regression={y=value1}}] {data.dat};
\end{axis}

\end{tikzpicture}
\end{document}


The answer above is a little bit complicated because the public \pgfcalendar* utility functions require a TeX counter on input and I did not want to waste one with \newcounter. The \countdef simply reuses an existing counter, but the effect is limited by the fact that the assignment is local to a group. The rest of that utility function is rather straight-forward: extract the desired entry from the input table, parse it as date, then convert the parsed representation to the desired output date format. This is not quite as powerful as the dateplot library because it does not compute tick labels automatically and because it does not support hours and minutes, but for the moment, it appears to be the most "one-line" solution at hand.

Note that \addplot table uses x expr=\coordindex, i.e. the row number as x coordinate.

Just for the records, I would like to stress that assigning tick labels manually is always possible, just as for any other plotting tool. Since xticklabels from table does not use the expected output date format, I would use the following for the minimal example:

\documentclass{minimal}
\usepackage{pgfplots}
\usepgfplotslibrary{dateplot}
\begin{filecontents}{data.dat}
date            value1
2010-01-01      2
2010-01-02      10
2010-01-03      8
2010-01-04      15
\end{filecontents}
\usepackage{pgfplotstable}
\begin{document}
\begin{tikzpicture}

\begin{axis}[
x tick label style={yshift=-3pt,xshift=-4pt,rotate=-30,anchor=west},
xticklabels={01.01.2010, 02.01.2010, 03.01.2010, 04.01.2010},
xtick=data,
]
\addplot table [x expr=\coordindex,y=value1] {data.dat};
\addplot table [x expr=\coordindex, y={create col/linear regression={y=value1}}] {data.dat};
\end{axis}

\end{tikzpicture}
\end{document}


The outcome is precisely the same as above.

-