4

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}
  • 2
    Can you add your preamble as well? – Alenanno Jun 28 '15 at 13:07
3

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}
  • great, thanks a ton! a side question, is there a way to multiply the slope of my regression by factor of 1000? i had to scale down the numbers so the y-axis looked easier to read, but for the slope I need it in its original scale. so instead of 54.91x i need it to be 54910 or 54.91x10^3, thanks again – Spencer Trinh Jun 28 '15 at 14:19
  • i think, pgfplotstable can help you here... You can put your data in a table or a file, and when you plot it, do some calcuations... a very short example (with a file) would be something like \pgfplotstableread{./data.txt} \datatable \addplot table [x expr = \thisrow{freq}/1e9] from \datatable {}; – Faekynn Jun 28 '15 at 14:50
  • @SpencerTrinh In your case mabe (untested): \addplot[only marks] table[x=X,y expr =\thisrow{Y}*1e3] – Faekynn Jun 28 '15 at 14:57
  • \addplot[no marks] table[x=X, y={create col/linear regression={y expr=\thisrow{Y}/1e3}}]; is this how to incorporate it for the linear regression? latex doesn't like this. is there a way to multiply or divide the pgfplotsregressiona variable by a number than report it in the legend? – Spencer Trinh Jun 29 '15 at 12:27
  • @SpencerTrinh maybe you could add the following two options to your axis-environment: scaled y ticks=real:1e3, and ytick scale label code/.code={$\cdot 10^3$}. Then you can use the real values in the tables, there's no need for a manual factor on the y-axis, and the regression equation is right... – Faekynn Jun 29 '15 at 13:55

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.