Q:

Add legend to tikz boxplot

I have major problems adding a proper legend to my tikz boxplot graph. Either the legend did not show at all or scrambled the whole boxplot layout. I would be happy with a basic legend inside or outside the grid which does not overlap with the boxplots (e.g. study A [red], study B [blue]).
\begin{tikzpicture}[
thick, /pgfplots/boxplot/box extend=0.3,]
\begin{axis}
        [
            name=border,
            grid=both,
            ytick={1,...,4},
            yticklabel style={align=right},
            yticklabels={
                statement a,
                statement b,
                statement c,
                statement d},
            cycle list={{red},{blue}},
            xticklabels={placeholder, Strongly\\Disagree, Disagree, Neutral, Agree, Strongly\\Agree},
            x tick label style  ={rotate=90,anchor=east},
            x tick label style  ={font=\footnotesize},
            xticklabel style   = {align=center},
            x dir=reverse,
            y dir=reverse
        ]

    \addplot+[ %PS VO1
        boxplot prepared={
            median=0,
            average=0.27,
            upper quartile=1,
            lower quartile=-1,
            upper whisker=2,
            lower whisker=-2,
            draw position=0.8
        },
    ] 
    coordinates{};

    \addplot+[ %IS VO1
        boxplot prepared={
            median=0.5,
            average=-0.08,
            upper quartile=1,
            lower quartile=-2,
            upper whisker=2,
            lower whisker=-2,
            draw position=1.2
        },
    ] coordinates{};

    \addplot+[ %PS VO2
        boxplot prepared={
            median=1,
            average=0.64,
            upper quartile=2,
            lower quartile=0,
            upper whisker=2,
            lower whisker=-2,
            draw position=1.8
        },
    ] 
    coordinates{};

    \addplot+[ %IS VO2
        boxplot prepared={
            median=1,
            average=1.12,
            upper quartile=2,
            lower quartile=0.75,
            upper whisker=2,
            lower whisker=-2,
            draw position=2.2
        },
    ] 
    coordinates{};

    \addplot+[ %PS VO3
        boxplot prepared={
            median=1,
            average=0.55,
            upper quartile=1,
            lower quartile=0,
            upper whisker=2,
            lower whisker=-1,
            draw position=2.8
        },
    ] 
    coordinates{};

    \addplot+[ %IS VO3
        boxplot prepared={
            median=1,
            average=0.96,
            upper quartile=2,
            lower quartile=0.75,
            upper whisker=2,
            lower whisker=-2,
            draw position=3.2
        },
    ] 
    coordinates{};

    %%

    \addplot+[ %PS VO4
        boxplot prepared={
            lower whisker=-1,
            upper whisker=2,
            average=0.73,
            median=1,
            lower quartile=0,
            upper quartile=2,
            draw position=3.8
        },
    ] 
    coordinates{};

    \addplot+[ %IS VO4
        boxplot prepared={
            lower whisker=-2,
            upper whisker=2,
            average=0.77,
            median=1,
            lower quartile=-1,
            upper quartile=2,
            draw position=4.2
        },
    ] 
    coordinates{};
\end{axis}
\end{tikzpicture}

Due to the 2-boxplot per statement workaround with draw position I´ve added the whole tikz code. Looking forward to your suggestions

A:

You can define your own legend with your preferred legend image code. For instance,
\documentclass[tikz,border=3mm]{standalone}
\usepackage{pgfplots}
\usepgfplotslibrary{statistics}
\pgfplotsset{compat=1.17}
\pgfplotsset{
/pgfplots/custom legend/.style={
legend image code/.code={
\draw [only marks,mark=diamond*]
plot coordinates { 
(0.3cm,0cm)
};
}, },
}
\begin{document}
\begin{tikzpicture}[
thick, /pgfplots/boxplot/box extend=0.3,]
\begin{axis}
        [
            name=border,
            grid=both,
            ytick={1,...,4},
            yticklabel style={align=right},
            yticklabels={
                statement a,
                statement b,
                statement c,
                statement d},
            cycle list={{red},{blue}},
            xticklabels={placeholder, Strongly\\Disagree, Disagree, Neutral, Agree, Strongly\\Agree},
            x tick label style  ={rotate=90,anchor=east},
            x tick label style  ={font=\footnotesize},
            xticklabel style   = {align=center},
            x dir=reverse,
            y dir=reverse,
            custom legend,
            legend style={at={(1,1)},anchor=south east,legend columns=2,
                column sep=0.5em}
        ]

    \addplot+[ %PS VO1
        boxplot prepared={
            median=0,
            average=0.27,
            upper quartile=1,
            lower quartile=-1,
            upper whisker=2,
            lower whisker=-2,
            draw position=0.8
        },
    ] 
    coordinates{};
    \addlegendentry{pft}
    \addplot+[ %IS VO1
        boxplot prepared={
            median=0.5,
            average=-0.08,
            upper quartile=1,
            lower quartile=-2,
            upper whisker=2,
            lower whisker=-2,
            draw position=1.2
        },
    ] coordinates{};
    \addlegendentry{blub}

    \addplot+[ %PS VO2
        boxplot prepared={
            median=1,
            average=0.64,
            upper quartile=2,
            lower quartile=0,
            upper whisker=2,
            lower whisker=-2,
            draw position=1.8
        },
    ] 
    coordinates{};

    \addplot+[ %IS VO2
        boxplot prepared={
            median=1,
            average=1.12,
            upper quartile=2,
            lower quartile=0.75,
            upper whisker=2,
            lower whisker=-2,
            draw position=2.2
        },
    ] 
    coordinates{};

    \addplot+[ %PS VO3
        boxplot prepared={
            median=1,
            average=0.55,
            upper quartile=1,
            lower quartile=0,
            upper whisker=2,
            lower whisker=-1,
            draw position=2.8
        },
    ] 
    coordinates{};

    \addplot+[ %IS VO3
        boxplot prepared={
            median=1,
            average=0.96,
            upper quartile=2,
            lower quartile=0.75,
            upper whisker=2,
            lower whisker=-2,
            draw position=3.2
        },
    ] 
    coordinates{};

    %%

    \addplot+[ %PS VO4
        boxplot prepared={
            lower whisker=-1,
            upper whisker=2,
            average=0.73,
            median=1,
            lower quartile=0,
            upper quartile=2,
            draw position=3.8
        },
    ] 
    coordinates{};

    \addplot+[ %IS VO4
        boxplot prepared={
            lower whisker=-2,
            upper whisker=2,
            average=0.77,
            median=1,
            lower quartile=-1,
            upper quartile=2,
            draw position=4.2
        },
    ] 
    coordinates{};
\end{axis}
\end{tikzpicture}
\end{document}

