# From pie chart to stacked horizontal bar chart

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

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:

\documentclass{article}
\usepackage[utf8]{inputenc}
\usepackage{tikz}
\usepackage{pgfplots}

\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]
\end{axis}
\end{tikzpicture}
\end{document}


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

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{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]
\end{axis}
\end{tikzpicture}
\end{document}


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

\documentclass{article}
\usepackage[utf8]{inputenc}
\usepackage{tikz}
\usepackage{pgfplots}

\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]

• 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? Jun 1, 2020 at 14:07
• 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 Jun 1, 2020 at 14:25
• 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 Jun 1, 2020 at 15:52