8
\documentclass{article}
\usepackage{pgfplots}
\begin{document}
\pgfplotsset{testbar/.style={
title=strookdiagram sporten,
xbar stacked,
width=10cm,
axis y line*= none, axis x line*= bottom,
xmajorgrids = true,
xmin=0,xmax=100,
ytick = data,
yticklabels = {},
tick align = outside, xtick pos = left,
bar width=6mm, y=8mm,
enlarge y limits={abs=0.625},% 0.5 + 0.5*(y - bar width)/y [TeX.sx #47995]
nodes near coords,
nodes near coords align={left},
}}
\begin{tikzpicture}
\begin{axis}[testbar] 
    \addplot[fill=red!40] coordinates{(20,0) };
    \addplot[fill=gray!40] coordinates{(10,0)};
    \addplot[fill=green!40] coordinates{(20,0) };
    \addplot[fill=orange!40] coordinates{(40,0) };
    \addplot[fill=blue!40] coordinates{(3,0) };
    \addplot[fill=red!40] coordinates{(7,0) };
\end{axis}
\end{tikzpicture}
\end{document}

I wanted to make a stacked bar graph, horizontal, with a legend and the percentage of each piece. The code above gives me:

stacked bar test1

But as you can see, the percentages are all zero. I also want a legend. Thanks in advance.

EDIT

I changed the code with the help of Jesse, so I now my code is:

 \documentclass{article}
 \usepackage{pgfplots}
 \usetikzlibrary{positioning}
 \begin{document}
 \pgfplotsset{testbar/.style={
 title=strookdiagram sporten,
 title style={yshift=10pt},
 xbar stacked, area style,
width=10cm,
axis y line*= none, axis x line*= bottom,
xmajorgrids = true,
xmin=0,
xmax=100,
x=1mm,
xtick={0,10,...,100},
ytick = data,
yticklabels = {},
tick align = outside, xtick pos = left,
bar width=6mm, y=8mm,
enlarge y limits={abs=0.625},% 0.5 + 0.5*(y - bar width)/y [TeX.sx #47995]
%nodes near coords,
nodes near coords={\pgfmathprintnumber\pgfplotspointmeta\%}
every node near coord/.append style={rotate=-90,font=\small, left,shift={(axis direction cs:-3,0)}}
}}
\begin{tikzpicture}
\begin{axis}[testbar,legend style={area legend, at={(0.5,-0.85)}, anchor=north, legend columns=-1}] 
\addplot[fill=red!40] coordinates{(20,0) };
\addplot[fill=gray!40] coordinates{(10,0)};
\addplot[fill=green!40] coordinates{(20,0) };
\addplot[fill=orange!40] coordinates{(40,0)};
\addplot[fill=blue!40] coordinates{(3,0) };
\addplot[fill=red!40] coordinates{(7,0) };
\legend{Voetbal,Netbal,C,D,E,F}
\end{axis}
\end{tikzpicture}
\end{document}

But the image it gives me is now

version 2

I've updated all packages with TeX Live Utilty and my TeX editor is TeXpad.

  • 1
    i have no help for calculating the percentages, but when you do solve that problem, you might want to use a smaller font, and adjust the position in the next-to-last bar (or any bar with a very small width) so that it's centered. maybe it would be worth considering centering all the values. – barbara beeton Dec 27 '13 at 16:34
  • 1
    @Arne -- Sorry, I did not have chance to check until now. The failure is caused by a missing , at the end of nodes near coords={\pgfmathprintnumber\pgfplotspointmeta\%}, <-- Here at line 22. – Jesse Dec 29 '13 at 10:18
  • @Jesse Thanks, I now gives me the correct alignment, but I still get 0% 0% 0% 0% 0% – Arne Timperman Dec 29 '13 at 10:42
  • @Arne -- I got correct percentages here. Anyway, could you please do a quick test? Use the second solution, changing the same y=0, to y=0,1,2,3,4,5, to see if still get all 0 percentages. – Jesse Dec 29 '13 at 10:54
  • @jesse a quick test done... it displays the y-coördinates 0 1 2 3 4 5 instead of the percentages – Arne Timperman Dec 29 '13 at 11:22
  • 1
    @Arne (1) The system will think we are chatting if we continue this way. (2) If you can download pgfplots manual from internet and read the last line on page 51 (which is my wild guess to your problem). (3) Take the code and run at different PC, OS or distributions. (4) If (3) still fails, your question deserves a new focus/question that prints y coordinates instead of printing accumulation of x coordinates because I get the expected results for both cases over the past 1 hour. Mine is Window, MiKTeX, TeXworks. – Jesse Dec 29 '13 at 12:12
  • @Jesse (1) I have the 469pages version of pgfplots.pdf but due to my brain, I couldn't understand the solution to my problem. (2) I've run the code on TeXWorks, but it was the same output. (3) I've I understand you, I should start a new question with this problem? – Arne Timperman Dec 29 '13 at 15:28

1 Answers1

7

This attempt tries to provide two solutions. Since stacking means to add either x- or y- coordinates of successive \addplot commands on top of each others (Here, it is the x direction), causing the overlap for the 90% and 3% in particular.

The first solution on the top is to rotate the label by -90 degree and use the small font size. To do that is to define an append style via every node near coord/.append style command. It is noted that the shift command shift={(axis direction cs:-3,0)} is to move the labels up so that they won't collide with the boxes for better readability.

The second plot at the bottom simply changes the y coordinates from the same y=0 to y=0,1,2,3,4,5, thus making them like a stair, without changing the default label orientations. Furthermore, in this plot, you will observe why the labels overlay if they collapse to a single, horizontal stacked bar.

As to the legend, a legend style is defined via legend style={area legend, at={(0.5,-0.15)}, anchor=north, legend columns=-1}.

enter image description here

\documentclass{article}
\usepackage{pgfplots}
\usetikzlibrary{positioning}
\begin{document}
\pgfplotsset{testbar/.style={
title=strookdiagram sporten,
xbar stacked, area style,
width=10cm,
axis y line*= none, axis x line*= bottom,
xmajorgrids = true,
xmin=0,xmax=100,
ytick = data,
yticklabels = {},
tick align = outside, xtick pos = left,
bar width=6mm, y=8mm,
enlarge y limits={abs=0.625},% 0.5 + 0.5*(y - bar width)/y [TeX.sx #47995]
nodes near coords,
every node near coord/.append style={rotate=-90,font=\small, left,shift={(axis direction cs:-3,0)}}
}}
\begin{tikzpicture}
\begin{axis}[testbar,legend style={area legend, at={(0.5,-0.85)}, anchor=north, legend columns=-1}] 
    \addplot[fill=red!40] coordinates{(20,0) };
    \addplot[fill=gray!40] coordinates{(10,0)};
    \addplot[fill=green!40] coordinates{(20,0) };
    \addplot[fill=orange!40] coordinates{(40,0)};
    \addplot[fill=blue!40] coordinates{(3,0) };
    \addplot[fill=red!40] coordinates{(7,0) };
   \legend{A,B,C,D,E,F}
\end{axis}
\end{tikzpicture}

\vspace{1cm}

\pgfplotsset{testbar/.style={
title=strookdiagram sporten,
xbar stacked, area style,
width=10cm,
axis y line*= none, axis x line*= bottom,
xmajorgrids = true,
xmin=0,xmax=100,
ytick = data,
yticklabels = {},
tick align = outside, xtick pos = left,
bar width=6mm, y=8mm,
enlarge y limits={abs=0.625},% 0.5 + 0.5*(y - bar width)/y [TeX.sx #47995]
nodes near coords,
nodes near coords align={left}
}}
\begin{tikzpicture}
\begin{axis}[testbar,legend style={area legend, at={(0.5,-0.15)}, anchor=north, legend columns=-1}] 
    \addplot[fill=red!40] coordinates{(20,0)};
    \addplot[fill=gray!40] coordinates{(10,1)};
    \addplot[fill=green!40] coordinates{(20,2) };
    \addplot[fill=orange!40] coordinates{(40,3) };
    \addplot[fill=blue!40] coordinates{(3,4) };
    \addplot[fill=red!40] coordinates{(7,5) };
   \legend{A,B,C,D,E,F}
\end{axis}
\end{tikzpicture}
\end{document}

Edit:

  1. The OP prefers to use percentages instead of numbers. Then replace the corresponding lines to nodes near coords={\pgfmathprintnumber\pgfplotspointmeta\%}.

  2. To move the title further up, add title style={yshift=10pt} to the axis of the first plot because the added % will collide with the title in the old setting.

  3. To remove the grid on top, add axis on top=false to the testbar/.style

With those changes made, a new output is shown below.

enter image description here

Jesse
  • 29,686
  • Thanks. It's almost perfect. But I still have two small problems.
    1. It still only shows zero's instead of the percentages 20,30,50,90,93,100.
    2. In fact I don't want these percentages, I prefer the 20%, 10%, 20%, 40% ...

    (I copied the texcode exactly as above, and it showed me something different, I don't know how to post an image in this answer.

    – Arne Timperman Dec 27 '13 at 18:18
  • 1
    @Arne -- On your question 2, the newly added information will help. As to the question 1, you could edit your poster and add the strange image to it. But ... the code (MWE) runs fine on my end. – Jesse Dec 27 '13 at 19:15
  • In pgfplotset I had to add "point meta=x", otherwise the labels inside the bars contained the corresponding y-values -> {1,...,5} – Murmel Oct 21 '16 at 08:48