# draw x tick labels on the top layer

For a group of plots i want to place all x tick labels on the top layer so that lines, that have been drawn at the end of the tikzpicture environment, are on a layer below the labels.

I already tried to set the layer with on top layer but unfortunately a scope environment seems to be always needed to draw on a specific layer.

My example below illustrates the problem. The goal is that the x tick label node at x=2 is eventually placed on top of the dashed line.

Example

\documentclass{standalone}

\usepackage{pgfplots}
\usepackage{tikz-layers}
\usepgfplotslibrary{groupplots}
\usetikzlibrary{backgrounds}
\usepackage{tikz}

\begin{document}

\begin{tikzpicture}[remember picture]
\begin{groupplot}[
no markers,
xmax=6,
ymin=0,
every x tick label/.style={
fill=red
},
group style = {
group size = 1 by 2,
},
]

\nextgroupplot[ymax=15]
\coordinate (t1g1-top) at (axis cs:2,15);
\coordinate (t1g1-bottom) at (axis cs:2,0);

\nextgroupplot
\coordinate (t1g2-bottom) at (axis cs:2,0);

\end{groupplot}

\begin{scope}[on above layer]
\draw[dashed, ultra thick] (t1g1-top) -- (t1g2-bottom);
\end{scope}

\end{tikzpicture}

\end{document}


The simplest way to do it with only a minor side effect is to use the set layers option in combination with axis on top to achieve what you want. Both options should be added to the axis options.

% used PGFPlots v1.16
\documentclass[border=5pt]{standalone}
\usepackage{pgfplots}
\usepgfplotslibrary{groupplots}
\begin{document}
\begin{tikzpicture}
\begin{groupplot}[
% ---------------------------------------------------------------------
% activate layers
set layers,
% and draw axis lines and labels on "top layers"
axis on top,
% ---------------------------------------------------------------------
no markers,
xmax=6,
ymin=0,
every x tick label/.style={
fill=red
},
group style = {
group size = 1 by 2,
},
]
\nextgroupplot[ymax=15]