[Tex/LaTex] How to correct nodes near coords position in ybar stacked

bar chartnodes-near-coordspgfplots

I'm working with this ybar stacked graph, but I can't position the nodes near coords in a correct position (not over something, not outside the chart, not over other bar, not to far away…readable!).

I've tried nodes near coords align={north} (west, etc.) and it changes place, but I can't understand this coordinates system!

My code:

\documentclass{article}
\usepackage{pgfplots}          % to print charts
\pgfplotsset{compat=1.8}
\begin{document}

\begin{figure}
  \centering
  \begin{tikzpicture}
    \begin{axis}[
        ybar stacked,
        nodes near coords,
        xbar legend,
%         nodes near coords align={right},
        legend pos=outer north east,
        enlarge x limits={abs=1},
        enlarge y limits=false,
        bar width=.5,
        % x axis
        xtick={1,2},
        xticklabels={Dots,Squares},
        % y axis
        ymin=0,
        ylabel={Aliens founds},
      ]

      \addplot table [
        x=index,
        y=blue,
      ] {
index   blue            pink    gray
1       25              10      5
2       35              0       100
      };

      \addplot table [
        x=index,
        y=pink,
      ] {
index   blue            pink    gray
1       25              10      5
2       35              0       100
};

      \addplot table [
      x=index,
      y=gray,
      ] {
index   blue            pink    gray
1       25              10      5
2       35              0       100
      };

      \legend{First one,Second one,Third}
    \end{axis}
  \end{tikzpicture}

  \caption{Caption placeholder}
\end{figure}
\end{document}

My graph:

Stacked y chart

Best Answer

My suggestion would be to update your pgfplots package. From pgfplots version 1.9, nodes near coords gives a better result. Compare the result with version 1.8:

\documentclass{article}
\usepackage{pgfplots}          % to print charts
\pgfplotsset{compat=1.8}

\begin{document}

\begin{figure}
  \centering
  \begin{tikzpicture}
    \begin{axis}[
        ybar stacked,
        nodes near coords,
        every node near coord/.append style={font=\footnotesize},
        xbar legend,
%         nodes near coords align={right},
        legend pos=outer north east,
        enlarge x limits={abs=1},
        enlarge y limits=false,
        bar width=.5,
        % x axis
        xtick={1,2},
        xticklabels={Dots,Squares},
        % y axis
        ymin=0,
        ylabel={Aliens founds},
      ]

      \addplot table [
        x=index,
        y=blue,
      ] {
index   blue            pink    gray
1       25              10      5
2       35              0       100
      };

      \addplot table [
        x=index,
        y=pink,
      ] {
index   blue            pink    gray
1       25              10      5
2       35              0       100
};

      \addplot table [
      x=index,
      y=gray,
      ] {
index   blue            pink    gray
1       25              10      5
2       35              0       100
      };

      \legend{First one,Second one,Third}
    \end{axis}
  \end{tikzpicture}

  \caption{Caption placeholder}
\end{figure}
\end{document}

enter image description here

and that of version 1.9 or newer:

\documentclass{article}
\usepackage{pgfplots}          % to print charts
\pgfplotsset{compat=1.9}

\begin{document}

\begin{figure}
  \centering
  \begin{tikzpicture}
    \begin{axis}[
        ybar stacked,
        nodes near coords,
        every node near coord/.append style={font=\footnotesize},
        xbar legend,
%         nodes near coords align={right},
        legend pos=outer north east,
        enlarge x limits={abs=1},
        enlarge y limits=false,
        bar width=.5,
        % x axis
        xtick={1,2},
        xticklabels={Dots,Squares},
        % y axis
        ymin=0,
        ylabel={Aliens founds},
      ]

      \addplot table [
        x=index,
        y=blue,
      ] {
index   blue            pink    gray
1       25              10      5
2       35              0       100
      };

      \addplot table [
        x=index,
        y=pink,
      ] {
index   blue            pink    gray
1       25              10      5
2       35              0       100
};

      \addplot table [
      x=index,
      y=gray,
      ] {
index   blue            pink    gray
1       25              10      5
2       35              0       100
      };

      \legend{First one,Second one,Third}
    \end{axis}
  \end{tikzpicture}

  \caption{Caption placeholder}
\end{figure}
\end{document}

enter image description here

If updating is not an option, you can try some manual adjustments; for example:

        every node near coord/.append style={xshift=-15pt,yshift=-3pt,anchor=east,font=\footnotesize},

which yields

\documentclass{article}
\usepackage{pgfplots}          % to print charts
\pgfplotsset{compat=1.8}
\begin{document}

\begin{figure}
  \centering
  \begin{tikzpicture}
    \begin{axis}[
        ybar stacked,
        nodes near coords,
        every node near coord/.append style={xshift=-15pt,yshift=-3pt,anchor=east,font=\footnotesize},
        xbar legend,
%         nodes near coords align={right},
        legend pos=outer north east,
        enlarge x limits={abs=1},
        enlarge y limits=false,
        bar width=.5,
        % x axis
        xtick={1,2},
        xticklabels={Dots,Squares},
        % y axis
        ymin=0,
        ylabel={Aliens founds},
      ]

      \addplot table [
        x=index,
        y=blue,
      ] {
index   blue            pink    gray
1       25              10      5
2       35              0       100
      };

      \addplot table [
        x=index,
        y=pink,
      ] {
index   blue            pink    gray
1       25              10      5
2       35              0       100
};

      \addplot table [
      x=index,
      y=gray,
      ] {
index   blue            pink    gray
1       25              10      5
2       35              0       100
      };

      \legend{First one,Second one,Third}
    \end{axis}
  \end{tikzpicture}

  \caption{Caption placeholder}
\end{figure}
\end{document}

enter image description here