# pgfplots: Aligning multiple bars and marks horizontally along symbolic X-axis

I want to plot average values of some measure as bar charts with error bars and its maximal values as marks above the bars.

I use two separate plots for that now. The problem is aligning marks with bars: right now all marks are positioned at the same symbolic-X position, whereas I want each mark to be above the corresponding bar (and have the same colour, but that's probably not that big a problem).

Here's a not-so-minimal example:

\documentclass{report}
\usepackage{pgfplots}
\begin{document}
\begin{tikzpicture}
\begin{axis}[
ybar,
ymin=0, yticklabel style={/pgf/number format/fixed},
symbolic x coords={0,1,2,3}, enlarge x limits=0.15,
legend style={at={(0.5,-0.2)}, anchor=north, legend columns=-1},
]
\addplot+ [nodes near coords, point meta=explicit symbolic, error bars/.cd, y dir=both, y explicit]
table[x index=0, y index=1, y error index=2, meta index=3] {
0   0.1 0.0040  0
1   0.0358  0.0017  224
2   0.0358  0.0017  224
3   0.0358  0.0017  224
};
\addplot+ [nodes near coords, point meta=explicit symbolic, error bars/.cd, y dir=both, y explicit]
table[x index=0, y index=1, y error index=2, meta index=3] {
0   0.1 0.0041  0
1   0.0322  0.0016  224
2   0.0321  0.0014  252
3   0.0321  0.0014  266
};
\addplot+ [only marks] table[x index=0,y index=1] {
0   0.2
1   0.06
2   0.06
3   0.06
};
\addplot+ [only marks] table[x index=0,y index=1] {
0   0.15
1   0.04
2   0.04
3   0.04
};
\legend{P1,P2}
\end{axis}
\end{tikzpicture}
\end{document}

-
I don't think there's a good way to do this automatically, but you can fix it manually by adding every mark/.append style={xshift=-6pt} to the first of the marker plots, and every mark/.append style={xshift=6pt} to the second. The value of 6pt is half the bar width (default value 10pt) plus half the bar gap (default value 2pt). – Jake Apr 25 '13 at 16:31
– Kurt Apr 25 '13 at 16:33
Thank you, @Jake, it is a fine solution, works for me. Only I put the values 12/-12pt to position marks above the center of a bar. – wxd Apr 26 '13 at 11:27
@wxd: Glad it helps. Using \addplot+ [only marks,every mark/.append style={xshift=-12pt}] in your document leads to the marks being shifted too far to the left for me, but if it works for you, that's great – Jake Apr 26 '13 at 11:33
@Jake, you are right: I've modified the document a bit since I posted the example. Now there's also [scale=0.6], which apparently affects the positioning. Without it suggested values 6/-6pt work as expected. Thanks a lot again! – wxd Apr 26 '13 at 11:56

You can shift the points by adding mark options={xshift=\pgfkeysvalueof{/pgf/bar shift}} to the \addplot [...] options:

\documentclass{report}
\usepackage{pgfplots}
\begin{document}
\begin{tikzpicture}
\begin{axis}[
ybar,
ymin=0, yticklabel style={/pgf/number format/fixed},
symbolic x coords={0,1,2,3}, enlarge x limits=0.15,
legend style={at={(0.5,-0.2)}, anchor=north, legend columns=-1},
]
\addplot+ [nodes near coords, point meta=explicit symbolic, error bars/.cd, y dir=both, y explicit]
table[x index=0, y index=1, y error index=2, meta index=3] {
0   0.1 0.0040  0
1   0.0358  0.0017  224
2   0.0358  0.0017  224
3   0.0358  0.0017  224
};
\addplot+ [nodes near coords, point meta=explicit symbolic, error bars/.cd, y dir=both, y explicit]
table[x index=0, y index=1, y error index=2, meta index=3] {
0   0.1 0.0041  0
1   0.0322  0.0016  224
2   0.0321  0.0014  252
3   0.0321  0.0014  266
};
\addplot+ [only marks, mark options={xshift=\pgfkeysvalueof{/pgf/bar shift}}] table[x index=0,y index=1] {
0   0.2
1   0.06
2   0.06
3   0.06
};
\addplot+ [only marks, mark options={xshift=\pgfkeysvalueof{/pgf/bar shift}}] table[x index=0,y index=1] {
0   0.15
1   0.04
2   0.04
3   0.04
};
\legend{P1,P2}
\end{axis}
\end{tikzpicture}
\end{document}


-