# How can I plot stacked pie charts using LaTeX?

I need to plot the following diagram:

But how I do not know any of the packages

• datatool,
• PGF/TikZ,
• PSTricks,
• datapie.

I know how to do simple pie charts with datapie, but this one it a bit tricky.

• Any half-decent plotting application will allow you to export in vector graphics. Even excel can be converted (saved as or printed to) to pdf, which if necessary can be tweaked using inkscape. And if even that's not possible, you should prefer .png over .jpg for diagrams. That said, one of the many knowledgeable people here will be able to help you - but why not show what you've tried so far? – Chris H Jan 31 '14 at 11:46
• I don't know how to do pie charts, but if you are able to create the individual concentric rings, it is possible to concentrically overlay them. As Chris H suggested, show us what you have tried. – Steven B. Segletes Jan 31 '14 at 11:48
• I have not tried anything special. I am looking for special package if has been ever written. Sometimes it is difficult to find approperite package. So I've decided to ask. Chris H is right I will use or png or pdf or eps formats if there is no package which can solve this problem. – saldenisov Jan 31 '14 at 11:58
• Best avoid pie charts as the visual information can be very misleading...see section 7.6 Plots and Charts of the PGF manual, especially the graphic on Kohle is am wichstigen – Leeser Jan 31 '14 at 12:03
• I don't understand how to read this pie chart. It looks as if you count the blue ring inside and outside as part of the same group, but it is tricky to add them together by eye. I had the first impression that we add the areas of the two blue regions, but I suppose you add their lengths, which is not as easy by eye. Could you please try a bar graph instead, to make it easier to read? – Benjamin McKay Jan 31 '14 at 13:39

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
\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)}

% 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}

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


A solution using PSTricks:

\documentclass{article}

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


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.)

• For some reason it shows error that all functions are not defined... – saldenisov Jan 31 '14 at 13:14
• As far as I understood. I should use LaTeX compile not PDFLaTeX comiler in TexStudio I'm using. I've changed, but the same problem. Could you describe in details what I could do with that? – saldenisov Jan 31 '14 at 13:21
• If you are using MikTeX on Windows, try the MikTeX Update program docs.miktex.org/manual/updating.html. If you are using Ubuntu, click the Super key and type update, and click on Software Updates. – Benjamin McKay Jan 31 '14 at 13:42
• Updating helped. It looks like the pstrick package were one year old, and it was the problem. – saldenisov Feb 1 '14 at 18:10
• Is it possible to use pstrick package with standalone? When I use in stadalone, created pdf page has format of A4, but I want to have size of the picture. What can be the solution? – saldenisov Feb 2 '14 at 12:58