Tikz: Grouped Plot with Secondary y-axis

groupplotspgfplotstikz-pgftikz-pictikz-styles

I have been working quite some time on a grouped error bar plot, but I've hit a wall: one of the variables (Variable 2) comes from a logistic regression, so its value/coefficient isn't directly comparable to the others. The scale difference between the values corresponding to 'Variable 2' and 'Variables 1 and 3' results in tiny error bars for the latter, complicating interpretation. To fix this, I want to add a secondary y-axis on the right side of the plot just for Variable 2. Would that be possible?

The plot that I have created right now looks as follows:

enter image description here

The code to generate the plot is given below.

\documentclass[tikz,border=7pt]{standalone}
\usetikzlibrary{matrix,arrows.meta,decorations.pathreplacing}
\usetikzlibrary{matrix, arrows.meta} % added arrows.meta
\usepackage{pgfplots}
\usepgfplotslibrary{groupplots}
\usepackage{xcolor}
\definecolor{buy_color}{RGB}{0, 102, 204} % blue
\definecolor{sell_color}{RGB}{255, 51, 51} % red
\definecolor{all_color}{RGB}{0, 153, 0} % green


\begin{document}

\begin{tikzpicture}
\pgfplotsset{
  error bars/.cd,
    x dir=none,
    y dir=both, y explicit,
}
\begin{groupplot}[
    group style={
        group name=my plots,
        group size=1 by 3,
        vertical sep=1.5cm
    },
    width=12cm,
    height=6cm,
    tick pos=left,
    tick align=outside,
    yticklabel pos=left,
    yticklabel style={font=\footnotesize},
    ylabel style={font=\footnotesize},
    legend style={font=\footnotesize},
    legend pos=north west,
]

\nextgroupplot[
    xlabel=Variables (Panel A),
    ylabel=Value,
    xtick={1,...,3},
    xticklabels={Variable 1,Variable 2, Variable 3},
]
  \addplot [mark=*,color=buy_color,only marks] table [x expr={\thisrow{x}-0.1}, y=y, y error=ey] {
    x y ey
    1 -0.057 -0.022
    2 -0.99 -0.28
    3 0.042 0.11
  };
  \addlegendentry{Buy}
  
  \addplot [mark=*,color=sell_color,only marks] table [x expr={\thisrow{x}+0.1}, y=y, y error=ey] {
    x y ey
    1 -0.10 -0.07
    2 -0.52 0.26
    3 0.022 0.10
  };
  \addlegendentry{Sell}
\end{groupplot}
\end{tikzpicture}

\end{document}

Best Answer

In case the groupplot is not required, how about this:

\documentclass[tikz,border=7pt]{standalone}
\usepackage{pgfplots}
\pgfplotsset{compat=1.18}
\usepackage{xcolor}
\definecolor{buy_color}{RGB}{0, 102, 204} % blue
\definecolor{sell_color}{RGB}{255, 51, 51} % red
\definecolor{all_color}{RGB}{0, 153, 0} % green

\begin{document}
\begin{tikzpicture}
    \pgfplotsset{%
        error bars/.cd,
        x dir=none,
        y dir=both, y explicit,
    }
    \begin{axis}[%
        width=12cm,
        height=6cm,
        tick pos=left,
        tick align=outside,
        yticklabel pos=left,
        yticklabel style={font=\footnotesize},
        ylabel style={font=\footnotesize},
        legend style={font=\footnotesize},
        legend pos=south east,
        xlabel=Variables (Panel A),
        ylabel=Value,
        xtick={1,...,3},
        xticklabels={Variable 1,Variable 2, Variable 3},
        ]
        \addplot [mark=*,color=buy_color,only marks, x filter/.expression={x == 2 ? nan : x-0.1}] table [y error=ey] {%
            x y ey
            1 -0.057 -0.022
            2 -0.99 -0.28
            3 0.042 0.11
        };
        \addlegendentry{Buy}
        \addplot [mark=*,color=sell_color,only marks, x filter/.expression={x == 2 ? nan : x+0.1}] table [y error=ey] {%
            x y ey
            1 -0.10 -0.07
            2 -0.52 0.26
            3 0.022 0.10
        };
        \addlegendentry{Sell}
    \end{axis}
    \begin{axis}[%
        width=12cm,
        height=6cm,
        tick pos=right,
        tick align=outside,
        yticklabel pos=right,
        ylabel=Value (Variable 2),
        xtick=\empty,
        xticklabels=\empty,
        xmin=1, xmax=3,
        ]
        \addplot [mark=*,color=buy_color,only marks, x filter/.expression={x == 2 ? x-0.1 : nan}] table [y error=ey] {%
            x y ey
            1 -0.057 -0.022
            2 -0.99 -0.28
            3 0.042 0.11
        };
        \addplot [mark=*,color=sell_color,only marks, x filter/.expression={x == 2 ? x+0.1 : nan}] table [y error=ey] {%
            x y ey
            1 -0.10 -0.07
            2 -0.52 0.26
            3 0.022 0.10
        };
    \end{axis}
\end{tikzpicture}
\end{document}

mwe

If the groupplot is needed, an approach would be:

\documentclass[tikz,border=7pt]{standalone}
\usepackage{pgfplots}
\usepgfplotslibrary{groupplots}
\pgfplotsset{compat=1.18}
\usepackage{xcolor}
\definecolor{buy_color}{RGB}{0, 102, 204} % blue
\definecolor{sell_color}{RGB}{255, 51, 51} % red
\definecolor{all_color}{RGB}{0, 153, 0} % green

\begin{document}
\begin{tikzpicture}
    \pgfplotsset{%
        error bars/.cd,
        x dir=none,
        y dir=both, y explicit,
    }
    \begin{groupplot}[%
        group style={%
            group name=my plots,
            group size=1 by 3,
            vertical sep=1.5cm
        },
        width=12cm,
        height=6cm,
        tick pos=left,
        tick align=outside,
        yticklabel pos=left,
        yticklabel style={font=\footnotesize},
        ylabel style={font=\footnotesize},
        legend style={font=\footnotesize},
        legend pos=north west,
        ]
        \nextgroupplot[
        xlabel=Variables (Panel A),
        ylabel=Value,
        xtick={1,...,3},
        xticklabels={Variable 1,Variable 2, Variable 3},
        ]
        \addplot [mark=*,color=buy_color, only marks, x filter/.expression={x == 2? nan : x-0.1}] table [y error=ey] {%
            x y ey
            1 -0.057 -0.022
            2 -0.99 -0.28
            3 0.042 0.11
        };
        \addlegendentry{Buy}
        \addplot [mark=*,color=sell_color,only marks, x filter/.expression={x == 2? nan : x+0.1}] table [y error=ey] {%
            x y ey
            1 -0.10 -0.07
            2 -0.52 0.26
            3 0.022 0.10
        };
        \addlegendentry{Sell}
        \nextgroupplot[
        ]
        \addplot [red, domain=-1:1, samples=100] {x^3};
    \end{groupplot}
    \begin{groupplot}[%
        group style={%
            group name=my plots,
            group size=1 by 3,
            vertical sep=1.5cm
        },
        width=12cm,
        height=6cm,
        ]
        \nextgroupplot[
        tick pos=right,
        tick align=outside,
        yticklabel pos=right,
        ylabel=Value (Variable 2),
        xtick=\empty,
        xticklabels=\empty,
        xmin=1, xmax=3,
        ]
        \addplot [mark=*,color=buy_color,only marks, x filter/.expression={x == 2? x-0.1 : nan}] table [y error=ey] {%
            x y ey
            1 -0.057 -0.022
            2 -0.99 -0.28
            3 0.042 0.11
        };
        \addplot [mark=*,color=sell_color,only marks, x filter/.expression={x == 2? x+0.1 : nan}] table [y error=ey] {%
            x y ey
            1 -0.10 -0.07
            2 -0.52 0.26
            3 0.022 0.10
        };
    \end{groupplot}
\end{tikzpicture}
\end{document}

mwe2

Related Question