I would like to create a plot similarly as to the bar plot example, but with boxplots instead of bars.
So far what I have this MWE:
\documentclass{article}
\usepackage{amsmath}
\usepackage{pgfplots}
\pgfplotsset{compat=newest}
\usepgfplotslibrary{statistics}
\begin{document}
\pgfplotstableread{
%\begin{filecontents}{test.dat}
error
1
2
3
5
5
5
5
5
5
15
%\end{filecontents}
}\mytable
%Define the color series
\definecolor{RYB1}{RGB}{230,97,1}
\definecolor{RYB2}{RGB}{200,150,250}
\pgfplotscreateplotcyclelist{colorbrewer-RYB}{
{RYB1!50!black,fill=RYB1},
{RYB2!50!black,fill=RYB2}} %Do not add a comma after the last element of the list!
%Define the width of the boxes
\def\boxwidth{0.75}%
\begin{tikzpicture}
\begin{axis}[
scale only axis,
axis on top,
ymin=0, ymax=20,
ylabel={Error [m]},
boxplot/draw direction=y,
cycle list name=colorbrewer-RYB,
xtick={1.5, 4.5},
xticklabels={Group A, Group B},
tick align=inside,
xtick style={draw=none},
legend style={at={(0.5,-0.15)}, anchor=north,legend columns=-1},
legend image code/.code={
\draw[#1, draw=none] (0cm,-0.1cm) rectangle (0.6cm,0.1cm);
},
]
%Group A
\addplot+[
boxplot={
draw position=1,
average, % <---- Where is the average?
box extend=\boxwidth
},
]
table[ y index=0, row sep=newline ]{\mytable}; % <---- Where are the outliers?
\addlegendentry{Yes;};
\addplot+[
boxplot={
draw position=2,
every average, % <---- This was a desperate try with a surprising result
box extend=\boxwidth
},
]
table[ y index=0, row sep=newline ]{\mytable};
\addlegendentry{No;};
%Group B
\addplot+[
boxplot prepared={draw position=4,
lower whisker=2.5,
lower quartile=4,
median=5,
average=6,
upper quartile=8,
upper whisker=9,
box extend=\boxwidth,
}
]
coordinates {(0,12) (0,10)};
\addplot+[
boxplot prepared={draw position=5,
lower whisker=2.5,
lower quartile=4,
median=8.5,
average=10,
upper quartile=12,
upper whisker=15,
box extend=\boxwidth,
},
]
table[row sep=\,y index=0] { 0\ 14\ 15\ };
\end{axis}
\end{tikzpicture}
\end{document}
which results in

Additions/Features Request
It would be nice to be able to create this type of grouped boxplots similarly as it is done with the bar plots. In these, one can specify a set of symbolic coordinates and simply add bars using those coordinates. Bars with the same coordinate then form a group, in which the spacing is well taken care of automatically.
In the presented MWE all the many dimensional quantities are defined manually. If possible it would be nice to be able to create the figure so that it scales automatically upon defining its desired height and width, for example using something like
\setlength\figureheight{1.5in}% \setlength\figurewidth{1.5in}%so that there is no need to change the lengths manually. Towards this end, I guess that being able to add the boxes using symbolic coordinates would be helpful.
Issues with the presented MWE
The data from the first two plots (Group A) is taken from the same table, whereas the boxes of the other two (Group B) are prepared. The previous figure has the following problems (which I don't know how to solve, obviously I am doing something wrong...)
Issues with Averages
- In the first box I used the option 'average' with the intention to plot the average, but it does not work.
- In the second I tried (by accident) to use 'every average' instead, with a surprising result: it plots the outliers.
How to plot the averages correctly? If possible so that the shape can be selected (the same shape for all boxes).
Issues with Outliers
I also want to plot the outliers in all the boxes, also if possible being able to select the shape (the same for all the boxes).
In the case of the first 2 plots, being the raw data imported from a table, I guess that the calculation of the outliers is automatic. Perhaps I need to add the right option... I How to add them?
In the case of the prepared plots, I tried adding coordinates and a table, with no success. What am I missing?