# Bar chart with PGFPlots: Two groups with two categories and two bars each

Using \pgfplots, I have a bar chart that looks like this:

Instead, I would like to have something like this, so that I have two groups ("Freie Wiedergabe Tag 1" and "Freie Wiedergabe Tag 2") in 2 categories ("Erinnert" and "Nicht erinnert") and with two bars ("neutral" and "negativ") each. I have no idea how to do this:

My MWE is here:

\documentclass{apa6}
\usepackage{pgfplots}
\pgfplotsset{compat=1.13}
\usetikzlibrary{arrows.meta}
\begin{document}
\begin{figure}
\begin{tikzpicture}[scale=1.6,transform shape]
\begin{axis}[
ybar,
enlargelimits=0.15,
legend style={at={(0.5,-0.25)},
anchor=north,legend columns=-1},
ylabel={Anteil},
symbolic x coords={Freie Wiedergabe Tag 1,Freie Wiedergabe Tag 2,Treffer,Falscher Alarm},
xtick=data,
xticklabel style={text width=1.5cm,
font=\tiny,
align=center
},
]
%neutral
coordinates{(Treffer,0.8560) +-(0.01503,0.01503) (Falscher Alarm,0.1390) +-(0.01737,0.01737)(Freie Wiedergabe Tag 1,0.1481) +-(0.01067,0.01067) (Freie Wiedergabe Tag 2,0.1119) +-(0.00922,0.00922) };
%negativ
coordinates {(Treffer,0.9365) +- (0.00587,0.00587)(Falscher Alarm,0.1435) +- (0.01737,0.01737)(Freie Wiedergabe Tag 1,0.3247) +-(0.01695,0.01695) (Freie Wiedergabe Tag 2,0.2556) +-(0.01524,0.01524)
};
\legend{neutral,negativ}
\end{axis}
\end{tikzpicture}
\caption{Unterschrift}
\label{GedaechtnisBilder}
\end{figure}
\end{document}


The data points are probably not in the correct place, but apart from that one way of achieving that is shown below. I've changed the coordinates to numerical x-values (1,2,3,4) instead of symbolic coordinates, and set xticklabels={Erinnert,Nicht erinnert,Erinnert,Nicht erinnert}. So the four data points in each \addplot should correspond to those categories, the first two for the first day, the last two for the second day.

Then I've added two extra x ticks={1.5,3.5} (midway between the two groups of bars), with extra x tick labels={Freie Wiedergabe Tag 1,Freie Wiedergabe Tag 2}. Some vertical shifting of the ticklabels is done.

\documentclass{apa6}
\usepackage{pgfplots}
\pgfplotsset{compat=1.13}
\usetikzlibrary{arrows.meta}
\begin{document}
\begin{figure}
\begin{tikzpicture}
\begin{axis}[
width=0.6\linewidth,
ybar,
enlargelimits=0.15,
legend style={
% to save space I would place the legend inside the axis, at least with these data
%at={(0.5,-0.2)},
%anchor=north,
legend columns=-1},
ylabel={Anteil},
ylabel style={font=\large},
xtick=data,
% use explicit ticklabels instead of symbolc x coords
xticklabels={Erinnert,Nicht erinnert,Erinnert,Nicht erinnert},
xticklabel style={
%  text width=2cm,
yshift=-18pt, % move xticks down a bit
%  align=center
},
% extra ticks
extra x ticks={1.5,3.5},
extra x tick labels={Freie Wiedergabe Tag 1,Freie Wiedergabe Tag 2},
extra x tick style={
% because the xticklabel style also affects the extra ticks,
% shift extra ticklabels back up
ticklabel style={yshift=13pt},
% tickwidth is actually the length of of the ticks (the small lines)
tickwidth=0
}
]
%neutral
coordinates{
(1,0.8560) +-(0.01503,0.01503)
(2,0.1390) +-(0.01737,0.01737)
(3,0.1481) +-(0.01067,0.01067)
(4,0.1119) +-(0.00922,0.00922)
};
%negativ
coordinates {
(1,0.9365) +- (0.00587,0.00587)
(2,0.1435) +- (0.01737,0.01737)
(3,0.3247) +-(0.01695,0.01695)
(4,0.2556) +-(0.01524,0.01524)
};
\legend{neutral,negativ}
\end{axis}
\end{tikzpicture}
\caption{Unterschrift}
\label{GedaechtnisBilder}
\end{figure}
\end{document}

• That's great, thank you so much for the code and the explanations! It looks just the way I wanted it to look. Do you know a way to move the caption down a bit so that it's not too close to the "Erinnert"/"Nicht erinnert"? – Anne B. Oct 30 '17 at 8:04
• @AnneB. Well, that is how captions are set in the apa6 class, very close to the image/diagram above it. Easy fix: add \medskip between \end{tikzpicture} and \caption. – Torbjørn T. Oct 30 '17 at 12:08