1

I'm avoiding pie charts and I would like to convert this pie chart to a stacked horizontal bar chart:

enter image description here

The x ticks would be percentage (how to include the sign?) and the absolute numbers (where percentage is derived from) inside the bars. The legend can be either inside the bars or below the chart but I think the four bars should have different colors.

Here is what i got so far:

enter image description here

\documentclass{article}
\usepackage[utf8]{inputenc}
\usepackage{tikz}
\usepackage{pgfplots}
\pgfplotsset{compat=newest}

\pgfplotsset{testbar/.style={
    nodes near coords xbar stacked configuration/.style={},
    xbar stacked,
    width=.8\textwidth,
    xmajorgrids = true,
    xmin=0,xmax=100,
    ytick = data, yticklabels = {Sex,Handedness},
    tick align = outside, xtick pos = left,
    bar width=6mm, y=8mm,
    nodes near coords,
    nodes near coords align={horizontal},
}}

\begin{document}
\begin{tikzpicture}
    \begin{axis}[testbar]
        \addplot coordinates{(58.8,1) (82.4,0)};
        \addplot coordinates{(41.2,1) (17.6,0)};
    \end{axis}
\end{tikzpicture}
\end{document}

I guess this should be easy but I am a beginner at tikz/pgfplots.

1 Answer 1

2

Here is a quick proposal, with minor improvements. In such a situation, I always refer back to the pgfplots manual, it is very well built and basically just search an option and you will see all of its possibilities.

\documentclass{article}
\usepackage[utf8]{inputenc}
\usepackage{tikz}
\usepackage{pgfplots}
\pgfplotsset{compat=newest}

\pgfplotsset{testbar/.style={
    xbar stacked,
    width=.8\textwidth,
    xmajorgrids = true,
    xmin=0,xmax=100,
    ytick = data, yticklabels = {Sex,Handedness},
    tick align = outside, xtick pos = left,
    bar width=6mm, y=8mm,
    nodes near coords={\pgfmathprintnumber{\pgfplotspointmeta}\%}, % <-- prints % sign after y coordinate value
    xticklabel={\pgfmathprintnumber{\tick}\%},% <-- prints % sign after x tick value
    nodes near coords align={center}, % <-- horizontal alignment centered of nodes 
    enlarge y limits=0.5, % <-- Adds vertical space so to not crop the bars
}}

\begin{document}
\begin{tikzpicture}
    \begin{axis}[testbar]
        \addplot coordinates{(58.8,1) (82.4,0)};
        \addplot coordinates{(41.2,1) (17.6,0)};
    \end{axis}
\end{tikzpicture}
\end{document}

enter image description here

EDIT : A way to get different colors for the second bar

\documentclass{article}
\usepackage[utf8]{inputenc}
\usepackage{tikz}
\usepackage{pgfplots}
\pgfplotsset{compat=newest}

\pgfplotsset{testbar/.style={
    xbar stacked,
    width=.8\textwidth,
    xmajorgrids = true,
    xmin=0,%xmax=100,
    ytick = data, yticklabels = {Sex,Handedness},
    tick align = outside, xtick pos = left,
    bar width=6mm, y=8mm,
    nodes near coords={\pgfmathprintnumber{\pgfplotspointmeta}\%}, % <-- prints % sign after y coordinate value
    xticklabel={\pgfmathprintnumber{\tick}\%},% <-- prints % sign after x tick value
    nodes near coords align={center}, % <-- horizontal alignment centered of nodes 
    enlarge y limits=0.5, % <-- Adds vertical space so to not crop the bars
}}

\begin{document}
\begin{tikzpicture}
    \begin{axis}[testbar]
        \addplot coordinates{(58.8,1) (82.4,0)};
        \addplot coordinates{(41.2,1) (0,0)};
        \addplot coordinates{(0,1) (17.6,0)};
    \end{axis}
\end{tikzpicture}
\end{document}

enter image description here

5
  • Thank you for the help @bamboo. I'm trying to change the colors of every four bars, but if I type \addplot [fill=red!80] coordinates{(41.2,1) (17.6,0)}; it changes the color of the top-right and bottom-right bars. Do you know I can change their colors independently?
    – Pedro
    Commented Jun 1, 2020 at 14:07
  • Both red bars stem from the same addplot. For such manipulation, add another addplot and set the replaced coordinate to nan
    – BambOo
    Commented Jun 1, 2020 at 14:11
  • I tried this \addplot [fill=red!80] coordinates{(41.2,1) (nan,0)}; \addplot [fill=green!20] coordinates{(nan,1) (17.6,0)}; but the bottom-right bar disappeared
    – Pedro
    Commented Jun 1, 2020 at 14:25
  • 1
    @Pedro, my bad, this one must be solved with zero-values. See my edit
    – BambOo
    Commented Jun 1, 2020 at 14:35
  • Thank you @bamboo, it worked. Just to complete the answer, I added the legend with code inside testbar./style{}. This is the code for the legend: legend style={ legend columns=4, at={(xticklabel cs:0.5)}, anchor=north, draw=none, font=\footnotesize, }, area legend, %did not work inside legend stye
    – Pedro
    Commented Jun 1, 2020 at 15:52

You must log in to answer this question.

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