3

I have a dataset in the format shown below, which I have exported from R using export.table() function. I want to make an XY plot using pgfplots, where x=time, and y=value, and the points are connected using straight lines, without any symbol for each point.

no date time value
1 2015-09-14 21:00:00 922
2 2015-09-14 22:00:00 3993
3 2015-09-14 23:00:00 3003
4 2015-09-14 00:00:00 991
5 2015-09-14 01:00:00 2021
6 2015-09-14 02:00:00 841
7 2015-09-14 03:00:00 2812
8 2015-09-14 14:00:00 991
9 2015-09-14 15:00:00 231
10 2015-09-14 16:00:00 678

This is what I tried to create as a minimal working example, which works fine withot the date/time filed (second dataset), but has problems with the first dataset which includes date/time.

% This produces errors with the first dataset, but works fine with the second
\documentclass{article}
\usepackage{tikz}
\usepackage{pgfplots}
\usepgfplotslibrary[statistics]
\pgfplotsset{compat=1.12}

\begin{document}

\begin{tikzpicture}

% fixes the second problem
\pgfplotsset{every axis/.append style={ultra thin}}

\begin{axis}[xlabel=time, ylabel=Value]
\addplot table[x=time,y=value]{data.txt};
\end{axis}
\end{tikzpicture}

\end{document}

I have these problems:

First: I can not read the data using the pgfplots. I get this error:

! Package PGF Math Error: Could not parse input '2015-09-14' as a floating poin
t number, sorry. The unreadable part was near '-09-14'..

I can re-export the data from R to have a format suitable for pgfplots, maybe CSV, if needed. But what is the suiable format?

Fixed, somehow:

Second: The data contains >1000 elements. I don't know how the output will be with those default big dots which pgfplots uses for each point. I couldn't find any examples of pgfplots for high frequency data. My data looks like this:

High frequency data

Third: There is lack of data in some periods of time, which is clearly visible in R plots. This is because R stars a line from the last value before time gap, puts space equal to the time gap, and connects it to the first value after the time gap. This is good to show that there is a time gap. I don't know if I can achieve such thing in pgfplots.

If you wan to test the code, the code actually works fine on this dataset:

time value
1 922
2 3993
3 3003
4 991
5 2021
6 841
7 2812
8 991
9 231
10 678
6

You need to use the dateplot library to read in dates, see page 331 of the v1.12 manual. You also need to save the file as a CSV. Then, the following MWE will plot the first dataset that you listed. Note that the key date coordinates in=x tells pgfplots to look for the date information in the column specified by the x coordinate of the \addplot command. In addition, pgfplots reads the date and time as one column, not as separate columns. You can adjust how the xticklabel is printed by adjusting that key as well; see the specified page in the manual.

Furthermore, you can control the options for a particular plot by setting the options on the \addplot command. I put +[no markers] which tells pgfplots only to overwrite the options I give to the command, but leave all the others (e.g., color, thickness, etc.). Without the +, only the option specified would be set, all others would be default values (e.g., color would be black, etc.).

For your third question, it is better to post one question per... post, so you should probably take what is working for you here and ask a new question specifically about how to deal with gaps in your data. Also, be sure to define how long a "gap" should be.

Finally, pgfplots automatically loads tikz so there is no reason to load the latter, and I used the filecontents package to make a self-contained example.

\documentclass{standalone}
\usepackage{pgfplots}
\usepgfplotslibrary{dateplot, statistics}
\pgfplotsset{compat=1.12}
\usepackage{filecontents}
\begin{filecontents*}{data.txt}
no, date, value
1, 2015-09-13 21:00:00, 922
2, 2015-09-13 22:00:00, 3993
3, 2015-09-13 23:00:00, 3003
4, 2015-09-14 00:00:00, 991
5, 2015-09-14 01:00:00, 2021
6, 2015-09-14 02:00:00, 841
7, 2015-09-14 03:00:00, 2812
8, 2015-09-14 14:00:00, 991
9, 2015-09-14 15:00:00, 231
10, 2015-09-14 16:00:00, 678

\end{filecontents*}
\begin{document}

\begin{tikzpicture}
\begin{axis}[xlabel=time, ylabel=Value, date coordinates in=x, table/col sep=comma, date ZERO=2015-09-13, xticklabel=\day. \hour:\minute, xticklabel style={rotate=90, anchor=near xticklabel}]
\addplot+[no markers] table[x=date,y=value] {data.txt};
\end{axis}
\end{tikzpicture}

\end{document}

enter image description here

  • Thank you for your time and knowledge. :-) It worked fine. The third problem was also got fiexd (mostly), as it worked just as I have described. Although I have some issues with the labels. I use\month as xticklabel style and I get numbers, and strangely, some months are repeated twice. :-/ – Ho1 Sep 14 '15 at 11:55
  • All the dates belong to a single year, but in some month, more data is gathered. – Ho1 Sep 14 '15 at 12:01
  • Yes, I'm not sure how pgfplots decides to format the dates as numbers. You might want to search around for an answer, and if you can't find one, post a new question :-) – darthbith Sep 14 '15 at 12:42
  • Thanks. This one was nice, although it was not completely related: tex.stackexchange.com/questions/20445 – Ho1 Sep 14 '15 at 12:43

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.