# Changing order of draw operation in PGFPlots

I've developed a pgfplots ybar chart with white horizontal grid lines over the vertical bars to help the reader judge the length of the bars. The "base" of the ybar chart (the x-axis) is set to y=1 so that I can see whether the normalized results are above or below the axis at y=1. My problem is that I'm having trouble drawing the black x-axis line at y=1 without it being overwritten by a white grid line.

\documentclass[border=10pt]{standalone}
\usepackage{pgfplots}
\usepackage{pgfplotstable}

\begin{document}
Cat A, 1.06
Cat B, 0.92
Cat C, 1.01
Cat D, 1.04
Cat E, 0.88
}\datatable
\begin{tikzpicture}
\begin{axis}[
ybar,
enlarge x limits=0.2,
height=4cm,
ymax=0.125,
ymin=-0.125,
xticklabels from table={\datatable}{0},
xtick=data,
ytick={-0.1,-0.05,...,0.1},
yticklabels={0.90,0.95,1.00,1.05,1.10},
ytick pos=left,
ymajorgrids=true,
major grid style={thin,color=white},
axis on top,
ylabel={Normalized value},
x axis line style={draw opacity=0},
xtick style={draw=none},
xticklabel style={
rotate=90,
anchor=east,
}
]
\addplot [fill=black,draw=none] table [x expr=\coordindex, y expr=\thisrowno{1}-1] {\datatable};
\draw [black,thin] (rel axis cs:0,0.5) -- (rel axis cs:1,0.5);
\end{axis}
\end{tikzpicture}
\end{document}


If I remove axis on top, the x-axis is drawn correctly, but then I lose the white grid lines over the vertical bars.

How can I change my plot such that the x-axis is drawn last or in such a way that it is drawn on top of the grid line?

You can achieve this by using the set layers feature. Then you simply need to draw the black line on the top most layer ...

% used PGFPlots v1.16
\documentclass[border=5pt]{standalone}
\usepackage{pgfplots}
\usepackage{pgfplotstable}
\begin{document}
\begin{tikzpicture}
col sep=comma,
]{
Cat A, 1.06
Cat B, 0.92
Cat C, 1.01
Cat D, 1.04
Cat E, 0.88
}\datatable
\begin{axis}[
ybar,
enlarge x limits=0.2,
height=4cm,
ymax=0.125,
ymin=-0.125,
xticklabels from table={\datatable}{0},
xtick=data,
ytick={-0.1,-0.05,...,0.1},
yticklabels={0.90,0.95,1.00,1.05,1.10},
ytick pos=left,
ymajorgrids=true,
major grid style={thin,color=white},
axis on top,
ylabel={Normalized value},
x axis line style={draw opacity=0},
xtick style={draw=none},
xticklabel style={
rotate=90,
anchor=east,
},
]
x expr=\coordindex,
y expr=\thisrowno{1}-1,
] {\datatable};

\pgfonlayer{axis foreground}
\draw [black,thin] (rel axis cs:0,0.5) -- (rel axis cs:1,0.5);
\endpgfonlayer
\end{axis}
\end{tikzpicture}
\end{document}


This is somewhat a workaround

\documentclass[border=10pt]{standalone}
\usepackage{pgfplots}
\usepackage{pgfplotstable}

\begin{document}
Cat A, 1.06
Cat B, 0.92
Cat C, 1.01
Cat D, 1.04
Cat E, 0.88
}\datatable
\begin{tikzpicture}
\begin{axis}[
ybar,
enlarge x limits=0.2,
height=4cm,
ymax=0.125,
ymin=-0.125,
xticklabels from table={\datatable}{0},
xtick=data,
ytick={-0.1,-0.05,...,0.1},
yticklabels={0.90,0.95,1.00,1.05,1.10},
ytick pos=left,
ymajorgrids=true,
major grid style={thin,color=white},
axis on top,
ylabel={Normalized value},
x axis line style={draw opacity=0},
xtick style={draw=none},
xticklabel style={
rotate=90,
anchor=east,
}
]
\addplot [fill=black,draw=none] table [x expr=\coordindex, y expr=\thisrowno{1}-1] {\datatable};
\end{axis}
\draw (rel axis cs:0.2,0.5) -- (rel axis cs:1.1,0.5);
\end{tikzpicture}
\end{document}