Take the 2-minute tour ×
TeX - LaTeX Stack Exchange is a question and answer site for users of TeX, LaTeX, ConTeXt, and related typesetting systems. It's 100% free, no registration required.

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

\pgfplotstableread{
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};
    }
]

\addplot +[xshift=-.2cm] table [x=X, y=Zn] {\datatable};% \addlegendentry{Zn}
\addplot +[xshift=-.2cm] table [x=X, y=Pb] {\datatable};% \addlegendentry{Pb}

\resetstackedplots

\addplot +[xshift=.2cm]table [x=X, y=Cu] {\datatable};% \addlegendentry{Zn}
\addplot +[xshift=.2cm]table [x=X, y=Fe] {\datatable};% \addlegendentry{Pb}
\addplot +[xshift=.2cm]table [x=X, y=Cr] {\datatable};% \addlegendentry{Zn}

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

Output from the above code

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.

share|improve this question
5  
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

1 Answer 1

It will work if you would specify a text resp. paper size, which is big enough, before you let it crop. Your plot is bigger than the default text width. For example:

\usepackage[a3paper,landscape]{geometry}
share|improve this answer
    
I have been able to make 30 cm wide plots with tightpage before, without specifying any paper size. In addition, in this case, making a small plot results in an overly large crop, which usually does not happen. Adding your suggestion does make it possible to fit more on the page, but the width still includes white space if the plot is smaller (using the A3 option increased the amount of white space). In this case, I solved the problem by switched to using the standalone package, as suggested by Jake in a comment to the original question. –  hjb981 Jul 5 '12 at 17:29
    
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
    
Hmm, if it works for you but not me, maybe I have an older version of the package. Even so, it is still strange that I can make my other plots bigger. I am most likely just missing something (I am still fairly new to this). I read (just now) in the manual for standalone that there are some known issues with tightpage. I don't know any details, but maybe what I see is one of those issues? Or, I should just update my package, but that will have to be another day -- no time now. –  hjb981 Jul 5 '12 at 19:52

Your Answer

 
discard

By posting your answer, you agree to the privacy policy and terms of service.

Not the answer you're looking for? Browse other questions tagged or ask your own question.