# Stacked ybar problem

The figure I'm looking to reproduce is the following:

I have been able to produce a stacked ybar chart before by using coordinates by following the examples given in the pgfplots manual chapter 4.5.9. Unfortunately now I have so many data points that it would take me ages to do this manually. Is there a simple way one could achieve something similar to the figure above? I have tried searching for example from previous questions but unfortunately I haven't been able to find one that suits my needs.

The data for the desired graph is the following:

\pgfplotstableread[col sep=comma,header=true]{
Year,RoW,MEA,China,Americas,APAC,Europe
2000,751,0,19,21,368,129
2001,807,0,24,24,496,265
2002,887,0,42,54,686,399
2003,964,0,52,102,916,601
2004,993,1,62,163,1198,1306
2005,1003,1,70,246,1502,2291
2006,1108,1,80,355,1827,3289
2007,1150,2,100,522,2098,5312
2008,1226,3,140,828,2628,11020
2009,1306,25,300,1328,3373,16854
2010,1590,80,800,2410,4951,30505
2011,2098,205,3300,4590,7513,52764
2012,2098,570,6800,8365,12159,70489
2013,2098,953,18600,13727,21992,81464
}\data


It's not clear what you think needed doing manually, but just using ybar stacked and plotting each piece works fine here.

For the totals atop the bars, we can use \addplot+[nodes near coords] for the last plot in the stack as shown in Stacked bar-plot: display total value. However, in pgfplots v1.9 and later, some extra code is added to modify the behavior of nodes near coords for the stacked bar plot types. This is discussed at the end of section 4.5.9 in the manual.

For most bar plots, these modifications make sense and are reasonable. But they are not wanted here. So, we can override the modifications by emptying an internal style used by pgfplots sometime before setting ybar stacked. This in general looks like

\pgfplotsset{nodes near coords ybar stacked configuration/.style={}}


and it can be set locally or globally in the document depending on how widespread its effect needs to be.

Here's the complete code:

\documentclass[tikz]{standalone}
\usepackage{pgfplots,pgfplotstable}
\pgfplotsset{compat=1.12}

Year,RoW,MEA,China,Americas,APAC,Europe
2000,751,0,19,21,368,129
2001,807,0,24,24,496,265
2002,887,0,42,54,686,399
2003,964,0,52,102,916,601
2004,993,1,62,163,1198,1306
2005,1003,1,70,246,1502,2291
2006,1108,1,80,355,1827,3289
2007,1150,2,100,522,2098,5312
2008,1226,3,140,828,2628,11020
2009,1306,25,300,1328,3373,16854
2010,1590,80,800,2410,4951,30505
2011,2098,205,3300,4590,7513,52764
2012,2098,570,6800,8365,12159,70489
2013,2098,953,18600,13727,21992,81464
}\data

\begin{document}
\begin{tikzpicture}
\begin{axis}[
nodes near coords ybar stacked configuration/.style={}, % disable modifications
ybar stacked,
]
\addplot table[x=Year,y=China]    from \data;
\addplot table[x=Year,y=Americas] from \data;
\addplot table[x=Year,y=APAC]     from \data;
\addplot+[nodes near coords] table[x=Year,y=Europe]   from \data;
\end{axis}
\end{tikzpicture}
\end{document}


Of course, the axis/tick labels, spacing, and other stuff needs changing to match the desired result, but all of this is doable. I think your question is rather focused on the stacking of the bars themselves.

And the result:

Edit 3: Another request was added in a comment for no scientific notation in the nodes near coords labels. The style of those nodes is determined by the every node near coord style, which is initially empty. We can disable scientific notation using the standard number-printing style pgf/number format/fixed. Here I've also shown decreasing the font size of those labels by also adding node font=\scriptsize:

\documentclass[tikz]{standalone}
\usepackage{pgfplots,pgfplotstable}
\pgfplotsset{compat=1.12}

Year,RoW,MEA,China,Americas,APAC,Europe
2000,751,0,19,21,368,129
2001,807,0,24,24,496,265
2002,887,0,42,54,686,399
2003,964,0,52,102,916,601
2004,993,1,62,163,1198,1306
2005,1003,1,70,246,1502,2291
2006,1108,1,80,355,1827,3289
2007,1150,2,100,522,2098,5312
2008,1226,3,140,828,2628,11020
2009,1306,25,300,1328,3373,16854
2010,1590,80,800,2410,4951,30505
2011,2098,205,3300,4590,7513,52764
2012,2098,570,6800,8365,12159,70489
2013,2098,953,18600,13727,21992,81464
}\data

\begin{document}
\begin{tikzpicture}
\begin{axis}[
nodes near coords ybar stacked configuration/.style={}, % disable modifications
ybar stacked,
every node near coord/.style={node font=\scriptsize,/pgf/number format/fixed},
]
\addplot table[x=Year,y=China]    from \data;
\addplot table[x=Year,y=Americas] from \data;
\addplot table[x=Year,y=APAC]     from \data;
\addplot+[nodes near coords] table[x=Year,y=Europe]   from \data;
\end{axis}
\end{tikzpicture}
\end{document}


• There is also the matter of putting the total above the top. – John Kormylo Apr 26 '15 at 18:53
• @JohnKormylo as I said, I have no idea what the specific problem was here; I'm waiting for comments from the OP. Anyway, the total at the top problem is covered here: Stacked bar-plot: display total value – Paul Gessler Apr 26 '15 at 18:56
• So my main problem is with the total values on top of the bars. I tried looking at the example you linked to but wasn't able to replicate that in my chart. – Cadbon May 1 '15 at 10:00
• @Cadbon see the edit. Since that answer was written, pgfplots added some modifications to the code for this style of plot, which needs to be overridden to produce your desired result. – Paul Gessler May 1 '15 at 12:51
• @PaulGessler, thx a lot for the edited answer! With your help I'm only left with one problem - how do I remove scaling from the nodes? The last two nodes I have are like "1*10^5" and I would like them to be just whole numbers. – Cadbon May 2 '15 at 9:27