[Tex/LaTex] Legend entry not showing correct image

legendpgfplots

I don't know why the legend does not reflect the correct image for its respective entry. I want the last two entries to just show the marker and not the line (dashed line). I set the last two to just be markers (diamond and triangle) but they are not shown in the legend. It works fine for the first entry (data points). Please help. Thanks in advance.

output result

\begin{figure}[!htbp]
\captionsetup{width=0.9\textwidth}
\centering
\begin{tikzpicture}[trim left]
\pgfplotsset{width=\textwidth,
    compat=1.3,
    legend style={font=\footnotesize}}
\begin{axis}[
scaled y ticks = false,
xlabel={concentration[$ppm$]},
ylabel={Area$\times 10^{3}$},
legend cell align=left,
legend pos=north west,
xmin=0, ymin=0,
ignore zero=y]
\addplot[only marks] table[x=X,y=Y]{
    X Y
    0   8.046
    5   302.329
    10  606.470
    20  1122.1465
    30  1666.8665
};
\addlegendentry{data points}
\addplot[no marks] table[y={create col/linear regression={y=Y}}] % compute a linear regression from the
%input table
{
    X Y
    0   8.046
    5   302.329
    10  606.470
    20  1122.1465
    30  1666.8665
};
\addlegendentry{%
    $\pgfmathprintnumber{\pgfplotstableregressiona} \cdot x
    \pgfmathprintnumber[print sign]{\pgfplotstableregressionb}$ lin. regression} 
    \addplot[no marks, dashed] table[x=X,y=Y]{
    X Y
    1.390942009 162.4025826
    5 360.5841458
    10 635.1454906
    20 1184.26818
    30 1733.39087
};
\addlegendentry{upper and lower CI}
\addplot[no marks, dashed] table[x=X,y=Y]{
    X Y
    1.390942009 44.98118766
    5 243.1627508
    10 517.7240956
    20 1066.846785
    30 1615.969475
};
\addplot[no marks, dotted, very thick] table[x=X,y=Y]{ 
    X Y
    0 103.6918851
    1.390942009 103.6918851
};
\addplot[no marks, dotted, very thick] table[x=X,y=Y]{ 
    X Y
    1.390942009 103.6918851
    1.390942009 1.390942009
};
\addplot[only marks, mark=triangle, mark size=4] table[x=X,y=Y]{ 
    X Y
    10.40635194 598.7485
};
\addlegendentry{Unknown A}
\addplot[only marks, mark=diamond, mark size=4] table[x=X,y=Y]{ 
    X Y
    13.1795209 751.0295
};
\addlegendentry{Unknown B}
\node at (axis cs:1.8,60) [anchor=west] {\textbf{LoD}};
\end{axis}
\end{tikzpicture}
\caption{Linear regression curve for caffeine using HPLC analysis, Limit of detection (LoD) also shown}
\end{figure}

Best Answer

You have to reverse the order of the plots to get the right curves at the legend entries. \addlegendentry adds a legend entry for each plot, plot after plot (so, in your example for the two dashed curves).

I also added some code so that the code compiles...

enter image description here

\documentclass{standalone}
\usepackage{pgfplots}
\usepackage{pgfplotstable}

\begin{document}


\begin{tikzpicture}[trim left]
\pgfplotsset{width=\textwidth,
    compat=1.3,
    legend style={font=\footnotesize}}
\begin{axis}[
scaled y ticks = false,
xlabel={concentration[$ppm$]},
ylabel={Area$\times 10^{3}$},
legend cell align=left,
legend pos=north west,
xmin=0, ymin=0,
%ignore zero=y
]
\addplot[only marks] table[x=X,y=Y]{
    X Y
    0   8.046
    5   302.329
    10  606.470
    20  1122.1465
    30  1666.8665
};
\addlegendentry{data points}
\addplot[no marks] table[y={create col/linear regression={y=Y}}] % compute a linear regression from the
%input table
{
    X Y
    0   8.046
    5   302.329
    10  606.470
    20  1122.1465
    30  1666.8665
};
\addlegendentry{%
    $\pgfmathprintnumber{\pgfplotstableregressiona} \cdot x
    \pgfmathprintnumber[print sign]{\pgfplotstableregressionb}$ lin. regression} 
    \addplot[no marks, dashed] table[x=X,y=Y]{
    X Y
    1.390942009 162.4025826
    5 360.5841458
    10 635.1454906
    20 1184.26818
    30 1733.39087
};
\addlegendentry{upper and lower CI}
\addplot[only marks, mark=triangle, mark size=4] table[x=X,y=Y]{ 
    X Y
    10.40635194 598.7485
};
\addlegendentry{Unknown A}
\addplot[only marks, mark=diamond, mark size=4] table[x=X,y=Y]{ 
    X Y
    13.1795209 751.0295
};
\addlegendentry{Unknown B}
\addplot[no marks, dashed] table[x=X,y=Y]{
    X Y
    1.390942009 44.98118766
    5 243.1627508
    10 517.7240956
    20 1066.846785
    30 1615.969475
};
\addplot[no marks, dotted, very thick] table[x=X,y=Y]{ 
    X Y
    0 103.6918851
    1.390942009 103.6918851
};
\addplot[no marks, dotted, very thick] table[x=X,y=Y]{ 
    X Y
    1.390942009 103.6918851
    1.390942009 1.390942009
};
\node at (axis cs:1.8,60) [anchor=west] {\textbf{LoD}};
\end{axis}
\end{tikzpicture}

\end{document}