How can I make tightpage work with this plot?

I want to plot something similar to this: pgfplots: Multiple (shifted) stacked plots in one diagram

with the addition of the kind of groupings presented here: How can I mix an "ybar" and an "ybar stacked" with pgfplots?

I merged the two like this, and got the kind of plot that I was after, except that I cannot make tightpage work properly:

\documentclass{article}
\usepackage{pgfplots, pgfplotstable}
\usepackage[tightpage,active]{preview}

\newcounter{groupcount}
\pgfplotsset{
draw group line/.style n args={5}{
after end axis/.append code={
\setcounter{groupcount}{0}
\pgfplotstableforeachcolumnelement{#1}\of\datatable\as\cell{%
\def\temp{#2}
\ifx\temp\cell
\ifnum\thegroupcount=0
\stepcounter{groupcount}
\pgfplotstablegetelem{\pgfplotstablerow}{X}\of\datatable
\coordinate [yshift=#4] (startgroup) at (axis cs:\pgfplotsretval,0);
\else
\pgfplotstablegetelem{\pgfplotstablerow}{X}\of\datatable
\coordinate [yshift=#4] (endgroup) at (axis cs:\pgfplotsretval,0);
\fi
\else
\ifnum\thegroupcount=1
\setcounter{groupcount}{0}
\draw [
shorten >=-#5,
shorten <=-#5
] (startgroup) -- node [anchor=base, yshift=0.5ex] {#3} (endgroup);
\fi
\fi
}
\ifnum\thegroupcount=1
\setcounter{groupcount}{0}
\draw [
shorten >=-#5,
shorten <=-#5
] (startgroup) -- node [anchor=base, yshift=0.5ex] {#3} (endgroup);
\fi
}
}
}

\makeatletter
\newcommand\resetstackedplots{
\makeatletter
\pgfplots@stacked@isfirstplottrue
\makeatother
\addplot [forget plot,draw=none] table [x=X, y=Zeroes] {\datatable};
}
\makeatother

X   Gp  C1  C2  Name        Zn      Pb  Cu  Fe  Cr  Zeroes
1   1A  0.2 3   Duracem     47.2    12  13  11  15  0
2   1A  0.2 3   Technocem   39      11  15  25  14  0
3   1A  0.2 3   Alipre      28      13  25  11  16  0
5   1A  0.2 8   Duracem     16.2    12  11  16  17  0
6   1A  0.2 8   Technocem   15      15  17  22  19  0
8   1A  0.5 3   Duracem     89      17  16  23  24  0
9   1A  0.5 3   Technocem   96      19  12  15  11  0
10  1A  0.5 3   Alipre      49.6    20  12  17  21  0
12  1A  0.5 8   Duracem     22.9    12  14  17  20  0
13  1A  0.5 8   Technocem   15.1    11  19  21  26  0
15  2A  0.2 3   Duracem     105     10  21  17  27  0
16  2A  0.2 3   Technocem   83      17  23  26  13  0
17  2A  0.2 3   Alipre      47      20  14  29  16  0
19  2A  0.2 8   Duracem     19.5    21  16  28  23  0
20  2A  0.2 8   Technocem   24.4    8   18  19  20  0
}\datatable

\begin{document}
\begin{preview}
\begin{tikzpicture}

\begin{axis}[
axis lines*=left, ymajorgrids,
width=25cm, height=6cm,
ymin=0,
ybar stacked,
bar width=8pt,
xtick=data,
xticklabels from table={\datatable}{Name},
xticklabel style={rotate=90,xshift=-10ex,anchor=mid east},
draw group line={C2}{3}{3\,\%}{-10ex}{4pt},
draw group line={C2}{8}{8\,\%}{-10ex}{4pt},
draw group line={C1}{0.2}{0.2\,\%}{-7ex}{5pt},
draw group line={C1}{0.5}{0.5\,\%}{-7ex}{5pt},
draw group line={Gp}{1A}{1A}{-4ex}{7pt},
draw group line={Gp}{2A}{2A}{-4ex}{7pt},
after end axis/.append code={
\path [anchor=base east, yshift=0.5ex]
(rel axis cs:0,0) node [yshift=-10ex] {Conc 1}
(rel axis cs:0,0) node [yshift=-7ex] {Conc 2}
(rel axis cs:0,0) node [yshift=-4ex] {Group};
}
]

\resetstackedplots

\end{axis}
\end{tikzpicture}

\end{preview}
\end{document}


The above code generates the below output, and the figure is cut. It should extend further both to the right and up.

How can I make tightpage work in the above example? I do not understand why it cuts out parts of the figure. If I reduce the size of the figure, there is unnecessary white space around the figure instead. I want to produce a free-standing picture, with the borders just outside of the figure without unnecessary white space around it.

In all my previous plots, I have used something like this without problems:

\pgfplotsset{width=15cm,compat=1.3} %use to get tight spacing of labels to tickmarks.
%Not enabled by default because it affects the spacing.
\usepackage[tightpage,active]{preview}

\begin{document}
\begin{preview}
\begin{tikzpicture}
\begin{axis}
% the plot here
\end{axis}
\end{tikzpicture}
\end{preview}
\end{document}


I then set the size of my plot after "width" (or height). For some reason, tightpage did not work this time.

• If you use \usepackage{standalone} instead of the preview environment to create the cropped PDF, it works fine. – Jake Jul 5 '12 at 16:07

\usepackage[a3paper,landscape]{geometry}

• Well, it makes tightpage work in this plot, when I tested it :-) I also support Jake's suggestion to switch to use the standalone class instead of the preview package. – Stefan Kottwitz Jul 5 '12 at 19:12