5

I'd like to space out the node values on the bars so that they are clearly visible. What do you recommend for the ones that are partially hidden? Can I do this automagically? Also I'd like to use the plot coordinates rather than the cumulative values of probability. Where abouts is that in the pgfplots handbook?

enter image description here

\documentclass[tikz,border=12pt]{standalone}
\usepackage{pgfplots}
\usepackage{subfig}
\usepackage{tabularx}
\usepackage{verbatim}
\pgfplotsset{compat=newest}
   %\usepgfplotslibrary{external}
%\tikzexternalize
%\documentclass{article}
%\usepackage{tikz}
\usetikzlibrary{shapes,arrows}
\usetikzlibrary{calc,
arrows,decorations.pathmorphing,
backgrounds,fit,positioning,shapes.symbols,chains}
\usetikzlibrary{decorations.pathreplacing}
\newcolumntype{C}[1]{>{\centering\arraybackslash}p{#1}}
%
\begin{document}

\begin{tikzpicture}
\begin{axis}[
ybar stacked,
legend style={at={(0.5,-0.50)},
anchor=north,legend columns=-1},
ylabel={Probability},
symbolic x coords={{Direct Care}, Housekeeping, Mealtimes, {Medication Round},
Miscellaneous,{Personal Care}},
xtick=data,
ymax=1,
x tick label style={rotate=45,anchor=east},
every node near coord/.style={
      check for zero/.code={
        \pgfkeys{/pgf/fpu=true}
        \pgfmathparse{\pgfplotspointmeta-.1}
        \pgfmathfloatifflags{\pgfmathresult}{-}{
           \pgfkeys{/tikz/coordinate}
        }{}
        \pgfkeys{/pgf/fpu=false}
      }, check for zero, yshift=-5pt, font=\footnotesize},
    nodes near coords={\pgfmathprintnumber[precision=2]{\pgfplotspointmeta}},
    nodes near coords align={center}
]
\addplot+[ybar,bar width=20pt] plot coordinates {({Direct Care},0.258883249)
({Housekeeping},0.176470588)
({Mealtimes},0.19047619)
({Medication Round},0.207207207)
({Miscellaneous},0.222222222)
({Personal Care},0.615384615)
};
\addplot+[ybar,bar width=20pt] plot coordinates {({Direct Care},0.015228426)
({Housekeeping},0.235294118)
({Mealtimes},0)
({Medication Round},0.027027027)
({Miscellaneous},0)
({Personal Care},0.153846154)
};
\addplot+[ybar,bar width=20pt] plot coordinates {({Direct Care},0.152284264)
({Housekeeping},0.058823529)
({Mealtimes},0.333333333)
({Medication Round},0.351351351)
({Miscellaneous},0.180555556)
({Personal Care},0.153846154)
};
\legend{{Handwash only},{Gloves only},{Alcohol rub only}}
\end{axis}
\end{tikzpicture}
\end{document}
6
  • You can use the plot coordinates with point meta=rawy as described (very sparsely) in section 4.22. SKIPPING OR CHANGING COORDINATES / FILTERS – Tom Bombadil Jul 17 '13 at 18:36
  • In this answer Jake gives a solution for placing the labels in the middle of a bar segment. However, it only seems to work with \addplot table, so you'd probably need to change your data input. – Tom Bombadil Jul 17 '13 at 19:37
  • Use explicit width and height such that there is enough space for the numbers to fit in (with smaller fonts maybe). Otherwise my opinion is that it looks very crowded. – percusse Jul 17 '13 at 23:27
  • @TomBombadil Jake's answers, gives this snippet of code: calculate offset/.code={ \pgfkeys{/pgf/fpu=true,/pgf/fpu/output format=fixed} \pgfmathsetmacro\testmacro{(\pgfplotspointmeta*10^\pgfplots@data@scale@trafo@EXPONENT@y)/2*\pgfplots@y@veclength)} \pgfkeys{/pgf/fpu=false} } How can I get rid of the bit that creates 10^ and just place the values in the middle? – HCAI Jul 18 '13 at 7:56
  • I do not think it will ever look tidy if you place 3 bars per each of the 6 categories behind/on top of each other. Maybe you should think of another way to present the data. I'm sure there is one. I do not quite understand the meaning of your figures, do the probabilities add up to one on some dimension? – sheß Jul 18 '13 at 20:13
1
  • Starting with version 1.9 of pgfplots the labels are placed automatically in the middle of the bars
  • The plot coordinates can be used with point meta=rawy
  • I would recommend not printing the labels of very small values. This can be done e.g. like this:

nodes near coords=%
    {   \pgfkeys{/pgf/fpu,/pgf/fpu/output format=fixed}%
        \pgfmathtruncatemacro{\bigenough}{\pgfplotspointmeta > 0.05 ? 1 : 0}%
        \ifthenelse{\bigenough = 1}%
            {\pgfmathprintnumber[fixed,precision=2]{\pgfplotspointmeta}}%
            {}
        \pgfkeys{/pgf/fpu=false}%
    },

This turns on the floating point unit library and sets the number format to fixed as otherwise \pgfmathtruncatemacro can't use it. Then a check is performed weather or not the value is bigger than some arbitrarily chosen limit, here 0.05. If it is bigger, draw it, otherwise don't. Then disable the fpu library.

Code

\documentclass[tikz,border=2mm]{standalone}
\usepackage{pgfplots}
\pgfplotsset{compat=1.9}
\usepackage{xifthen}

\begin{document}

\begin{tikzpicture}
\begin{axis}
[   ybar stacked,
    legend style={at={(0.5,-0.50)},
    anchor=north,legend columns=-1},
    ylabel={Probability},
    symbolic x coords={{Direct Care}, Housekeeping, Mealtimes, {Medication Round}, Miscellaneous,{Personal Care}},
    xtick=data,
    ymin=0,
    ymax=1,
    point meta=rawy,
    height=8cm,
    width=10cm,
    x tick label style={rotate=45,anchor=east},
    every node near coord/.style={font=\fontsize{6}{7}\selectfont},
    nodes near coords=%
    {   \pgfkeys{/pgf/fpu,/pgf/fpu/output format=fixed}%
        \pgfmathtruncatemacro{\bigenough}{\pgfplotspointmeta > 0.05 ? 1 : 0}%
        \ifthenelse{\bigenough = 1}%
            {\pgfmathprintnumber[fixed,precision=2]{\pgfplotspointmeta}}%
            {}
        %\pgfmathprintnumber[fixed,precision=2]{\pgfplotspointmeta}%
        \pgfkeys{/pgf/fpu=false}%
    },
]
\addplot+[bar width=20pt] plot coordinates {({Direct Care},0.258883249)
({Housekeeping},0.176470588)
({Mealtimes},0.19047619)
({Medication Round},0.207207207)
({Miscellaneous},0.222222222)
({Personal Care},0.615384615)
};
\addplot+[bar width=20pt] plot coordinates {({Direct Care},0.015228426)
({Housekeeping},0.235294118)
({Mealtimes},0)
({Medication Round},0.027027027)
({Miscellaneous},0)
({Personal Care},0.153846154)
};
\addplot+[bar width=20pt] plot coordinates {({Direct Care},0.152284264)
({Housekeeping},0.058823529)
({Mealtimes},0.333333333)
({Medication Round},0.351351351)
({Miscellaneous},0.180555556)
({Personal Care},0.153846154)
};
\legend{{Handwash only},{Gloves only},{Alcohol rub only}}
\end{axis}
\end{tikzpicture}
\end{document}

Output

enter image description here

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.