# How to correct nodes near coords position in ybar stacked?

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},
]

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

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

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:

• Perhaps something like this helps a little every node near coord/.append style={xshift=-15pt,yshift=-3pt,anchor=east,font=\footnotesize},? – Gonzalo Medina Oct 2 '15 at 14:27

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},
]

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

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

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}


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},
]

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

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

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}


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},
]

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

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

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}


• Thanks, this works! Do you know what the north, west, etc refer to? I found reference on the manual, but I don't seem to make sense of it. When I use nodes near coords align={north}, they go north of what? If you prefer I can ask this in another question. – Fernando César Oct 2 '15 at 15:17
• @FernandoCésar Those are \node anchors (in TikZ parlance); when you use north, then the node will be positioned using its north anchor (in practice, you will see a shifting downwards). Imagine each node as a ractangle; using north will use the point in the middle of the upper segment as reference point; using south east, for example will use the point in the lower right corner, etc. – Gonzalo Medina Oct 2 '15 at 15:31