Example: Sub series in stacked bar plot

Published 2014-03-28 | Author: Elke Schubert

Two series will be shown in a bar plot. Additionally, they contain sub series, which we would like to plot in the same diagram. So we just need a 2D plot. We will shift the series to achieve that.

Download as: [PDF] [TEX]  •  [Open in Overleaf]

Sub series in stacked bar plot

Do you have a question regarding this example, TikZ or LaTeX in general? Just ask in the LaTeX Forum.
Oder frag auf Deutsch auf TeXwelt.de.

\documentclass[border=5pt]{standalone}
\usepackage{pgfplots}
\pgfplotsset{compat = 1.8}
\colorlet{xcolorA}{red!80!black}
\colorlet{xcolorB}{orange}
\colorlet{ycolorA}{blue}
\colorlet{ycolorB}{blue!40!white}
\begin{document}
\begin{tikzpicture}
  \pgfplotsset{ybar stacked, ymin=0, ymax=100, xmin=0.5, xmax=2.5, xtick=data}
  \begin{axis}[bar shift=-8pt,
    legend pos = outer north east, legend style = {name = serieA}]
    \addplot [fill=xcolorA] table [x index = 0, y index = 1] {serieA.dat};
    \addplot [fill=ycolorA] table [x index = 0, y index = 2] {serieA.dat};
    \legend{x (Serie A), y (Serie A)}
  \end{axis}
  \begin{axis}[bar shift = 8pt,
    legend style = {at = {([yshift = -1mm]serieA.south west)},
      anchor = north west}]
    \addplot [fill = xcolorB] table [x index = 0, y index = 1] {serieB.dat};
    \addplot [fill = ycolorB] table [x index = 0, y index = 2] {serieB.dat};
    \legend{x (Serie B), y (Serie B)}
  \end{axis}
\end{tikzpicture}
\end{document}

Comments

Adding comments is currently not enabled.