0

I have a scatter graph drawn using pgfplots from here, I have added a linear regression line using pgfplotstable.

Graph

As you can see, there appears to be one statistically significant outlier (more than 2 standard deviations away from the line of best fit) at about (2.5e-3,5.8).

How would I go about circling this, or otherwise indicating that it is an outlier, on my graph?

Ideally, I'd like pgfplotstable to calculate the standard deviation of the linear regression and then check to see if each point is an outlier individually. However, if this cannot be done, how would I got about manually circling, or changing the symbol used on, that one point?

Edit: MWE:

\documentclass{article}
\usepackage{filecontents}
\usepackage{tikz,pgfplots}
\usepackage{pgfplotstable} 
\begin{filecontents}{GraphData.txt}
   lnPa    1/T
6.5723  0.002304
6.7214  0.002273
6.7178  0.002273
6.7117  0.002278
6.7178  0.002278
6.6783  0.002283
6.6490  0.002288
6.6254  0.002294
6.6026  0.002299
6.5820  0.002304
6.5539  0.002309
6.4907  0.002320
6.4281  0.002331
6.3509  0.002347
6.2823  0.002364
6.2066  0.002381
6.1312  0.002381
6.0707  0.002404
6.0113  0.002421
5.8861  0.002445
5.8141  0.002506
5.7236  0.002475
5.6490  0.002494
5.5759  0.002506
5.5134  0.002519
5.3660  0.002545
5.2679  0.002571
5.1761  0.002584
5.0876  0.002597
5.0239  0.002611
4.9628  0.002625
4.8283  0.002646
4.7791  0.002660
\end{filecontents}


\begin{document}

\begin{tikzpicture}
\begin{axis}[legend pos=north east,anchor=west, xlabel={$T^{-1}/ K^{-1}$}, ylabel={$\ln P*_a$}]
\addplot table [only marks, x={1/T},y={lnPa}]{GraphData.txt};
\addplot [thick, red] table[x={1/T},
    y={create col/linear regression={y={lnPa}}}
] % compute a linear regression from the input table
{GraphData.txt};
%\addlegendentry{$\dfrac{dy}{dx} = \pgfplotstableregressiona$}
%\addlegendentry{$y_{intercept} = \pgfplotstableregressionb$}
\end{axis}
\end{tikzpicture}
\end{document}
5
  • 2
    maybe \draw (<x>,<y>) circle [radius=0.5];
    – percusse
    Oct 24, 2017 at 15:07
  • Welcome to TeX.SX! Could you add a minimal working example (MWE). It will be much easier to write an answer when we see compilable code, starting with \documentclass and ending with \end{document}. - percusse hint (manually circling) should work well.
    – Bobyandbob
    Oct 24, 2017 at 15:23
  • @Bobyandbob I added what you need... The op has added legents and have reversed the x-y columns to avoid usage of x={1/T} in his(her) addplots... but you can run it now. If (s)he want he can edit later.
    – koleygr
    Oct 24, 2017 at 15:47
  • You could adapt the solution shown at tex.stackexchange.com/a/335625 to solve your problem when you know which points are the outliers. Oct 24, 2017 at 16:35
  • @Bobyandbob I deliberately didn't provide a MWE because I was looking for someone to point me in the right direction rather than to do my work for me (I always feel bad when someone does that) but thanks for your help everyone! Oct 25, 2017 at 11:22

1 Answer 1

3

Here the outlier is searched automatically by comparing the distance from the linear regression in y-dirction against an allowed maximum. For this a y-filter is used and a third plot is drawn.

Loading the table in the preamble is not realy necessary, but this way it has to be read only once from file.

The filter macro takes the allowed distance as its parameter. This way it is easy to adapt it to your needs. (Try 0.05, this will also highlight the other outlier.)

The result looks like this:

enter image description here

The code:

\documentclass{article}
\usepackage{filecontents}
\usepackage{tikz,pgfplots}
\usepackage{pgfplotstable} 
\begin{filecontents}{GraphData.txt}
   lnPa    1/T
6.5723  0.002304
6.7214  0.002273
6.7178  0.002273
6.7117  0.002278
6.7178  0.002278
6.6783  0.002283
6.6490  0.002288
6.6254  0.002294
6.6026  0.002299
6.5820  0.002304
6.5539  0.002309
6.4907  0.002320
6.4281  0.002331
6.3509  0.002347
6.2823  0.002364
6.2066  0.002381
6.1312  0.002381
6.0707  0.002404
6.0113  0.002421
5.8861  0.002445
5.8141  0.002506
5.7236  0.002475
5.6490  0.002494
5.5759  0.002506
5.5134  0.002519
5.3660  0.002545
5.2679  0.002571
5.1761  0.002584
5.0876  0.002597
5.0239  0.002611
4.9628  0.002625
4.8283  0.002646
4.7791  0.002660
\end{filecontents}

% read table
\pgfplotstableread[]{GraphData.txt}\graphdata
% add column with linear regression
\pgfplotstablecreatecol[linear regression={x={1/T},y={lnPa}}]{linreg}{\graphdata}

% macro for y-filter
\newcommand*{\outlierfilter}[1]{%
  % set the allowed distance in y-direction from parameter
  \pgfmathsetmacro{\alloweddist}{#1}%
  % store y-value
  \pgfmathsetmacro{\ypoint}{\pgfmathresult}%
  % get value from linear regression
  \pgfplotstablegetelem{\coordindex}{[index]2}\of\graphdata
  % and store it
  \pgfmathsetmacro{\linregpoint}{\pgfplotsretval}%
  % calculate the absolute distance from y to linear regression
  \pgfmathsetmacro{\currdist}{abs(\ypoint - \linregpoint)}%
  % check against the allowed distance and set \pgfmathresult accordingly
  \pgfmathparse{ifthenelse(\currdist>\alloweddist,\ypoint,nan)}%
}



\begin{document}

\begin{tikzpicture}
\begin{axis}[legend pos=north east,anchor=west, xlabel={$T^{-1}/ K^{-1}$}, ylabel={$\ln P*_a$}]
\addplot table [only marks, x={1/T},y={lnPa}]{\graphdata};
\addplot [thick, red] table[x={1/T},y={linreg}]{\graphdata};
\addplot [cyan,unbounded coords=jump,only marks,mark=o,mark size=4pt,
          y filter/.code={\outlierfilter{0.1}}]
          % set allowed distance here ---^^^
    table[x={1/T},y={lnPa}]{\graphdata};
%\addlegendentry{$\dfrac{dy}{dx} = \pgfplotstableregressiona$}
%\addlegendentry{$y_{intercept} = \pgfplotstableregressionb$}
\end{axis}
\end{tikzpicture}
\end{document}
0

You must log in to answer this question.

Not the answer you're looking for? Browse other questions tagged .