How can I plot stacked pie charts using LaTeX?

A solution using PSTricks:

\documentclass{article}

\usepackage{pstricks-add}
\usepackage{xfp}

% marcos
\newcommand*\FiveInner{\fpeval{round(\fiveInner/\TotalInner*360+\startAngle)}}
\newcommand*\Ten{\fpeval{round((\fiveInner+\ten)/\TotalInner*360+\startAngle)}}
\newcommand*\Twenty{\fpeval{round((\fiveInner+\ten+\twenty)/\TotalInner*360+\startAngle)}}
\newcommand*\TotalInner{\fpeval{\fiveInner+\ten+\twenty+\twentyfive}}
\newcommand*\FiveOuter{\fpeval{round(\fiveOuter/\TotalOuter*360+\startAngle)}}
\newcommand*\TotalOuter{\fpeval{\fiveOuter+\fifteen}}
\def\legend[#1]#2#3{%
  \psframe[
    fillcolor = #1,
    linewidth = 0.5\pslinewidth
  ](5,#2)(!5.4 #2 0.4 add)
  \uput[0](!5.4 #2 0.2 add){\small #3}}
\newcommand*\ringInner[4]{%
  \psRing[fillcolor = blue!70](0,0)[#1,#2]{0.5}{2}
  \psRing[fillcolor = green!70](0,0)[#2,#3]{0.5}{2}
  \psRing[fillcolor = purple!70](0,0)[#3,#4]{0.5}{2}
  \psRing[fillcolor = yellow!70](0,0)[#4,#1]{0.5}{2}}
\newcommand*\ringOuter[2]{%
  \psRing[fillcolor = blue!70](0,0)[#1,#2]{3}{4.5}
  \psRing[fillcolor = brown!40](0,0)[#2,#1]{3}{4.5}}

% parameters
\def\startAngle{90}
\def\fiveOuter{25}
\def\fifteen{15}
\def\fiveInner{23}
\def\ten{8}
\def\twenty{2}
\def\twentyfive{5}

\begin{document}

\psset{unit = 0.7, fillstyle = solid}
\begin{pspicture}(-4.5,-4.5)(9,5)
  \psaxes[labels = none, ticks = none](0,0)(0,5)
  % inner ring
  \ringInner{\startAngle}{\FiveInner}{\Ten}{\Twenty}
  \psline(0,1.25)(0.2,1.25)
  \uput[0](0.2,1.25){\small No}
  % outer ring
  \ringOuter{\startAngle}{\FiveOuter}
  \psline(0,3.75)(0.2,3.75)
  \uput[0](0.2,3.75){\small Yes}
  % legends
  \legend[blue!70]{1}{$5$~(years)}
  \legend[green!70]{0.5}{$10$~(years)}
  \legend[brown!40]{0}{$15$~(years)}
  \legend[purple!70]{-0.5}{$20$~(years)}
  \legend[yellow!70]{-1}{Over $25$~(years)}
  % text
  \rput[t](6.8,4){\shortstack[c]{%
    \small How many years\strut\\[-1ex]
    \small experience do you\strut}}
  \rput(7,-3.5){\footnotesize Pie Slices show Count}
\end{pspicture}

\end{document}

output

Note that all you have to do is enter the absolute values for each of the parameters and then the diagram will be drawn automatically. (Note that you have to choose the start angle of the first pie slice relative to horizontal; in the diagram above, it is 90 degrees.)


Just for fun, a TikZ solution inspired by Jake's wheelchart:

\documentclass[border=3mm]{standalone}
\usepackage{tikz}
\begin{document}

% The main macro
% #1 - List of value/color pairs
% #2 - inner radius
% #3 - outer radius
\newcommand{\wheelchart}[3]{
    % Calculate total
    \pgfmathsetmacro{\totalnum}{0}
    \foreach \value/\colour in {#1} {
        \pgfmathparse{\value+\totalnum}
        \global\let\totalnum=\pgfmathresult
    }

    % Calculate the thickness and the middle line of the wheel
    \pgfmathsetmacro{\wheelwidth}{(#3)-(#2)}
    \pgfmathsetmacro{\midradius}{(#3+#2)/2}

    % Rotate so we start from the top
    \begin{scope}[rotate=90]
    % Loop through each value set. \cumnum keeps track of where we are in the wheel
        \pgfmathsetmacro{\cumnum}{0}
        \foreach \value/\colour in {#1} {
            \pgfmathsetmacro{\newcumnum}{\cumnum + \value/\totalnum*360}

      % Draw the color segments.
            \draw[fill=\colour] (-\cumnum:#2) arc (-\cumnum:-\newcumnum:#2)--(-\newcumnum:#3) arc (-\newcumnum:-\cumnum:#3)--cycle;

       % Set the old cumulated angle to the new value
            \global\let\cumnum=\newcumnum
      }
      \end{scope}
}

\begin{tikzpicture}

% Usage: \wheelchart{<value1>/<colour1>, ...}{inner radius}{outer radius}
\wheelchart{5/yellow!70,2/purple!70,8/green!70,23/blue!70}{.5cm}{2cm}

\wheelchart{25/brown!70, 60/blue!70}{3cm}{4.5cm}

\draw[thick] (0,0)--(90:5cm);
\draw[thick] (0,1.25cm)--++(0:3mm) node[right] {No};
\draw[thick] (0,3.75cm)--++(0:3mm) node[right] {Yes};

\begin{scope}[xshift=5cm]
\draw[line width=3mm,blue!70] (0,1) -- ++(0:3mm) node[right, black] {5 years};
\draw[line width=3mm,green!70] (0,.5) -- ++(0:3mm) node[right, black] {10 years};
\draw[line width=3mm,brown!70] (0,0) -- ++(0:3mm) node[right, black] {15 years};
\draw[line width=3mm,purple!70] (0,-.5) -- ++(0:3mm) node[right, black] {20 years};
\draw[line width=3mm,yellow!70] (0,-1) -- ++(0:3mm) node[right, black] {Over 25 years};

\node[anchor=west] at (-.5,-3) {Pie Slices show Count};
\node[anchor=west, align=center] at (-.5,3) {How many years\\ experience do you};
\end{scope}
\end{tikzpicture}

\end{document}

enter image description here

Tags:

Tikz Pgf