# How to modify the image of an legend entry

I have the following code

\documentclass[tikz,convert={size=640}]{standalone}
\usepackage{pgfplots}
\begin{document}
\begin{tikzpicture}
\begin{axis}[
legend entries={simulation sample 1,
measurement sample 1,
simulation sample 2,
measurement sample 2},
legend pos=outer north east]
(0,0)
(1,1)
(2,2)
};
(0,1)
(1,1.5)
(2,1.8)
};
(3,0.5)
(4.5,1)
(5,2)
};
(3,0)
(4,1)
(5,2.2)
};
\end{axis}
\end{tikzpicture}
\end{document}


which produces this image:

But now I want to group my legend entries and only have an entry with line color blue to indicate the simulations and one with line color green for measurements and then one for the circle marker for the sample 1 and one for the square marker to indicate the sample 2. How do I achieve this? Can I insert a ghost plot which is only visible in the entries, or can I use the \addlegendimage command to adjust the legend image?

-

Here's slightly different version of Martin H's excellent answer that works without the \addlegendentry commands and uses the legend entries key instead (I find that to be more readable). The legend entries are assigned to the \addplot and \addlegendimage commands in the order they're given, so if you move the \addlegendimages before the \addplots (or if you add forget plot to the \addplot options), you can use the legend entries key:

\documentclass[border=5mm]{standalone}
\usepackage{pgfplots}
\begin{document}
\begin{tikzpicture}
\begin{axis}[
legend entries={simulation,
measurement,
sample 1,
sample 2},
legend pos=outer north east]
(0,0)
(1,1)
(2,2)
};
(0,1)
(1,1.5)
(2,1.8)
};
(3,0.5)
(4.5,1)
(5,2)
};
(3,0)
(4,1)
(5,2.2)
};
\end{axis}
\end{tikzpicture}
\end{document}

-

You should be able to get the first part of your custom legend like so

\addlegendimage{line legend,green}


obviously for that to work you should comment the legend entries= that you have in your code now. Just add the above code after you \addplot commands.

I am not sure how to get the matching plot marks

EDIT

With the comment from Jake this seems to work

\documentclass{article}

\usepackage{pgfplots}

\begin{document}
\begin{tikzpicture}
\begin{axis}

You can pass the same options to \addlegendimage as you would to \addplot, so to get the circle and square marks, you could use \addlegendimage{only marks, mark=o} \addlegendimage{only marks, mark=square}. Also, if you put the \addlegendimage commands before your \addplots, and/or you add forget plot to the \addplots, you can still use the legend entries key and don't have to use \addlegendentry. –  Jake May 11 '13 at 17:52