I have two questions regarding the pgfplots package. I have attached the source code I worked on. There are two features I am not able to realize:

  1. I would like to put the total value (sum of the whole stacked bar) on top of the bar, rotated by 90°. For me, it was only possible yet to get the value of each part of the stacked bar by using the nodes near coords command.

  2. Additionally, the percentage of each part of the stacked bar as relative to the whole stacked bar should be displayed in the bar, also rotated by 90° (addition equals 100%)

If it is somehow possible, percentages should have only one significant digit, and those below 5% should not be displayed in the bar. I know this is a pretty complex example, but maybe there is one who can solve it.


 Criterion  ddd     ccc     vvv 
 (0)    700         750         750 
 (a)    700         750         750 
 (b)        1200        750         750     
 (c)    33      345     345
 (d)    34      333     345
 (e)    234     344     344

 Criterion  ddd     ccc     vvv 
 0      70          75          75  
 a      70          75          75  
 b          120         75      75      
 c      3       34      34
 d      3       33      34
 e      23      34      34

\begin{groupplot}[group style= {
    columns=2,xlabels at=edge bottom,
    ylabels at=edge left,
    horizontal sep=1cm,group name=plots,
ymin=0, enlarge x limits={abs=.5}, enlarge y limits={upper,value=0.4}, 
ylabel=Mon, ylabel style={text height=0.02\textwidth,inner ysep=0pt}, 
ybar,ybar stacked,
major x tick style = transparent, 
/pgf/bar shift=0pt, /pgf/bar width=9pt,
x tick label style={text height=2ex},   
xlabel style={yshift=-2ex}, 
xticklabels from table={\Rob}{Criterion}, 
xtick=data,legend columns=-1, legend style={draw=none, /tikz/every even column/.append   style={column sep=5pt}}]

\nextgroupplot[xlabel=Rob, xlabel shift = -7.5cm, legend to name=grouplegend]
        \noexpand\addplot table [x expr=\noexpand\coordindex,y=\col] {\noexpand\Rob};
        \noexpand\addlegendentry {\col}

\nextgroupplot[xlabel=Gab, xlabel shift = -7.5cm]
        \noexpand\addplot table [x expr=\noexpand\coordindex,y=\col] {\noexpand\Gab};

 \draw[dashed] (plots c1r1.south west) -- ++(0,-1.5);
  \draw[dashed] (plots c1r1.south east) -- ++(0,-1.5);
 \draw[dashed] (plots c2r1.south west) -- ++(0,-1.5);
 \draw[dashed] (plots c2r1.south east) -- ++(0,-1.5);
 \node at (plots c1r1.south) [inner sep=0pt,anchor=north, yshift=-12ex]     {\ref{grouplegend}};
  • This would in principle be possible using visualization depends on keys, but at the moment they're broken for stacked plots, unfortunately. See tex.stackexchange.com/questions/86369/…. – Jake Jun 12 '13 at 3:24
  • @Jake I believe the bug you mention is fixed: any chance of an answer here (to get it off the 'unanswered' list)? – Joseph Wright Aug 3 '14 at 19:13
  • @Jake Any chance of an answer? :) – Sean Allred Aug 1 '15 at 21:45

Here is a solution that works without visualization depends on.

Please note that I have truncated the given code to just one plot, because there is nothing new in the second one and only would make the code even longer. I also removed a lot of styles etc. which almost didn't change the output and weren't related to the questions, so would also just "disturb" or distract.

(I also didn't apply the "show only one significant digit" requirement -- I think OP meant "one decimal digit" --, because that doesn't look good. But I think with the given solution it should be pretty easy to adapt it if this should really be necessary.)

For details on how the solution works, please have a look at the comments in the code.

        % so that the new feature of centering the `nodes near coords' text is applied
        % create a style which will be used to show the sum values
        % (borrowed from <http://tex.stackexchange.com/a/162389/95441>)
        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})
                ] {\pgfmathprintnumber{\pgfkeysvalueof{/data point/y}}};
        Criterion   ddd     ccc     vvv
        (0)         700     750     750
        (a)         700     750     750
        (b)         1200    750     750
        (c)         33      345     345
        (d)         34      333     345
        (e)         234     344     344
    % get number of columns in the table
        \pgfmathtruncatemacro{\NoOfCols}{\pgfmathresult - 1}
    % we also need this value again on a "zero basis" for a style
    % add column of the sum of the values in each row
    % (because you did the rest also with loops, so here is the loop solution for it)
    % (inspired by <http://tex.stackexchange.com/a/281436/95441>)
        create col/assign/.code={
                % create and initialize a variable that stores the sum values
            % cycle through the columns
            \pgfplotsforeachungrouped \i in {1,...,\NoOfCols} {
            % set the calculated value to the corresponding cell
            \pgfkeyslet{/pgfplots/table/create col/next content}{\RowSum}
            group style={
                xlabels at=edge bottom,
                ylabels at=edge left,
                horizontal sep=1cm,
                group name=plots,
            enlarge y limits={upper,value=0.4},
            ybar stacked,
            % instead of making the ticks invisible, just set the length to zero
            major tick length=0pt,
            xticklabels from table={\Rob}{Criterion},
            legend columns=-1,
            % -----
            % define the number style for the `nodes near coords' where
            % only values greater than 5 should be shown
            % (adapted from <http://tex.stackexchange.com/a/59961/95441>
            nodes near coords={
%            % because rotating the values would cause them to overlap
%            % I just provide the solution to that in commented form
%            nodes near coords style={
%                rotate=90,
%                font=\footnotesize,
%            },
            % to the last plot the sum value should be added as a node
            % for which we use the created style
            every axis plot no \NoOfColsZeroBased/.append style={
                show sum on top,
            % instead of shifting the `xlabel' just provide a title
            legend to name=grouplegend,
            % add all data columns to the axis
            \foreach \i in {1,...,\NoOfCols} {
                \addplot+ [
                    % this has to be given because we want to use a different
                    % value than the default `y' value
                    point meta=explicit,
                ] table [
                    x expr=\coordindex,
                    y index=\i,
                    % here we calculate the number which we want to be plotted
                    meta expr=\thisrowno{\i}/\thisrow{Sum}*100,
                ] {\Rob};
                        % extract the corresponding column header name from the table
                    % use it as legend entry
%        % I removed the second plot because there is nothing different here
%        \nextgroupplot

        \draw [dashed] (plots c1r1.south west) -- ++(0,-1.5);
        \draw [dashed] (plots c1r1.south east) -- ++(0,-1.5);

        \node [inner sep=0pt,anchor=north, yshift=-12ex]
            at (plots c1r1.south) {\ref{grouplegend}};


image showing the result of above code

| improve this answer | |

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.