2

The user Korijn has posted a nice approach on how to create pairs of boxplots which are belonging to each other.


Minimum Working Example (MWE):

\documentclass[border=2mm]{standalone}

\usepackage{pgfplots}
\usepgfplotslibrary{statistics}
\pgfplotsset{compat=1.8}

\begin{document}

\begin{tikzpicture}
\begin{axis}[
boxplot/draw direction=y,
ylabel={RMSE},
height=6cm,
ymin=0,ymax=7,
cycle list={{red},{blue}},
boxplot={
        draw position={1/3 + floor(\plotnumofactualtype/2) + 1/3*mod(\plotnumofactualtype,2)},
        %
        box extend=0.3
},
x=2cm,
xtick={0,1,2,...,10},
x tick label as interval,
xticklabels={%
        {dataset 1\\{\tiny disabled enabled}},%
        {dataset 2\\{\tiny disabled enabled}},%
        {dataset 3\\{\tiny disabled enabled}},%
        {dataset 4\\{\tiny disabled enabled}},%
},
        x tick label style={
                text width=2.5cm,
                align=center
        },
]

\addplot
table[row sep=\\,y index=0] {
data\\
2.764\\
2.938\\
2.075\\
1.493\\
1.285\\
};

\addplot
table[row sep=\\,y index=0] {
data\\
1.175\\
2.813\\
2.006\\
3.893\\
2.012\\
};

\addplot
table[row sep=\\,y index=0] {
data\\
1.621\\
3.659\\
6.357\\
2.851\\
1.416\\
};

\addplot
table[row sep=\\,y index=0] {
data\\
2.280\\
1.482\\
1.787\\
2.326\\
1.795\\
};

\addplot
table[row sep=\\,y index=0] {
data\\
2.778\\
2.388\\
1.016\\
1.328\\
1.151\\
};

\addplot
table[row sep=\\,y index=0] {
data\\
1.028\\
1.571\\
4.090\\
3.875\\
1.890\\
};

\addplot
table[row sep=\\,y index=0] {
data\\
1.405\\
1.188\\
4.330\\
3.665\\
1.439\\
};

\addplot
table[row sep=\\,y index=0] {
data\\
2.937\\
1.320\\
1.357\\
1.852\\
1.215\\
};

\end{axis}
\end{tikzpicture}


\end{document}

Screenshot of the result:

Screenshot of the result


Question:

As you can see, there is a pair of boxplots existent for each dataset (from dataset 1 to dataset 4).

While preserving the boxplots of type "disabled" in y-axis 1, how can I draw the boxplots of type "enabled" in an additional second y-axis?

I have created the following code to create a second y-axis:

\documentclass[border=2mm]{standalone}

\usepackage{pgfplots}
\usepgfplotslibrary{statistics}
\pgfplotsset{compat=1.8}

\begin{document}

\begin{tikzpicture}
\begin{axis}[
axis y line*=left,
boxplot/draw direction=y,
ylabel={y-axis 1},
height=6cm,
ymin=0,ymax=7,
cycle list={{red},{blue}},
boxplot={
        draw position={1/3 + floor(\plotnumofactualtype/2) + 1/3*mod(\plotnumofactualtype,2)},
        box extend=0.3
},
x=2cm,
xtick={0,1,2,...,10},
x tick label as interval,
xticklabels={%
    {dataset 1\\{\tiny disabled enabled}},%
    {dataset 2\\{\tiny disabled enabled}},%
    {dataset 3\\{\tiny disabled enabled}},%
    {dataset 4\\{\tiny disabled enabled}},%
},
        x tick label style={
                text width=2.5cm,
                align=center
        },
]

\addplot
table[row sep=\\,y index=0] {
data\\
2.764\\
2.938\\
2.075\\
1.493\\
1.285\\
};

\addplot
table[row sep=\\,y index=0] {
data\\
1.175\\
2.813\\
2.006\\
3.893\\
2.012\\
};

\addplot
table[row sep=\\,y index=0] {
data\\
1.621\\
3.659\\
6.357\\
2.851\\
1.416\\
};

\addplot
table[row sep=\\,y index=0] {
data\\
2.280\\
1.482\\
1.787\\
2.326\\
1.795\\
};

\addplot
table[row sep=\\,y index=0] {
data\\
2.778\\
2.388\\
1.016\\
1.328\\
1.151\\
};

\addplot
table[row sep=\\,y index=0] {
data\\
1.028\\
1.571\\
4.090\\
3.875\\
1.890\\
};

\addplot
table[row sep=\\,y index=0] {
data\\
1.405\\
1.188\\
4.330\\
3.665\\
1.439\\
};

\addplot
table[row sep=\\,y index=0] {
data\\
2.937\\
1.320\\
1.357\\
1.852\\
1.215\\
};

\end{axis}
\begin{axis}[
axis y line*=right,
axis x line=none,
boxplot/draw direction=y,
ylabel={y-axis 2},
height=6cm,
ymin=0,ymax=7,
cycle list={{red},{blue}},
boxplot={
    draw position={1/3 + floor(\plotnumofactualtype/2) + 1/3*mod(\plotnumofactualtype,2)},
    box extend=0.3
},
x=2cm,
xtick={0,1,2,...,10},
x tick label as interval,
xticklabels={%
    {dataset 1},%
    {dataset 2},%
    {dataset 3},%
    {dataset 4},%
},
x tick label style={
    text width=2.5cm,
    align=center
},
]

\addplot
table[row sep=\\,y index=0] {
    data\\
    2.764\\
    2.938\\
    2.075\\
    1.493\\
    1.285\\
};

\addplot
table[row sep=\\,y index=0] {
    data\\
    1.175\\
    2.813\\
    2.006\\
    3.893\\
    2.012\\
};

\addplot
table[row sep=\\,y index=0] {
    data\\
    1.621\\
    3.659\\
    6.357\\
    2.851\\
    1.416\\
};

\addplot
table[row sep=\\,y index=0] {
    data\\
    2.280\\
    1.482\\
    1.787\\
    2.326\\
    1.795\\
};

\addplot
table[row sep=\\,y index=0] {
    data\\
    2.778\\
    2.388\\
    1.016\\
    1.328\\
    1.151\\
};

\addplot
table[row sep=\\,y index=0] {
    data\\
    1.028\\
    1.571\\
    4.090\\
    3.875\\
    1.890\\
};

\addplot
table[row sep=\\,y index=0] {
    data\\
    1.405\\
    1.188\\
    4.330\\
    3.665\\
    1.439\\
};

\addplot
table[row sep=\\,y index=0] {
    data\\
    2.937\\
    1.320\\
    1.357\\
    1.852\\
    1.215\\
};

\end{axis}

\end{tikzpicture}


\end{document}

Screenshot of the result:

Screenshot of the result


  • How can I separate the boxplot of "enabled" into y-axis 2?
  • All ideas and solutions are appreciated, it is not mandatory to reuse the upper code.
  • @marmot: Thanks a lot for your reply! I have updated my question to clarify the desired behavior. :-) What do you mean with name=upper? – Dave May 10 at 12:53
  • @marmot: I want to put the boxplots of type disabled on y-axis 1, but the boxplots of type enabled on y-axis 2. The reason for this is that disabled and enabled have different values and therefore different axis scalings, so I can not use both boxplot types within the same y-axis. – Dave May 10 at 13:20
  • 1
    @marmot: In the end, both axis will have 4 datasets each. I just copied y-axis 1 including its plots to have the same scaling for y-axis 2 in the MWE. :-) – Dave May 10 at 13:47
2

How about

\documentclass[border=2mm]{standalone}

\usepackage{pgfplots}
\usepgfplotslibrary{statistics}
\pgfplotsset{compat=1.8}

\begin{document}

\begin{tikzpicture}
\begin{axis}[
axis y line*=left,
boxplot/draw direction=y,
ylabel={y-axis 1},
height=6cm,
ymin=0,ymax=7,
cycle list={{red}},
boxplot={
        draw position={0.3 + \plotnumofactualtype},
        box extend=0.3
},
x=2cm,xmax=4.25,xmin=0,
xtick={0,1,2,...,5},
x tick label as interval,
xticklabels={%
    {dataset 1\\{\tiny disabled enabled}},%
    {dataset 2\\{\tiny disabled enabled}},%
    {dataset 3\\{\tiny disabled enabled}},%
    {dataset 4\\{\tiny disabled enabled}},%
},
        x tick label style={
                text width=2.5cm,
                align=center
        },
]

\addplot
table[row sep=\\,y index=0] {
data\\
2.764\\
2.938\\
2.075\\
1.493\\
1.285\\
};

\addplot
table[row sep=\\,y index=0] {
data\\
1.175\\
2.813\\
2.006\\
3.893\\
2.012\\
};

\addplot
table[row sep=\\,y index=0] {
data\\
1.621\\
3.659\\
6.357\\
2.851\\
1.416\\
};

\addplot
table[row sep=\\,y index=0] {
data\\
2.280\\
1.482\\
1.787\\
2.326\\
1.795\\
};

\end{axis}
\begin{axis}[
axis y line*=right,
axis x line=none,
boxplot/draw direction=y,
ylabel={y-axis 2},
height=6cm,
ymin=0,ymax=7,
cycle list={{blue}},
boxplot={
    draw position={0.7 + \plotnumofactualtype},
    box extend=0.3
},
x=2cm,xmax=4.25,xmin=0,
xtick={0,1,2,...,4},
x tick label as interval,
xticklabels={%
    {dataset 1},%
    {dataset 2},%
    {dataset 3},%
    {dataset 4},%
},
x tick label style={
    text width=2.5cm,
    align=center
},
]

\addplot
table[row sep=\\,y index=0] {
    data\\
    2.778\\
    2.388\\
    1.016\\
    1.328\\
    1.151\\
};

\addplot
table[row sep=\\,y index=0] {
    data\\
    1.028\\
    1.571\\
    4.090\\
    3.875\\
    1.890\\
};

\addplot
table[row sep=\\,y index=0] {
    data\\
    1.405\\
    1.188\\
    4.330\\
    3.665\\
    1.439\\
};

\addplot
table[row sep=\\,y index=0] {
    data\\
    2.937\\
    1.320\\
    1.357\\
    1.852\\
    1.215\\
};

\end{axis}

\end{tikzpicture}
\end{document}

enter image description here

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.