0

I would like to include an average into my boxplots. The boxplots are made using https://tex.stackexchange.com/a/183856/164280 as an example. I have seen in How to show the mean or average value on a box plot with pgfplots that it is possible with boxplot prepared, but I'm using boxplot and it gives me an unknown key error. The code that works is:

\documentclass[11pt, twoside, a4paper]{report}
\usepackage[inner = 30mm, outer = 20mm,  top = 30mm, bottom = 20mm, headheight = 13.6pt]{geometry}
\usepackage{pgfplots}
\pgfplotsset{compat=1.11,
    /pgfplots/ybar legend/.style={
        /pgfplots/legend image code/.code={%
            \draw[##1,/tikz/.cd,yshift=-0.25em]
            (0cm,0cm) rectangle (3pt,0.8em);},},}
\usepgfplotslibrary{statistics}

\begin{document}

\begin{figure}
    \centering
    \begin{tikzpicture}
    \begin{axis}[
    boxplot/draw direction=y,
    ylabel={PR\textsubscript{A}},
    axis lines*=left,
    ymin = 0.75, ymax = 0.95,
    ymajorgrids,
    y tick label style={
    /pgf/number format/.cd,
    fixed,
    fixed zerofill,
    precision=2,
    /tikz/.cd
    },  
    height=8cm,
    boxplot={
    %
    % Idea: 
    %  place the 
    %  group 1 at 0.3333 and 0.6666
    %  group 2 at 1.3333 and 1.6666
    %  group 3 at 2.3333 and 2.6666
    %  ...
    % in a formular:
    draw position={1/3 + floor(\plotnumofactualtype/2) + 1/3*mod(\plotnumofactualtype,2)},
    %
    % that means the box extend must be at most 0.33333 :
    box extend=0.3,
    },
    % ... it also means that 1 unit in x controls the width:
    x=3.2cm,
    % ... and it means that we should describe intervals:
    xtick={0,1,2,...,10},
    x tick label as interval,
    xticklabels={%
        {System 1\\{\small without/with}},%
        {System 2\\{\small without/with}},%
        {System 3\\{\small without/with}},%
        {System 4\\{\small without/with}},%
    },
    x tick label style={
        text width=5cm,
        align=center
    },
    ]
\addplot[boxplot={ every box/.style={very thick,draw=red!50!black},
    every whisker/.style={red!50!black,very thick},
    every median/.style={red!50!black,very thick}}]
    table[row sep=\\,y index=0] {
    data\\
    0.926\\
    0.940\\
    0.862\\
    0.849\\
    0.871\\
    0.894\\
    0.903\\
    0.885\\
    0.892\\
    0.837\\
    0.814\\
    0.818\\
    0.810\\ 
};
\addplot[boxplot={ every box/.style={very thick,draw=red!50!black, fill = black!10},
every whisker/.style={red!50!black,very thick},
every median/.style={red!50!black,very thick}}]
table[row sep=\\,y index=0] {
data\\
0.923\\
0.944\\
0.892\\
0.857\\
0.904\\
0.938\\
0.930\\
0.907\\
0.910\\
0.874\\
0.857\\
0.854\\
0.853\\     
};
\addplot[boxplot={ every box/.style={very thick,draw=blue!50!black},
    every whisker/.style={blue!50!black,very thick},
    every median/.style={blue!50!black,very thick}}]
    table[row sep=\\,y index=0] {
    data\\
    0.923\\
    0.921\\
    0.857\\
    0.867\\
    0.886\\
    0.880\\
    0.886\\
    0.888\\
    0.893\\
    0.826\\
    0.818\\
    0.857\\
    0.848\\
};
\addplot[boxplot={ every box/.style={very thick,draw=blue!50!black,  fill = black!10},
every whisker/.style={blue!50!black,very thick},
every median/.style={blue!50!black,very thick}}]
table[row sep=\\,y index=0] {
data\\
0.913\\
0.916\\
0.881\\
0.869\\
0.917\\
0.919\\
0.909\\
0.905\\
0.907\\
0.859\\
0.858\\
0.892\\
0.891\\ 
};
\addplot[boxplot={ every box/.style={very thick,draw=green!50!black},
    every whisker/.style={green!50!black,very thick},
    every median/.style={green!50!black,very thick}}]
table[row sep=\\,y index=0] {
    data\\
    0.915\\
    0.912\\
    0.840\\
    0.845\\
    0.873\\
    0.868\\
    0.875\\
    0.877\\
    0.883\\
    0.812\\
    0.789\\
    0.840\\
    0.833\\ 
};
\addplot[boxplot={ every box/.style={very thick,draw=green!50!black,  fill = black!10},
every whisker/.style={green!50!black,very thick},
every median/.style={green!50!black,very thick}}]
table[row sep=\\,y index=0] {
data\\
0.907\\
0.910\\
0.869\\
0.848\\
0.905\\
0.904\\
0.889\\
0.889\\
0.891\\
0.836\\
0.820\\
0.868\\
0.868\\     
};
\addplot[boxplot={ every box/.style={very thick,draw=white!50!black},
    every whisker/.style={white!50!black,very thick},
    every median/.style={white!50!black,very thick}}]
    table[row sep=\\,y index=0] {
    data\\
    0.895\\
    0.887\\
    0.809\\
    0.809\\
    0.847\\
    0.848\\
    0.844\\
    0.854\\
    0.863\\
    0.775\\
    0.761\\
    0.789\\
    0.883\\ 
};
\addplot[boxplot={ every box/.style={very thick,draw=white!50!black,  fill = black!10},
every whisker/.style={white!50!black,very thick},
every median/.style={white!50!black,very thick}}]
table[row sep=\\,y index=0] {
data\\
0.878\\
0.878\\
0.838\\
0.817\\
0.884\\
0.893\\
0.879\\
0.877\\
0.877\\
0.817\\
0.810\\
0.830\\
0.936\\ 
};
\end{axis}
\end{tikzpicture}
\caption{Distribution of array performance ratios without and with temperature correction}
\label{fig:distribution pr}
\end{figure}
\end{document}

This results in: enter image description here And I would like to add the average to each boxplot with a big dot in the same color as the line.

1 Answer 1

2

Judging by the manual, pgfplots doesn't calculate the mean values. If calculating them manually is an option, then you could use a scatter plot. Add

scatter/classes={
  a={mark=*,red!50!black},
  b={mark=*,blue!50!black},
  c={mark=*,green!50!black},
  d={mark=*,white!50!black}
 }

to the axis options, and then use

\addplot [scatter,only marks,scatter src=explicit symbolic] 
   table [
     x expr={1/3 + floor(\coordindex/2) + 1/3*mod(\coordindex,2)},
     y=y,
     meta=meta
    ]
{
y meta
0.86 a
0.89 a
0.86 b
0.89 b
0.86 c
0.89 c
0.82 d
0.85 d
};

You have to modify the y-values of course. Note the labels in the meta column correspond to the classes in the scatter setting, that's how the colours are set.

enter image description here

Complete code:

\documentclass[11pt, twoside, a4paper]{report}
\usepackage[inner = 30mm, outer = 20mm,  top = 30mm, bottom = 20mm, headheight = 13.6pt]{geometry}
\usepackage{pgfplots}
\pgfplotsset{compat=1.11}
\usepgfplotslibrary{statistics}

\begin{document}

\begin{figure}
    \centering
    \begin{tikzpicture}
    \begin{axis}[
    boxplot/draw direction=y,
    ylabel={PR\textsubscript{A}},
    axis lines*=left,
    ymin = 0.75, ymax = 0.95,
    ymajorgrids,
    y tick label style={
    /pgf/number format/.cd,
    fixed,
    fixed zerofill,
    precision=2,
    /tikz/.cd
    },  
    height=8cm,
    boxplot={
    %
    % Idea: 
    %  place the 
    %  group 1 at 0.3333 and 0.6666
    %  group 2 at 1.3333 and 1.6666
    %  group 3 at 2.3333 and 2.6666
    %  ...
    % in a formular:
    draw position={1/3 + floor(\plotnumofactualtype/2) + 1/3*mod(\plotnumofactualtype,2)},
    %
    % that means the box extend must be at most 0.33333 :
    box extend=0.3,
    },
    % ... it also means that 1 unit in x controls the width:
    x=3.2cm,
    % ... and it means that we should describe intervals:
    xtick={0,1,2,...,10},
    x tick label as interval,
    xticklabels={%
        {System 1\\{\small without/with}},%
        {System 2\\{\small without/with}},%
        {System 3\\{\small without/with}},%
        {System 4\\{\small without/with}},%
    },
    x tick label style={
        text width=5cm,
        align=center
    },
    scatter/classes={
      a={mark=*,red!50!black},
      b={mark=*,blue!50!black},
      c={mark=*,green!50!black},
      d={mark=*,white!50!black}
     }
    ]
\addplot[boxplot={ every box/.style={very thick,draw=red!50!black},
    every whisker/.style={red!50!black,very thick},
    every median/.style={red!50!black,very thick}}]
    table[row sep=\\,y index=0] {
    data\\
    0.926\\
    0.940\\
    0.862\\
    0.849\\
    0.871\\
    0.894\\
    0.903\\
    0.885\\
    0.892\\
    0.837\\
    0.814\\
    0.818\\
    0.810\\ 
};
\addplot[boxplot={ every box/.style={very thick,draw=red!50!black, fill = black!10},
every whisker/.style={red!50!black,very thick},
every median/.style={red!50!black,very thick}}]
table[row sep=\\,y index=0] {
data\\
0.923\\
0.944\\
0.892\\
0.857\\
0.904\\
0.938\\
0.930\\
0.907\\
0.910\\
0.874\\
0.857\\
0.854\\
0.853\\     
};
\addplot[boxplot={ every box/.style={very thick,draw=blue!50!black},
    every whisker/.style={blue!50!black,very thick},
    every median/.style={blue!50!black,very thick}}]
    table[row sep=\\,y index=0] {
    data\\
    0.923\\
    0.921\\
    0.857\\
    0.867\\
    0.886\\
    0.880\\
    0.886\\
    0.888\\
    0.893\\
    0.826\\
    0.818\\
    0.857\\
    0.848\\
};
\addplot[boxplot={ every box/.style={very thick,draw=blue!50!black,  fill = black!10},
every whisker/.style={blue!50!black,very thick},
every median/.style={blue!50!black,very thick}}]
table[row sep=\\,y index=0] {
data\\
0.913\\
0.916\\
0.881\\
0.869\\
0.917\\
0.919\\
0.909\\
0.905\\
0.907\\
0.859\\
0.858\\
0.892\\
0.891\\ 
};
\addplot[boxplot={ every box/.style={very thick,draw=green!50!black},
    every whisker/.style={green!50!black,very thick},
    every median/.style={green!50!black,very thick}}]
table[row sep=\\,y index=0] {
    data\\
    0.915\\
    0.912\\
    0.840\\
    0.845\\
    0.873\\
    0.868\\
    0.875\\
    0.877\\
    0.883\\
    0.812\\
    0.789\\
    0.840\\
    0.833\\ 
};
\addplot[boxplot={ every box/.style={very thick,draw=green!50!black,  fill = black!10},
every whisker/.style={green!50!black,very thick},
every median/.style={green!50!black,very thick}}]
table[row sep=\\,y index=0] {
data\\
0.907\\
0.910\\
0.869\\
0.848\\
0.905\\
0.904\\
0.889\\
0.889\\
0.891\\
0.836\\
0.820\\
0.868\\
0.868\\     
};
\addplot[boxplot={ every box/.style={very thick,draw=white!50!black},
    every whisker/.style={white!50!black,very thick},
    every median/.style={white!50!black,very thick}}]
    table[row sep=\\,y index=0] {
    data\\
    0.895\\
    0.887\\
    0.809\\
    0.809\\
    0.847\\
    0.848\\
    0.844\\
    0.854\\
    0.863\\
    0.775\\
    0.761\\
    0.789\\
    0.883\\ 
};
\addplot[boxplot={ every box/.style={very thick,draw=white!50!black,  fill = black!10},
every whisker/.style={white!50!black,very thick},
every median/.style={white!50!black,very thick}}]
table[row sep=\\,y index=0] {
data\\
0.878\\
0.878\\
0.838\\
0.817\\
0.884\\
0.893\\
0.879\\
0.877\\
0.877\\
0.817\\
0.810\\
0.830\\
0.936\\ 
};

\addplot [scatter,only marks,scatter src=explicit symbolic] 
   table [
     x expr={1/3 + floor(\coordindex/2) + 1/3*mod(\coordindex,2)},
     y=y,
     meta=meta
    ]
{
y meta
0.86 a
0.89 a
0.86 b
0.89 b
0.86 c
0.89 c
0.82 d
0.85 d
};
\end{axis}
\end{tikzpicture}
\caption{Distribution of array performance ratios without and with temperature correction}
\label{fig:distribution pr}
\end{figure}
\end{document}
2
  • +1. But I thought the mean values could be computed, at least this is how I read the part "Analyzing Samples Automatically" that starts on p. 501 of the pgfplots manual.
    – user121799
    Jul 24, 2018 at 11:49
  • @marmot There it says that median, quartiles and whiskers are computed, the average isn’t mentioned. Jul 24, 2018 at 12:02

You must log in to answer this question.

Not the answer you're looking for? Browse other questions tagged .