4

The following code draws a bar chart with number labels. I want the entries in the legend to be the other way around i.e. for there to be a gray box at the top with the label "two", and a white box below with the label "one". How can I do that? I could well be missing something obvious. EDIT: read on for a version using pgfplots 1.14

\documentclass{standalone}
\usepackage{pgfplots}
\pgfplotsset{compat=1.3}
\begin{document}
  \begin{tikzpicture}
  \begin{axis}[
    ybar stacked,
    symbolic x coords={A,B,C},
    xtick=data,
    xticklabel style={align=center},
    nodes near coords={\pgfmathprintnumber\pgfplotspointmeta},
    nodes near coords align={vertical},
    nodes near coords align={anchor=north},%Move values in bar
    totals/.style={nodes near coords align={anchor=south}},
]
  \addplot [fill=white] coordinates {
({A},24)
({B},16)
({C},11)}; 
\addlegendentry{one}
 \addplot [fill=lightgray,point meta=explicit] coordinates {
({A},53)[53]
({B},47)[47]
({C},33)[33]};
\addlegendentry{two}
  \addplot[totals] coordinates {
({A},0)
({B},0)
({C},0)};
\legend{one,two}
  \end{axis}
  \end{tikzpicture}
\end{document}

enter image description here

EDIT: for anyone who comes along later, the following code gets almost the same layout as my original post, but is compatible with version 1.14 of pgfplots (see tex.stackexchange.com/a/162389/95441). I've included the reverse legend suggestion from Jake, which solves my original problem.

\documentclass[border=5mm]{standalone}
\usepackage{pgfplots}
\pgfplotsset{compat=1.14}
\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}[
    reverse legend,
    ybar stacked,
    ymin=0,
    ymax=90,
    symbolic x coords={A,B,C},
    xtick=data,
    xticklabel style={align=center},
    nodes near coords={\pgfmathprintnumber\pgfplotspointmeta},
   ]
   \addplot [fill=white] coordinates {
      ({A},24)
      ({B},16)
      ({C},11)};
   \addlegendentry{one}
   \addplot [fill=lightgray,show sum on top] coordinates {
      ({A},53)
      ({B},47)
      ({C},33)};
   \addlegendentry{two}
   \legend{one,two}
  \end{axis}
  \end{tikzpicture}
\end{document}

enter image description here

  • Maybe you should have noticed that you adapted your solution from (most probably) tex.stackexchange.com/a/162389/95441. – Stefan Pinnow Nov 17 '16 at 21:13
  • I am not sure what you mean by "should have noticed". I did make use of the answer you have given the link to, and (if I remember correctly) upvoted it by way of thanks. I have added a link to it in my post. – Gareth Walker Nov 18 '16 at 9:29
  • Yes, something like your last edit is fine. The reason why this helps is, that you didn't write why this is working and also at the link are given other useful information like that normalized axis cs: isn't documented so far. That was the reason why I was searching for that and found that link. Thanks for editing the question! – Stefan Pinnow Nov 18 '16 at 9:36
7

Set reverse legend in the axis options, and add forget plot to those \addplot commands you don't want to appear in the legend. Also, you only need either the two \addlegendentry commands, or the \legend command, but not both.

\documentclass{standalone}
\usepackage{pgfplots}
\pgfplotsset{compat=1.3}
\begin{document}
  \begin{tikzpicture}
  \begin{axis}[
    ybar stacked,
    reverse legend,
    symbolic x coords={A,B,C},
    xtick=data,
    xticklabel style={align=center},
    nodes near coords={\pgfmathprintnumber\pgfplotspointmeta},
    nodes near coords align={vertical},
    nodes near coords align={anchor=north},%Move values in bar
    totals/.style={nodes near coords align={anchor=south}},
]
  \addplot [fill=white] coordinates {
({A},24)
({B},16)
({C},11)}; 
\addlegendentry{one}
 \addplot [fill=lightgray,point meta=explicit] coordinates {
({A},53)[53]
({B},47)[47]
({C},33)[33]};
\addlegendentry{two}
  \addplot[totals, forget plot] coordinates {
({A},0)
({B},0)
({C},0)};
  \end{axis}
  \end{tikzpicture}
\end{document}
4

As Jake already stated in his answer the alternative would be to use the \legend command. This has the advantage that do don't need to use forget plot on the \addplot command, which besides not adding the plot to the legend has some other (maybe/sometimes unwanted) "side effects" like not advancing the cycle list.

To ignore \addplots with the \legend command, just write empty labels for them.

(By the way: When both, i.e. \addlegendentry and \legend, are given, \legend "wins".)

\documentclass[border=5pt]{standalone}
\usepackage{pgfplots}
    \pgfplotsset{compat=1.3}
\begin{document}
    \begin{tikzpicture}
        \begin{axis}[
            ybar stacked,
            reverse legend,
            symbolic x coords={A,B,C},
            xtick=data,
            nodes near coords,
            nodes near coords align={anchor=north},%Move values in bar
            totals/.style={
                nodes near coords align={anchor=south},
                red,        % <-- added, to distinguish it from "one"
            },
        ]
            \addplot [fill=white] coordinates {
                (A,24) (B,16) (C,11)
            };
            \addplot [fill=lightgray,point meta=explicit] coordinates {
                (A,53)[53] (B,47)[47] (C,33)[33]
            };
            \addplot [totals] coordinates {
                (A,0) (B,0) (C,0)
            };
            % give all the `\addplot's that should not be shown in the legend
            % an empty legend entry
            % (here "three" is empty, because there is a comma after the last entry)
            \legend{
                one,
                two,
            }
%            % perhaps this one easier to understand
%            % (here "two" isn't shown in the legend
%            \legend{
%                one,
%                ,
%                three
%            }
        \end{axis}
    \end{tikzpicture}
\end{document}

image showing the result of above code

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.