Pgfplots: How to position the second label in a two-axis plot

pgfplots

How to position the second label above the second axis?

In the MWE below, simply declaring \pgfplotsset{compat=newest} solves the problem. However, I would like to control the label position locally in a document where the compat bounds cannot be set (think of it as temporary workaround). Is there a way to explicitly specify the label's position inside the second axis?

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

\begin{document}
\begin{tikzpicture}
    \begin{axis}[
        ymin=0,
        ymax=1,
        axis y line*=left,
        ylabel={left},
    ]
    \addplot [color=blue, domain=0:1] expression {x} node[midway]{axis 1};
    \end{axis}
    \begin{axis}[
        ymin=0,
        ymax=2,
        axis x line=none,
        axis y line*=right,
        ylabel={\hspace{4cm} should be right},
    ]
    \addplot [color=red, domain=0:1] expression {x} node[midway]{axis 2};
    \end{axis}
\end{tikzpicture}
\end{document}

enter image description here

Best Answer

Without specifying compat inside a pgfplotsset command, adding ylabel near ticks should produce what you want. However, the documentation (section 4.9.3) mentions that using pgfplotsset is preferred to make this behavior consistent document-wide and to avoid any unnecessary space (as fixed distances are used if activated outside of the preamble).

\documentclass[border=5pt]{standalone}
\usepackage{pgfplots}
\begin{document}
\begin{tikzpicture}
    \begin{axis}[
        ymin=0,
        ymax=1,
        axis y line*=left,
        ylabel={left},
    ]
    \addplot [color=blue, domain=0:1] expression {x} node[midway]{axis 1};
    \end{axis}
    \begin{axis}[
        ymin=0,
        ymax=2,
        axis x line=none,
        axis y line*=right,
        ylabel near ticks,
        ylabel={should be right},
    ]
    \addplot [color=red, domain=0:1] expression {x} node[midway]{axis 2};
    \end{axis}
\end{tikzpicture}
\end{document}

enter image description here