# How can I get a stacked bar with single values and sum on top.

I have a stacked pgfplot like this:

But I want the single values inside the bars and the total sum on top of the bar (this graphic is 'handmade'):

How can I get this result?

My attempt up to now:

\documentclass[border=5mm] {standalone}
\usepackage{pgfplots, pgfplotstable}

\begin{document}
\begin{tikzpicture}
\begin{axis}[
title={Test stacked},
ybar stacked, ymin=0,
bar width=10mm,
symbolic x coords={a,b,c,d},
xtick=data,
nodes near coords,
nodes near coords align={anchor=north},%Move values in bar
every node near coord/.style={
},
]
%Active
({a},15)
({b},25)
({c},35)
({d},15)};
%Inactive
({a},60)
({b},50)
({c},40)
({d},30)};
\legend{Active,Inactive}
\end{axis}
\end{tikzpicture}
\end{document}


This writes the added values inside the bar (not the single values) and the total sum is missing.

The TeX-code is generated, it would be no problem for me to write the total in my TeX-code, there is no need to calculate it with tikz.

-
I created a new similar question with grouped bars, not stacked bars: tex.stackexchange.com/questions/129816/… –  knut Aug 23 '13 at 20:45
Does the new question supersede this one? I.e., can this one be closed or are you still looking for answers? –  Paul Gessler Feb 23 '14 at 0:35
@PaulGessler No, this question is still open. I found no answer on this questions and replaced the stacked bar with a grouped bar (and then I needed again the sum on top). –  knut Feb 23 '14 at 15:23

A recent pgfplots version (1.9?) added support to access all involved values when these nodes are typeset.

The default configuration of compat=1.9 would be to place the nodes near coords in the middle of each bar (and show the increment).

In addition to that feature, you can draw two nodes for the last plot: one which shows the increment and one which shows the sum on top.

This could be done as follows:

\documentclass[border=5mm] {standalone}
\usepackage{pgfplots, pgfplotstable}

\pgfplotsset{compat=1.9}

\begin{document}
\begin{tikzpicture}

\pgfplotsset{
show sum on top/.style={
/pgfplots/scatter/@post marker code/.append code={%
\node[
at={(normalized axis cs:%
\pgfkeysvalueof{/data point/x},%
\pgfkeysvalueof{/data point/y})%
},
anchor=south,
]
{\pgfmathprintnumber{\pgfkeysvalueof{/data point/y}}};
},
},
}

\begin{axis}[
title={Test stacked},
ybar stacked, ymin=0,
bar width=10mm,
symbolic x coords={a,b,c,d},
xtick=data,
nodes near coords,
%nodes near coords align={anchor=north},%Move values in bar
%nodes near coords bar offset=1,
%every node near coord/.style={},
]
%Active
({a},15)
({b},25)
({c},35)
({d},15)};
%Inactive
\addplot [fill=red,show sum on top] coordinates {
({a},60)
({b},50)
({c},40)
({d},30)};
\legend{Active,Inactive}
\end{axis}
\end{tikzpicture}
\end{document}


Note that I added compat=1.9 on top; this is crucial. Since this is sufficient for the increments, I commented out some style modifications. If you want to have the increments on top, you can easily include nodes near coords bar offset=1 and the style which sets anchor=north again.

Finally, the sum is generated by a new style defined in the example above. I called it show sum on top; it modifies @post marker code. The "modify" means to keep the old @post marker code (which has been generated by nodes near coords in this context). My new code is to add a further \node which is placed at the current (x,y) coordinates. The "normalized" prefix is necessary because the value stored in /data point/x is some numeric quantity and not one of your symbols (. The new node contains the value of /data point/y which is the sum.

Regarding references in the documentation: the documentation shows the high-level use-cases (namely the effect of compat=1.9) in the section of stacked plots. It also contains a couple of details about the normalized axis cs, but only indirectly in the documentation of \pgfplotspointgetcoordinates and \pgfplotspointgetnormalizedcoordinates. More details still need to be added to the manual (I'll take a note for those).

-
The solution doesn't seem to work, if for one bar the second (red) value is zero. Also would be possible to show only the total sum without the values for each part? –  maetra Mar 18 '14 at 9:41
@maetra : the problem with zero increments is a duplicate of the bug shown in tex.stackexchange.com/questions/150243/… . It has been fixed in the development version of pgfplots and will become part of the next stable. –  Christian Feuersänger Mar 19 '14 at 20:30
@maetra In order to show only the total sum without the values for each part, you can simplify the example considerably. Just write nodes near coords only for the red plot and remove the option everywhere else. Remove show sum on top and all its references. AND: change compat=1.9 to compat=1.8 or earlier. Please post a separate question if you need more details or an example. –  Christian Feuersänger Mar 19 '14 at 20:33

This solution is not the most elegant, but since you're generating the code for this anyway, I don't think it's a critical problem.

First, I took the liberty of adding x tick label style={anchor=south,yshift=-0.5cm}, to your options list. This makes the baselines of each character (a,b,c,d) align nicely (typically not an issue because figures, each having the same height, are normally used here).

I added a style totals/.style={nodes near coords align={anchor=south}}, to typeset the totals on top of the stacked bars when we're ready to do so, overriding the anchor=north which is specified for the other nodes.

Next is where things start to get a bit inelegant. The first plot can remain unchanged. In the second plot, we use pgfplots' point meta=explicit feature to source the node label text from somewhere other than the point's actual y-coordinate on the plot. We need to provide this explicit source as a bracketed number in the coordinates list, simply repeating the second "coordinate". This is the complete code for the second plot:

  %Inactive
({a},60) [60] % <--- note repeated number in brackets
({b},50) [50]
({c},40) [40]
({d},30) [30]};
\legend{Active,Inactive}


Finally, we add a third "dummy" stacked plot with all zeroes and the totals style applied to typeset the totals atop the bars.

Complete Code

\documentclass[border=5mm] {standalone}
\usepackage{pgfplots, pgfplotstable}

\begin{document}
\begin{tikzpicture}
\begin{axis}[
title={Test stacked},
ybar stacked, ymin=0,
bar width=10mm,
symbolic x coords={a,b,c,d},
xtick=data,
nodes near coords,
nodes near coords align={anchor=north},%Move values in bar
totals/.style={nodes near coords align={anchor=south}},
x tick label style={anchor=south,yshift=-0.5cm},
]
%Active
({a},15)
({b},25)
({c},35)
({d},15)};
%Inactive
({a},60) [60]
({b},50) [50]
({c},40) [40]
({d},30) [30]};
\legend{Active,Inactive}
%Dummy stacked plot to produce totals