3

I have a bar plot with grouped bars. Each group should contain the total sum on top of the group.

Example:

enter image description here

My example has two problems:

  • The x-position is estimated
  • The y-psition (height) depends on the 2nd bar

How can I get the sums to their correct positions?

The example is created with this code:

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

\begin{document}

  \begin{tikzpicture}
  \begin{axis}[
    title={Test},
    ybar, ymin=3,  
    bar width=5mm,
    enlarge y limits ={value=0.3,upper},
    symbolic x coords={a,b,c,d},
    xtick=data,
    nodes near coords, 
    nodes near coords align={anchor=north},%Move values in bar anchor=mid
  ]
  %Active
  \addplot [fill=blue] coordinates {
    ({a},15) [xa]
    ({b},25)
    ({c},35)
    ({d},55)};
  \addlegendentry{Active}
  %Inactive
  \addplot [fill=red] coordinates {
    ({a},60)
    ({b},50)
    ({c},40)
    ({d},30)}
      [yshift=5mm]
      node[pos=0] {a: 75}
      node[pos=.25] {b: 75}
      node[pos=.5] {c: 75}
      node[pos=1] {d: 45};
      \addlegendentry{Inactive}  
  \end{axis}
  \end{tikzpicture}
\end{document}

Remarks:

1 Answer 1

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

\begin{document}

  \begin{tikzpicture}
  \begin{axis}[
    title={Test},
    ybar, ymin=3,  ymax=80,
    bar width=5mm,
    symbolic x coords={a,b,c,d},
    xtick=data,
    nodes near coords, 
    nodes near coords align={anchor=north},%Move values in bar anchor=mid
  ]
  %Active
  \addplot [fill=blue] coordinates {
    ({a},15) [xa]
    ({b},25)
    ({c},35)
    ({d},55)};
  \addlegendentry{Active}
  %Inactive
  \addplot [fill=red] coordinates {
    ({a},60)
    ({b},50)
    ({c},40)
    ({d},30)};
      \addlegendentry{Inactive};
   \node at (axis cs:a,65) {a: 75};
   \node at (axis cs:b,55) {b: 75};
   \node at (axis cs:c,45) {c: 75};
   \node at (axis cs:d,60) {d: 85};
  \end{axis}
  \end{tikzpicture}
\end{document}

enter image description here

2
  • The axis cs defines the x-coordinate - great! Did I understand correct: for the height you take the biggest value + 5?
    – knut
    Commented Aug 24, 2013 at 0:01
  • @knut yes, you understand correct. Then you can choose the value you prefer for the height.
    – Red
    Commented Aug 24, 2013 at 8:19

You must log in to answer this question.

Not the answer you're looking for? Browse other questions tagged .