3

I would like to draw a scatterplot like create scatter plot from dat file of a table containing data (actually in an external file) structured label, x=x, and y=y, but keep getting errors with the label names. LaTex reads Direct Care as two separate columns, how would I correct this? Can be using Tikz, pgfplot or other at your discretion. No need for data labels however.

\documentclass{article}
\usepackage{pgfplots,filecontents}
\pgfplotsset{compat=1.7}
\begin{filecontents*}{mydata.dat}
label x y
Direct Care 3   1
Housekeeping    2.366666667 5
Mealtimes   1   1
Medication Round    2.7 7
Miscellaneous   0.883333333 1
Personal Care   8   5
\end{filecontents*}
\begin{document}
\begin{figure}
\centering
\begin{tikzpicture}
    \begin{axis}[xlabel=Time(mins),ylabel=Surface contacts]
        \addplot[
                visualization depends on={\thisrow{nodes}\as\myvalue},
            scatter/classes={
                Direct Care={mark=*,blue},
                Housekeeping={mark=*,red},
                Mealtimes={mark=*,blue},
                Medication Round={mark=*,red},
                Miscellaneous={mark=*,blue},
                Personal Care={mark=*,red},
                },
                scatter, only marks,
                scatter src=explicit symbolic,
                ]
         table[meta=label,x=x,y=y]
            {mydata.dat};
    \end{axis}
\end{tikzpicture}

\end{figure}

\end{document}
HCAI
  • 3,325

1 Answers1

4

You'll need to either use a column separator that's not white space (commas, for example), or enclose the multi-part entries in {...}:

\documentclass{article}
\usepackage{pgfplots,filecontents}
\pgfplotsset{compat=1.7}
\begin{filecontents*}{mydata.dat}
label x y
{Direct Care} 3   1
Housekeeping    2.366666667 5
Mealtimes   1   1
{Medication Round}    2.7 7
Miscellaneous   0.883333333 1
{Personal Care}   8   5
\end{filecontents*}
\begin{document}
\begin{figure}
\centering
\begin{tikzpicture}
    \begin{axis}[xlabel=Time(mins),ylabel=Surface contacts]
        \addplot[
            scatter/classes={
                Direct Care={mark=*,blue},
                Housekeeping={mark=*,red},
                Mealtimes={mark=*,blue},
                Medication Round={mark=*,red},
                Miscellaneous={mark=*,blue},
                Personal Care={mark=*,red}
                },
                scatter, only marks,
                scatter src=explicit symbolic,
                ]
         table[meta=label,x=x,y=y]
            {mydata.dat};
    \end{axis}
\end{tikzpicture}

\end{figure}

\end{document}
Jake
  • 232,450
  • superb, thank you! I'll just use find/replace to add brackets. By the way is there a way of separating the points into separate plots based on the label? I.e. the would be 6 plots in total and potentially a line of best fit in each. I've seen pgfplots capable of this I think. Is it relatively easy do you think? – HCAI Mar 19 '13 at 14:09
  • @user1134241: It would take a bit of work to do that completely automatically (it's not too hard to selectively display only data for a particular label, though). Maybe it's best if you ask a new question for that (and accept this answer if you consider the question answered). – Jake Mar 19 '13 at 14:15
  • OK, this-> http://tex.stackexchange.com/questions/50640/adjust-size-and-position-of-a-pgfplots-plot Looks amazing! I think this-> http://tex.stackexchange.com/questions/29303/how-to-get-the-domain-for-the-line-of-best-fit-w-pgfplots-to-be-all-real-numbers might answer the linear regression part. I'm happy to do the plotting manually (I think). How do you quickly selectively display data for a particular label? Again many thanks! – HCAI Mar 19 '13 at 14:35
  • 1
    @user1134241: See Filter rows from a table for the filtering. – Jake Mar 19 '13 at 14:37
  • Your help is very much appreciated. – HCAI Mar 19 '13 at 14:46