I have tried different ways to put colour legend for this chart but failed or not aligned. Can you please advise.
So \legend{} did not provide anything and \addlegendry{} is not alleged. I just want a simple legend to name each colour as type A,B,C,D,E.
Also, it is possible to add extra space between the region's box charts?
\documentclass[border=5pt]{standalone}
\usepackage{pgfplots}
\usepgfplotslibrary{statistics}
\pgfplotsset{compat=1.8}
% borrowed from <https://tex.stackexchange.com/a/145967/95441>
\pgfmathdeclarefunction{fpumod}{2}{%
\pgfmathfloatdivide{#1}{#2}%
\pgfmathfloatint{\pgfmathresult}%
\pgfmathfloatmultiply{\pgfmathresult}{#2}%
\pgfmathfloatsubtract{#1}{\pgfmathresult}%
% replaced `0' by `5' to make it work for this problem
\pgfmathfloatifapproxequalrel{\pgfmathresult}{#2}{\def\pgfmathresult{5}}{}%
}
\begin{document}
\begin{tikzpicture}
\begin{axis}[
legend entries = {A, B,C,D,E},
legend to name={legend},
% name=border,
boxplot/draw direction=y,
ylabel={time (s)},
height=8cm,
boxplot={
% in a formular:
draw position={
1/10+ floor(\plotnumofactualtype/5)
+ 1/5*fpumod(\plotnumofactualtype,5)
},
% that means the box extend must be at most 0.2 :
box extend=0.15,
},
% ... it also means that 1 unit in x controls the width:
x=3cm,
% ... and it means that we should describe intervals:
xtick={0,1,2,...,50},
% x tick label as interval,
xticklabels={%
{Region 1},%
{Region 2},%
{Region 3},%
{Region 4},%
},
x tick label style={
text width=2.5cm,
align=right,
xshift=6ex
},
cycle list={{green},{cyan},{blue},{orange},{red}},
legend pos=north west
]
\addplot table [row sep=\\,y index=0] {
data\\
6\\
51\\
71\\
50\\
125\\
};\legend{A}
\addplot table [row sep=\\,y index=0] {
data\\
8\\
69\\
90\\
70\\
153\\
};
\addplot table [row sep=\\,y index=0] {
data\\
75\\
41\\
30\\
88\\
29\\
};
\addplot table [row sep=\\,y index=0] {
data\\
56\\
80\\
356\\
20\\
36\\
};
\addplot table [row sep=\\,y index=0] {
data\\
68\\
34\\
70\\
32\\
37\\
};
% block 2
\addplot table [row sep=\\,y index=0] {
data\\
60\\
516\\
710\\
503\\
1253\\
};
\addplot table [row sep=\\,y index=0] {
data\\
60\\
516\\
710\\
503\\
1253\\
};
\addplot table [row sep=\\,y index=0] {
data\\
759\\
419\\
309\\
883\\
299\\
};
\addplot table [row sep=\\,y index=0] {
data\\
516\\
480\\
1356\\
200\\
736\\
};
\addplot table [row sep=\\,y index=0] {
data\\
684\\
340\\
700\\
325\\
377\\
};
% block 3
\addplot table [row sep=\\,y index=0] {
data\\
60\\
516\\
710\\
503\\
1253\\
};
\addplot table [row sep=\\,y index=0] {
data\\
60\\
516\\
710\\
503\\
1253\\
};
\addplot table [row sep=\\,y index=0] {
data\\
759\\
419\\
309\\
883\\
299\\
};
\addplot table [row sep=\\,y index=0] {
data\\
516\\
480\\
1356\\
200\\
736\\
};
\addplot table [row sep=\\,y index=0] {
data\\
684\\
340\\
700\\
325\\
377\\
};
% block 4
\addplot table [row sep=\\,y index=0] {
data\\
60\\
516\\
710\\
503\\
1253\\
};
\addplot table [row sep=\\,y index=0] {
data\\
60\\
516\\
710\\
503\\
1253\\
};
\addplot table [row sep=\\,y index=0] {
data\\
759\\
419\\
309\\
883\\
299\\
};
\addplot table [row sep=\\,y index=0] {
data\\
516\\
480\\
1356\\
200\\
736\\
};
\addplot table [row sep=\\,y index=0] {
data\\
684\\
340\\
700\\
325\\
377\\
};
\end{axis}
\end{tikzpicture}
\end{document}

