# I have 3 data series and 2 different y-axis. How can I add, outside the graph, the legend for each of three series?

Does someone know how can i change the position of my legend to right-side of my graph? I try many things but i can't. Please give me some help! Here is my code!

\resizebox{\textwidth}{!}{
\begin{minipage}{1.3\textwidth}
\begin{center}
\begin{tikzpicture}

\begin{axis}[ /pgf/number format/.cd, use comma, 1000 sep={}, scale only axis, xmin=2006,xmax=2012, ymin=65, ymax=125, axis y line*=left, xlabel=Year, ylabel=\color{black}$Percentage(\%)$]
%Government Debt
(2006, 68.3608)
(2007, 68.0186)
(2008, 74.963)
(2009, 89.5022)
(2010, 102.7844)
(2011, 114.9962)
(2012, 121.9426)};
\end{axis}
%
\begin{axis}[ scale only axis, xmin=2006,xmax=2012, ymin=-15, ymax=5, axis y line*=right, axis x line=none, ylabel=\color{blue}$Percentage (\%)$]%
\addlegendentry{$Debt$}
%Total Budget Balance
(2006, -1.5862)
(2007, -1.9186)
(2008, -5.624)
(2009, -9.7638)
(2010, -13.0882)
(2011, -8.0618)
(2012, -6.4806)};
\addlegendentry{$TBB$}
%
%Real per capita GDP growth rate
(2006, 3.7266)
(2007, 3.3016)
(2008, -0.5192)
(2009, -4.1474)
(2010, -0.4744)
(2011, -1.2876)
(2012, -2.4802)
};
\addlegendentry{$GDPpc GR$}
\end{axis}
\end{tikzpicture}
\end{center}
\end{minipage}}


As darthbith says, in general you would add legend pos=outer north east to place the legend outside the axis on the top right. In this case it will overlap with the top ticklabel on the right y-axis. A simple fix for that here is to also add legend style={xshift=1em}, thereby shifting it to the right a bit.

• On the second axis, change from axis y line*=right to axis y line=right. The latter also makes the ylabel move to the right side, which I would think you want, considering that you changed the colour of it.
• Instead of \color{blue}, I added ylabel style={blue}. Not a big deal I think, mostly a matter of preference.
• Don't write the labels and legend entries in math mode, unless they're actual mathematical variables. Percentage and Debt for example are text, and should not be written as math.

\documentclass[border=3mm]{standalone}
\usepackage{pgfplots}
\begin{document}
\begin{tikzpicture}

\begin{axis}[
/pgf/number format/.cd, use comma,
1000 sep={},
scale only axis,
xmin=2006,
xmax=2012,
ymin=65,
ymax=125,
axis y line*=left,
xlabel=Year,
ylabel=Percentage (\%)]

%Government Debt
(2006, 68.3608)
(2007, 68.0186)
(2008, 74.963)
(2009, 89.5022)
(2010, 102.7844)
(2011, 114.9962)
(2012, 121.9426)};
\end{axis}
%
\begin{axis}[
scale only axis,
xmin=2006,
xmax=2012,
ymin=-15,
ymax=5,
axis y line=right,
axis x line=none,
ylabel=Percentage (\%),
ylabel style={blue},
yticklabel style={blue},
legend pos=outer north east,
legend style={xshift=1em}]%

%Total Budget Balance
(2006, -1.5862)
(2007, -1.9186)
(2008, -5.624)
(2009, -9.7638)
(2010, -13.0882)
(2011, -8.0618)
(2012, -6.4806)};
%
%Real per capita GDP growth rate
(2006, 3.7266)
(2007, 3.3016)
(2008, -0.5192)
(2009, -4.1474)
(2010, -0.4744)
(2011, -1.2876)
(2012, -2.4802)
};