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 need to group the labels in the x axis by putting a second level label. I need to separate the nodes that are grouped from the other ones or marked them somehow.

For example, if I have the labels in the example below I would like to have

S1 S2 S3  S4 S5 S6 S7   S8 S9 S10
X Group     Y Group      Z Group

How can I achieve this? I check the manual of pgfplots without any hint. Is there a special name for what I'm trying to achieve?

\documentclass{standalone}

\usepackage{tikz}
\usepackage{pgfplots}

\pgfplotstableread{
1   19.178  26.027  8.219   6.849   39.726
2   54.795  21.918  4.110   6.849   12.329
3   28.767  16.438  6.849   8.219   39.726
4   63.014  2.740   2.740   2.740   28.767
5   90.411  1.370   6.849   0.000   1.370
6   15.068  2.740   16.438  8.219   57.534
7   67.123  0.000   0.000   0.000   32.877
8   72.603  6.849   5.479   0.000   15.068
9   56.164  12.329  6.849   4.110   20.548
10  50.685  4.110   8.219   1.370   35.616
}\datatable

\begin{document}
\makeatletter
\begin{tikzpicture}
\begin{axis}[
    ylabel=label,
    xtick=data,
    xticklabels={S1,S2,S3,S4,S5,S6,S7,S8,S9,S10},
    enlarge y limits=false,
    enlarge x limits=0.1,
    ymin=0,ymax=100,
    ybar stacked,
    bar width=10pt,
    legend style={
      font=\footnotesize,
      cells={anchor=west},
      legend columns=5,
      at={(0.5,-0.15)},
      anchor=north,
      /tikz/every even column/.append style={column sep=0.2cm}
    },
]

\addplot table[x index=0,y index=1] \datatable;
\addplot table[x index=0,y index=2] \datatable;
\addplot table[x index=0,y index=3] \datatable;
\addplot table[x index=0,y index=4] \datatable;
\addplot table[x index=0,y index=5] \datatable;
\legend{Far,Near,Here,There,NotThere}
\end{axis}
\end{tikzpicture}
\end{document}
share|improve this question
    
Maybe tex.stackexchange.com/questions/55554/… can help? –  Jake Sep 13 '12 at 9:24
add comment

1 Answer 1

up vote 7 down vote accepted

You could adapt the approach described in How can I mix an "ybar" and an "ybar stacked" with pgfplots? to place the group labels underneath the individual ones:

\documentclass{standalone}

\usepackage{tikz}
\usepackage{pgfplots}

\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}{[index]0}\of\datatable
                        \coordinate [yshift=#4] (startgroup) at (axis cs:\pgfplotsretval,0);
                    \else
                        \pgfplotstablegetelem{\pgfplotstablerow}{[index]0}\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=north] {#3} (endgroup);
                    \fi
                \fi
            }
            \ifnum\thegroupcount=1
                        \setcounter{groupcount}{0}
                        \draw [
                            shorten >=-#5,
                            shorten <=-#5
                        ] (startgroup) -- node [anchor=north] {#3} (endgroup);
            \fi
        }
    }
}


\pgfplotstableread{
1   19.178  26.027  8.219   6.849   39.726 1
2   54.795  21.918  4.110   6.849   12.329 1
3   28.767  16.438  6.849   8.219   39.726 1
4   63.014  2.740   2.740   2.740   28.767 2
5   90.411  1.370   6.849   0.000   1.370  2
6   15.068  2.740   16.438  8.219   57.534 2
7   67.123  0.000   0.000   0.000   32.877 3
8   72.603  6.849   5.479   0.000   15.068 3
9   56.164  12.329  6.849   4.110   20.548 3
10  50.685  4.110   8.219   1.370   35.616 3
}\datatable

\begin{document}
\makeatletter
\begin{tikzpicture}
\begin{axis}[
    ylabel=label,
    xtick=data,
    xticklabels={S1,S2,S3,S4,S5,S6,S7,S8,S9,S10},
    enlarge y limits=false,
    enlarge x limits=0.1,
    ymin=0,ymax=100,
    ybar stacked,
    bar width=10pt,
    legend style={
      font=\footnotesize,
      cells={anchor=west},
      legend columns=5,
      at={(0.5,-0.2)},
      anchor=north,
      /tikz/every even column/.append style={column sep=0.2cm}
    },
    draw group line={[index]6}{1}{X Group}{-3.5ex}{7pt},
    draw group line={[index]6}{2}{Y Group}{-3.5ex}{7pt},
    draw group line={[index]6}{3}{Z Group}{-3.5ex}{7pt}
]

\addplot table[x index=0,y index=1] \datatable;
\addplot table[x index=0,y index=2] \datatable;
\addplot table[x index=0,y index=3] \datatable;
\addplot table[x index=0,y index=4] \datatable;
\addplot table[x index=0,y index=5] \datatable;
\legend{Far,Near,Here,There,NotThere}
\end{axis}
\end{tikzpicture}
\end{document}
share|improve this answer
add comment

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.