2

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(\%)$]
    \addplot[black] coordinates {
    %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 (\%)$]%
\addlegendimage{/pgfplots/refstyle=Hplot}
\addlegendentry{$Debt$}
    \addplot[blue, mark=+] coordinates {
    %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$}
%
    \addplot[blue,mark=o] coordinates {
%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}}

My graph

Torbjørn T.
  • 206,688
  • Welcome to TeX.SX! Please help us to help you and add a minimal working example (MWE) that illustrates your problem. It will be much easier for us to reproduce your situation and find out what the issue is when we see compilable code, starting with \documentclass{...} and ending with \end{document}. –  Sep 10 '14 at 11:42
  • Please shorten your title also –  Sep 10 '14 at 11:42
  • You should use the option legend pos=outer north east as described on page 208 of the pgfplots 1.11 manual. Related: http://tex.stackexchange.com/q/134084/32374 http://tex.stackexchange.com/q/83328/32374 http://tex.stackexchange.com/q/4085/32374 – darthbith Sep 10 '14 at 12:25
  • You might also find http://tex.stackexchange.com/questions/54794/using-a-pgfplots-style-legend-in-a-plain-old-tikzpicture useful. – John Kormylo Sep 10 '14 at 13:25

1 Answers1

1

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.

Other changes I made:

  • 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.

enter image description here

\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 (\%)]

\addplot[black] coordinates {
    %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}]%
\addlegendimage{/pgfplots/refstyle=Hplot}
\addlegendentry{Debt}

\addplot[blue, mark=+] coordinates {
    %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}
%
\addplot[blue,mark=o] coordinates {
%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{document}
Torbjørn T.
  • 206,688