[Tex/LaTex] Trend line in pgf plot

errorspgfplotspgfplotstabletikz-pgf

I have been trying to fit a trend line using pgf plot for my data and unfortunately can't seem to hit the right spot with the code. I have looked in the manual for examples of linear regression and unfortunately it has been of no help. I already know the equation of the line from excel which calculates it to be $y=21208x+346.68$. Can anyone think of how I can fix this? To top it off now my code presented below is not correct!

    \documentclass{article}
    \usepackage{textcomp}
    \usepackage{pgfplots}
    \usepackage{pgfplotstable}
    \pgfplotsset{compat = newest}


    \begin{document}

    \begin{tikzpicture}
    \begin{axis}[
        title={BET Plot of Mesoporous Solid},
        xlabel={Relative pressure $P/P_0\times 10 ^{-3}$},
        ylabel={$\frac{P/P_0}{V(1-P/P_0)}$ [$m^{-3}$]},
        xmin=0, xmax=250,
        ymin=0, ymax=5000,
        xtick={0,50,100,150,200,250},
        ytick={0,1000,2000,3000,4000,5000},
        legend pos=north west,
        ymajorgrids=true,
        grid style=dashed,
    ]

    \addplot[
        color=black,
        mark=square,
        ]
        coordinates {
        (29.9025,905.43528)(63.7920,1716.5088)(97.6815,2471.0442)(122.6003,2977.7455)(145.5255,3470.4408)(174.4313,4060.0613)(194.3663,4448.5030)(208.3208,4708.7817) };

    \addplot[    
        \addplot table[y={create col/linear regression={y=Y}}]{
        (29.9025,905.43528)(63.7920,1716.5088)(97.6815,2471.0442)(122.6003,2977.7455)(145.5255,3470.4408)(174.4313,4060.0613)(194.3663,4448.5030)(208.3208,4708.7817) };

    \end{axis}
    \end{tikzpicture}
\end{document}

Best Answer

You use \addplot table, but then provide a coordinate stream instead of a table. Also, you say y=Y, but there's no column called Y in your data:

\documentclass{article}


    \usepackage{pgfplots}
    \usepackage{pgfplotstable}
    \pgfplotsset{compat = newest}


    \begin{document}

    \begin{tikzpicture}
    \begin{axis}[    
        title={BET Plot of Mesoporous Solid},
        xlabel={Relative pressure $P/P_0\times 10 ^{-3}$},
        ylabel={$\frac{P/P_0}{V(1-P/P_0)}$ [$m^{-3}$]},
        xmin=0, xmax=250,
        ymin=0, ymax=5000,
        xtick={0,50,100,150,200,250},
        ytick={0,1000,2000,3000,4000,5000},
        legend pos=north west,
        ymajorgrids=true,
        grid style=dashed,
    ]

    \addplot[
        color=black,
        mark=square,
        ]
        coordinates {
        (29.9025,905.43528)(63.7920,1716.5088)(97.6815,2471.0442)(122.6003,2977.7455)(145.5255,3470.4408)(174.4313,4060.0613)(194.3663,4448.5030)(208.3208,4708.7817) };

        \addplot [thick, red] table[y={create col/linear regression}]{
        29.9025 905.43528
        63.7920 1716.5088
        97.6815 2471.0442
        122.6003 2977.7455
        145.5255 3470.4408
        174.4313 4060.0613
        194.3663 4448.5030
        208.3208 4708.7817
        };



    \end{axis}
    \end{tikzpicture}
\end{document}