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.

How can I get multiple stacked plots (ybar stacked, stack plots=y) in one diagram, such that there are multiple stacked bars for the same x-coordinate, each slightly shifted? Is there a way to reset pgfplot's zero levels after an \addplot command, such that the next \addplot[xhshift=...] doesn't add something to the current stack, but rather starts a fresh stack?

FWIW, overlaying two plots with scale only axis won't help; I need this to work with groupplot

share|improve this question

1 Answer 1

up vote 8 down vote accepted

I assume you want "stacked and clustered bar charts".

The following solution is not the most elegant, but it works quite well. It basically resets the stacked plot variables and starts stacking the following plots. I haven't found a way to properly reset the zero levels for the first plot after the reset command, that's why I use an invisible plot with the forget plot option that has zero values at all coordinates. This does not interrupt the style cycle, but unfortunately it requires hard coding the coordinates and won't work if you don't know the number of bar stacks beforehand.

Plenty of room for improvement, but it's a start and might be enough for you to get the job done.

\documentclass{article}
\usepackage{pgfplots}

\makeatletter
\newcommand\resetstackedplots{
\makeatletter
\pgfplots@stacked@isfirstplottrue
\makeatother
\addplot [forget plot,draw=none] coordinates{(1,0) (2,0) (3,0)};
}
\makeatother
\begin{document}
\begin{tikzpicture}
\begin{axis}[
    ybar stacked,
    xtick=data,
    ymin=0,
    enlarge x limits=0.5,
    legend entries={A,B,C,D,E}
]
\addplot +[bar shift=-.2cm] coordinates {(1,1) (2,1.75) (3,0.5)};
\addplot +[bar shift=-.2cm] coordinates {(1,0.5) (2,2.5) (3,1.5)};

\resetstackedplots

\addplot  +[bar shift=.2cm]coordinates {(1,1.2) (2,1.5) (3,0.3)};
\addplot  +[bar shift=.2cm] coordinates {(1,0.5) (2,2) (3,1.5)};
\addplot  +[bar shift=.2cm] coordinates {(1,0.2) (2,0.1) (3,0.3)};
\end{axis}
\end{tikzpicture}
\end{document}

share|improve this answer
    
I've got a follow-up question on this. When you use [xshift] in the addplot` options, a legend created from this plot is also shifted (add a legend entry for your above example to see what I mean). Is there a way to un-shift the legend entry? –  Trevor Aug 18 '12 at 0:31
    
@Trevor: You're right, thanks for spotting that! I should have used bar shift instead of xshift (the former doesn't influence the legend image). I've edited my answer accordingly. –  Jake Aug 18 '12 at 8:40
    
Perfect! That did the trick. –  Trevor Aug 18 '12 at 18:43
1  
one more follow-up... When I use bar shift instead, the bar is in the right place, but the nodes near coords label doesn't shift with it. I'm using your nodes near coords horizontally centered solution as well. Ideas? –  Trevor Aug 19 '12 at 19:21

Your Answer

 
discard

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