2

I have a set of data and I would like to highlight the difference between two different groups. For this I want to build a graph similar to the one presented below.

enter image description here

My question would be how to plot the raw data and the distribution curve for the first boxplot on my graph, as in the figure above. The following is my code used to create the three boxplot for my data.

\documentclass{standalone}

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

\begin{document}

\begin{tikzpicture}
\begin{axis}
  [
  boxplot/draw direction = y,
  ylabel = {lmean},
  xtick = {1, 2, 3},
  xticklabels = {A, B, AB},
  every axis plot/.append style = {fill, fill opacity = .1},
  ]

  \addplot + [
  mark = *,
  boxplot,
  black
  ]
  table [row sep = \\, y index = 0] {
    data \\
    23.427 \\
    23.44604 \\
    24.38042 \\
    23.38132 \\
    23.42772 \\
    23.442 \\
    22.95047 \\
    22.99269 \\
    23.40156 \\
    23.56823 \\
    22.06118 \\
    22.75578 \\
    19.4876 \\
    25.21417 \\
    21.41917 \\
    20.25453 \\
    21.32184 \\
    21.56096 \\
    20.91323 \\
    19.50758 \\
    20.6354 \\
    21.69172 \\
    20.00605 \\
    18.33375 \\
    20.35927 \\
    18.95515 \\
    20.18885 \\
    18.02808 \\
    21.67389 \\
    18.0061 \\
};

  \addplot + [
  mark = *,
  boxplot,
  black
  ]
  table [row sep = \\, y index = 0] {
    data \\
    20.25453 \\
    21.32184 \\
    21.56096 \\
    20.91323 \\
    19.50758 \\
    20.6354 \\
    21.69172 \\
    20.00605 \\
    18.33375 \\
    20.35927 \\
    18.95515 \\
    20.18885 \\
    18.02808 \\
    21.67389 \\
    18.0061 \\
};

  \addplot + [
  mark = *,
  boxplot,
  black
  ]
  table [row sep = \\, y index = 0] {
    data \\
    23.427 \\
    23.44604 \\
    24.38042 \\
    23.38132 \\
    23.42772 \\
    23.442 \\
    22.95047 \\
    22.99269 \\
    23.40156 \\
    23.56823 \\
    22.06118 \\
    22.75578 \\
    19.4876 \\
    25.21417 \\
    21.41917 \\
};
\end{axis}
\end{tikzpicture}

\end{document}

With this code I get the following figure:

enter image description here

Can anybody help me? Thanks in advance for your attention!

2

Most likely this reinvents some wheel but you can compute the average and variance of the combined data and plot the normal distribution, similarly to what's done e.g. here. BTW, it is probably easier to let pgfplots combine the data.

\documentclass[tikz,border=3mm]{standalone}
\usepackage{pgfplots}
\usepackage{pgfplotstable}
\pgfplotsset{compat = 1.17}
\usepgfplotslibrary{statistics}

\begin{document}

\begin{tikzpicture}
\pgfplotstableread[row sep = \\]{%
    data \\
    20.25453 \\
    21.32184 \\
    21.56096 \\
    20.91323 \\
    19.50758 \\
    20.6354 \\
    21.69172 \\
    20.00605 \\
    18.33375 \\
    20.35927 \\
    18.95515 \\
    20.18885 \\
    18.02808 \\
    21.67389 \\
    18.0061 \\
}\dataA
\pgfplotstableread[row sep = \\]{%
    data \\
    23.427 \\
    23.44604 \\
    24.38042 \\
    23.38132 \\
    23.42772 \\
    23.442 \\
    22.95047 \\
    22.99269 \\
    23.40156 \\
    23.56823 \\
    22.06118 \\
    22.75578 \\
    19.4876 \\
    25.21417 \\
    21.41917 \\
}\dataB
% combine tables A and B to AB (see https://tex.stackexchange.com/a/188492/194703)
\pgfplotstablevertcat{\dataAB}{\dataA} %
\pgfplotstablevertcat{\dataAB}{\dataB} %
\pgfplotstablegetrowsof{\dataAB}%
\pgfmathtruncatemacro{\numrows}{\pgfplotsretval-1}%
% compute sum, minimum, maximum and average
\edef\sumAB{0}%
\pgfplotsforeachungrouped\X in{0,...,\numrows}
{\pgfplotstablegetelem{\X}{[index]0}\of\dataAB
\pgfmathsetmacro{\sumAB}{\sumAB+\pgfplotsretval}%
\ifnum\X=0
\pgfmathsetmacro{\minAB}{\pgfplotsretval}%
\pgfmathsetmacro{\maxAB}{\pgfplotsretval}%
\else
\pgfmathsetmacro{\minAB}{min(\minAB,\pgfplotsretval)}%
\pgfmathsetmacro{\maxAB}{max(\maxAB,\pgfplotsretval)}%
\fi
}
\pgfmathsetmacro{\averageAB}{\sumAB/(\numrows+1)}
% compute variance
\edef\varsqAB{0}%
\pgfplotsforeachungrouped\X in{0,...,\numrows}
{\pgfplotstablegetelem{\X}{[index]0}\of\dataAB
\pgfmathsetmacro{\varsqAB}{\varsqAB+pow(\pgfplotsretval-\averageAB,2)}%
}
\begin{axis}
  [xmin=-1,width=12cm,height=8cm,
  ylabel = {lmean},
  xtick = {1, 2, 3},
  xticklabels = {AB, A, B},
  every axis plot/.append style = {fill, fill opacity = .1},
  ]

 \addplot[domain=\minAB:\maxAB,fill=none] ({-1.5+1.5*exp(-pow(x-\averageAB,2)/\varsqAB)},x);

 \addplot + [boxplot/draw direction = y,
  mark = *,
  boxplot,
  black
  ]
  table [y index = 0] {\dataAB};

  \addplot + [boxplot/draw direction = y,
  mark = *,
  boxplot,
  black
  ]
  table [y index = 0] {\dataA};
\typeout{\pgfplotsboxplotvalue{average}}
  \addplot + [boxplot/draw direction = y,
  mark = *,
  boxplot,
  black
  ]
  table [y index = 0] {\dataB};
\end{axis}
\end{tikzpicture}

\end{document}

enter image description here

| improve this answer | |
  • Thank you very much, @Schrödinger's cat! Would you be able to plot the points as well? Before the curve? Another question, I'm trying to increase the white space between the boxplots, but I still haven't found the solution. Would you know how to do that? – Alexandre Loures May 9 at 15:43
  • @AlexandreLoures You do not just mean increasing the width of the plot? – user194703 May 9 at 16:11

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.