10

I am trying to display a trend line with pgfplots. I followed the instruction in the manual and looked around. I though I had implemented it correctly, but there is no regression line, and although the dots display correctly.

Any suggestions?

\begin{tikzpicture}[scale=1]
\begin{axis}[
   /pgf/number format/.cd,
        use comma,
        1000 sep={}]
\addplot [color=blue, only marks] coordinates{
(   1985    ,   293.4445874757  )
(   1986    ,   279.3118605376  )
(   1987    ,   282.6149240816  )
(   1988    ,   275.0321316854  )
(   1989    ,   282.514265914   )
(   1990    ,   265.2009422222  )
(   1991    ,   275.1986687778  )
(   1992    ,   284.7559793069  )
(   1993    ,   276.545687619   )
(   1994    ,   292.6098138043  )
(   1995    ,   302.9776170115  )
(   1996    ,   319.0738164198  )
(   1997    ,   299.9292631959  )
(   1998    ,   361.3646108235  )
(   1999    ,   293.1112261224  )
(   2000    ,   294.2231206931  )
(   2001    ,   283.6616129293  )
(   2002    ,   313.2993810185  )
(   2003    ,   273.69578875    )
(   2004    ,   278.5970550926  )
(   2005    ,   286.7000069643  )
(   2006    ,   262.0665872115  )
(   2007    ,   245.7247934066  )
(   2008    ,   258.789477  )
(   2009    ,   239.1641025714  )
(   2010    ,   243.2148177778  )
(   2011    ,   240.0082707477  )
(   2012    ,   242.0303828571  )
(   2013    ,   230.697315  )};
\addplot table[y={create col/linear regression={y=Y}}]{
(   1985    ,   293.4445874757  )
(   1986    ,   279.3118605376  )
(   1987    ,   282.6149240816  )
(   1988    ,   275.0321316854  )
(   1989    ,   282.514265914   )
(   1990    ,   265.2009422222  )
(   1991    ,   275.1986687778  )
(   1992    ,   284.7559793069  )
(   1993    ,   276.545687619   )
(   1994    ,   292.6098138043  )
(   1995    ,   302.9776170115  )
(   1996    ,   319.0738164198  )
(   1997    ,   299.9292631959  )
(   1998    ,   361.3646108235  )
(   1999    ,   293.1112261224  )
(   2000    ,   294.2231206931  )
(   2001    ,   283.6616129293  )
(   2002    ,   313.2993810185  )
(   2003    ,   273.69578875    )
(   2004    ,   278.5970550926  )
(   2005    ,   286.7000069643  )
(   2006    ,   262.0665872115  )
(   2007    ,   245.7247934066  )
(   2008    ,   258.789477  )
(   2009    ,   239.1641025714  )
(   2010    ,   243.2148177778  )
(   2011    ,   240.0082707477  )
(   2012    ,   242.0303828571  )
(   2013    ,   230.697315  )};
\end{axis}
\end{tikzpicture}
dorien
  • 1,615

2 Answers2

11

First it looks like you need to include the pgfplotstable package.F I had sort of the same problem a few days ago. Writing my data like this solved it for me.:

\usepackage{siunitx}
\usepackage{pgfplots}
\usepackage{pgfplotstable}


\begin{tikzpicture}
    \pgfplotsset{width=10cm,
        compat=1.3,
        legend style={font=\footnotesize}}
    \begin{axis}[
    xlabel={Glukosekonzentration [\si{\gram\per\liter}]},
    ylabel={Absorption $[-]$},
    legend cell align=left,
    legend pos=north west]
    \addplot[only marks] table[row sep=\\]{
        X Y\\
        0.1 0.147\\
        0.1 0.165\\
        0.8 0.918\\
        0.8 1.149\\
    };
    \addlegendentry{Messpunkte}
    \addplot table[row sep=\\,
    y={create col/linear regression={y=Y}}] % compute a linear regression from the
    %input table
    {
        X Y\\
        0.1 0.147\\
        0.1 0.165\\
        0.8 0.918\\
        0.8 1.149\\
    };
    \addlegendentry{%
        $\pgfmathprintnumber{\pgfplotstableregressiona} \cdot x
        \pgfmathprintnumber[print sign]{\pgfplotstableregressionb}$ lin. Regression} %
    \end{axis}
    \end{tikzpicture}

In this example you tell pgfplots the points belonging to the Y axes so it is able to calculate it.

The result looks like this:

enter image description here

David Carlisle
  • 757,742
Rico
  • 6,097
3

enter image description here

Check out leastsquares function from the Asymptote stats module. tr.asy:

import graph;
import markers;
import stats;
real[] xp={
 1985
,1986
,1987
,1988
,1989
,1990
,1991
,1992
,1993
,1994
,1995
,1996
,1997
,1998
,1999
,2000
,2001
,2002
,2003
,2004
,2005
,2006
,2007
,2008
,2009
,2010
,2011
,2012
,2013
};
real[] yp={
 293.4445874757
,279.3118605376
,282.6149240816
,275.0321316854
,282.514265914 
,265.2009422222
,275.1986687778
,284.7559793069
,276.545687619 
,292.6098138043
,302.9776170115
,319.0738164198
,299.9292631959
,361.3646108235
,293.1112261224
,294.2231206931
,283.6616129293
,313.2993810185
,273.69578875  
,278.5970550926
,286.7000069643
,262.0665872115
,245.7247934066
,258.789477  
,239.1641025714
,243.2148177778
,240.0082707477
,242.0303828571
,230.697315  
};

real w=300; real h=200;
size(w,h,IgnoreAspect);

real xmin=1984; real xmax=2014;
real ymin=50; real ymax=400;
fixedscaling((xmin,ymin),(xmax,ymax));

frame mark=newframe;
pen markPen=darkblue+0.8pt;
real markScale=2.2;
filldraw(mark,scale(markScale)*polygon(5),yellow,markPen);

linefit lf=leastsquares(xp,yp);
//  struct linefit {
//    real m,b;     // slope, intercept
//    real dm,db;   // standard error in slope, intercept
//    real r;       // correlation coefficient
//    real fit(real x) {
//      return m*x+b;
//    }
//  }
defaultpen(fontsize(10pt));
string trendStr="$y="+format("%4.2f",lf.m)+"x+"+format("%4.2f",lf.b)+"$";

draw(graph(xp,yp),nullpen, legend="data", marker(mark));

pen trendPen=red+1.3pt+opacity(0.7);
guide trendLine=graph(lf.fit,xmin+1,xmax-1);

draw(trendLine,legend=trendStr,trendPen);

add(legend(),point(S),-20S,UnFill);

xaxis(YEquals(ymin),xmin,xmax,RightTicks(Step=5,step=1));
yaxis(XEquals(xmin),ymin,ymax,LeftTicks(Step=50,step=25,beginlabel=false));

Run asy -f pdf tr.asy to get a standalone tr.pdf.

g.kov
  • 21,864
  • 1
  • 58
  • 95
  • +1, that's really useful! In general, I think it would be useful if Asymptote solutions were presented embedded in LaTeX documents, using the document font, since I think that's one of the great things about Asymptote and also what makes it relevant for the site (otherwise one could also post a pure R solution, for instance). – Jake Jun 04 '13 at 11:35
  • @Jake: Thanks. I see a big misunderstanding here. Pure R or, say, gnuplot solutions are fundamentally different in this case. Should we also suggest to post METAPOST solutions embedded in LaTeX documents too? Because the Asymptote is an extension of the METAPOST in exactly the same way as LuaTeX is to TeX, and it is part of the TeXLive distribution, which I believe R is not. – g.kov Jun 04 '13 at 13:05
  • I'm sorry, I didn't mean to step on your toes. I don't understand how standalone Asymptote solutions are fundamentally different from R or gnuplot, though: Taken by themselves, they allow you to generate a PDF of a visualization that can then be included in LaTeX. gnuplot is also included in TeXLive. Should we give standalone gnuplot answers for that reason? Maybe I'm missing something, but Asymptote is "only" related to TeX in the way that it can use TeX to typeset labels, right? – Jake Jun 04 '13 at 13:28
  • @Jake These are not my toes anyway :) and perhaps I'm not the right person to talk about it. I'm just trying to study the beautiful world of TeX & friends. The Asymptote is (almost) useless without working TeX system, it not only can, it was designed to use TeX/LaTeX/XeLaTeX to typeset labels, plus it uses the same algorithm to build cubic Bezier splines, as is used in METAFONT/METAPOST, and it extends it to 3D and to cubic Bezier surfaces; it uses PSTricks pstextpath macro to fit labels along a path, etc, etc. – g.kov Jun 04 '13 at 15:21
  • Ah, okay, it is much more intertwined with TeX than I thought, then. Also, I was wrong about gnuplot being in TeXLive; it is on CTAN, though. Sorry for the confusion. – Jake Jun 04 '13 at 16:13