[Tex/LaTex] Draw Pareto chart with pgfplots

overlayspgfplotstikz-pgf

I've been following the pgfplots documentation to make some basic bar charts, mostly following this example:

\begin{tikzpicture}
  \begin{axis}[
    ybar, ymin=0,
    width=12cm, height=3.5cm, enlarge x limits=0.5,
    ylabel={\#participants},
    symbolic x coords={no,yes},
    xtick=data,
    nodes near coords, nodes near coords align={horizontal},
    ]
    \addplot coordinates {(3,no) (7,yes)};
  \end{axis}
\end{tikzpicture}

Now I'd like to overlay a line plot on the bar chart this produces, in order to create a Pareto chart (which uses the line to show cumulative data). Can pgfplots put these two things on the same axis?

Best Answer

You can add a new plot with a different plot type by using \addplot [<plot type>] ...;.

If you want to add a line plot to a column plot, you would use \addplot [sharp plot] ...;.

If you want to keep the current color cycle uninterrupted, add a + in front of the options: \addplot +[sharp plot] ...;

To calculate the cumulated sums automatically, you can make use of the PGFplotstable package that ships with PGFplots. It allows you to save data into tables which can then be fed to PGFplots using \addplot table {<\tablemacro>}. You create the table using

\pgfplotstableread{
Answer Count
no 7
yes 3
undecided 1
}\results

where the first data line is the header that assigns the column names, and the \results is the macro the table is saved to. To create a new column that contains the cumulated results, you issue

\pgfplotstableset{
    create on use/Cumulated/.style={
        create col/expr={
            \pgfmathaccuma + \thisrow{Count}
        }
    }
}

which tells PGFplotstable that whenever you access the column Cumulated (which does not exist yet), it will automatically create a new column consisting of the sum of \pgfmathaccuma (initially empty) and the current value of Count.

In your plot, you would then just say

\addplot [sharp plot] table [y=Cumulated] {\results};

Here's the complete code:

\documentclass[11pt]{amsart}
\usepackage{pgfplots}
\usepackage{pgfplotstable}

\begin{document}

\pgfplotstableread{
Answer Count
no 7
yes 3
undecided 1
}\results

\pgfplotstableset{
    create on use/Cumulated/.style={
        create col/expr={
            \pgfmathaccuma + \thisrow{Count}
        }
    }
}

\begin{tikzpicture}
  \begin{axis}[
    ybar, ymin=0,
    ylabel={\#participants},
    symbolic x coords={no,yes,undecided},
    xtick=data
    ]
    \addplot [fill=gray!30, nodes near coords] table {\results};
    \addplot [sharp plot,mark=*] table [y=Cumulated] {\results};
  \end{axis}
\end{tikzpicture}

\end{document}