# Combine boxplot and histogram in one diagram (PGF/TikZ)

For my bachelor thesis I did a questionnaire, now I want to evaluate and visualise it in a diagram, combining multiple values. Most of the answers from this survey are in a scale of 1-5 or 1-6. I want to display them in a histogram for absolute values and also in a boxplot for median, mean, standard deviation,...

To work efficiently with the space, I want to display them in a table, left is the question, right the diagram. Currently the data is processed in two diagrams, instead of one.

What I would like to have, is a combination of those two into one diagram, containing the boxplot and a histogram. Here is an example of what I wanted to do.

After several hours of reading, followed by trial-and-error, I didn't get any further. I wonder if anyone can help me with getting the design right. Here is an image that explains the values in the german examples.

Is it even possible to do this with LaTex?

It would already help to know the term for such a diagram

Edit: This is the minimal code example, generating the plot in the first picture.

\documentclass[11pt]{scrartcl}
\usepackage[utf8]{inputenc}
\usepackage{pgf}
\usepackage{pgfplots}
\usepgfplotslibrary{statistics, external}
\usetikzlibrary{fit,calc}
\usepackage{mathtools}
\usepackage[ngerman]{babel}

\newcommand*{\boxplot}[6]{
% As seen in several examples
% #1: center, #2: median, #3: 1/4 quartile, #4: 3/4 quartile, #5: min, #6: max
line width=.2mm,
black,
boxplot prepared={
lower whisker={#5},
lower quartile={#3},
median={#2},
upper quartile={#4},
upper whisker={#6},
draw position = 10,
box extend = 15,
whisker extend = 17,
}
]
coordinates{};
}

\begin{document}

\begin{table}
\centering
\begin{tabular}{p{7.5cm}|p{7.5cm}}
\hline
Wie wichtig ist es dir, dass du mit deinen Kommilitonen gut auskommst? &
\begin{tikzpicture}
\begin{axis}[
height=2cm, width=7cm,
ybar, ymax=19, ymin=0, ytick={0,19}, yticklabels={0,19},
xmin=0, xmax=6, xtick={1,...,5}, xticklabels={$\text{gar nicht}$,2,3,4,$\text{sehr}$}
] \addplot coordinates {(1,0) (2,0) (3,1) (4,8) (5,10)};
\end{axis}
\end{tikzpicture}
\hspace*{0.3cm}
\begin{tikzpicture}
\begin{axis}[
height=2cm, width=7cm,
ymin=0, ymax=19, ytick={0,19}, yticklabels={,},
xmin=0, xmax=6, xtick={1,...,5}, xticklabels={$\text{gar nicht}$,2,3,4,$\text{sehr}$},
boxplot/every median/.style={draw=red},
] \boxplot{4.4736842105}{5}{3.91966759}{5.027700831}{3}{5}
\end{axis}
\end{tikzpicture}\\
\hline
\end{tabular}
\end{table}

\end{document}

• Welcome to TeX.SX! Of course it is possible. But could you please add a minimal compilable code example (MWE) of what you've tried so far? That will help us help you. Jan 18, 2019 at 15:25
• In support of TeXnician, I would like to add that you can just use two axis environments whereby the second one is shifted downwards. Or you can use a group plot for this. All this is straightforward, but no one is keen on punching in the texts from your screen shot, I guess.
– user121799
Jan 18, 2019 at 15:30

Using only one tikzpicture environment and tweaking some settings results in the following plot (leaving most of your code unchanged).

Smaller remarks: The axis y line*=right setting changes the location of the second axis to the right-hand side of the plot (which is not needed in this case, because we don't have any visible axis labels. However, as the second plot may have an axis, it is useful to move it to the right-hand side.)

\documentclass[11pt]{scrartcl}
\usepackage[utf8]{inputenc}
\usepackage{pgf}
\usepackage{pgfplots}
\usepgfplotslibrary{statistics, external}
\usetikzlibrary{fit,calc}
\usepackage{mathtools}
\usepackage[ngerman]{babel}

\newcommand*{\boxplot}[6]{
% As seen in several examples
% #1: center, #2: median, #3: 1/4 quartile, #4: 3/4 quartile, #5: min, #6: max
line width=.2mm,
black,
boxplot prepared={
lower whisker={#5},
lower quartile={#3},
median={#2},
upper quartile={#4},
upper whisker={#6},
draw position = 15,
box extend = 3,
whisker extend = 3,
}
]
coordinates{};
}

\begin{document}

\begin{table}
\centering
\begin{tabular}{p{7.5cm}|p{7.5cm}}
\hline
Wie wichtig ist es dir, dass du mit deinen Kommilitonen gut auskommst? &
\begin{tikzpicture}
\begin{axis}[
height=4cm, width=7cm,
ybar, ymax=19, ymin=0, ytick={0,19}, yticklabels={0,19},
xmin=0, xmax=6, xtick={1,...,5}, xticklabels={$\text{gar nicht}$,2,3,4,$\text{sehr}$}
] \addplot coordinates { (3,1) (4,8) (5,10)};
\end{axis}

\begin{axis}[
height=4cm, width=7cm,
axis x line*=top,
axis y line*=right,
ymin=0, ymax=19, ytick={0,19}, yticklabels={},
xmin=0, xmax=6, xtick={1,...,5}, xticklabels={0\%,0\%,10\%,40\%,50\%},
boxplot/every median/.style={draw=red},
] \boxplot{4.4736842105}{5}{3.91966759}{5.027700831}{3}{5}
\end{axis}
\end{tikzpicture}\\
\hline
\end{tabular}
\end{table}

\end{document}


This results in a plot looking like this:

• Thank you. Now I get how to have two axis environments. Could you elaborate what axis y line*=right does, especially the star? And do you think there's an easy way to have the relative frequencies of answers as upper x-axis labels? Jan 18, 2019 at 17:44
• Sure, I changed the code a bit. The axis y line*=right changes the position of the axis to the right-hand side. The star hides the default arrow of the axis. Also, I added some arbitrary percentages.
– Tom
Jan 18, 2019 at 18:00