2

I'm trying to create a 'Forest Plot' in TikZ. An example of this is below:

Forest Plot

I have experience with PGFPlots but I can't find an example or even a starting point for something like this. The only way I can think of is to draw this using low-level TikZ with absolute positioning. However, I'm almost sure there has to be a better way.

All I'm looking for is some ideas for strategy on how to draw this best when ther are 100+ rows. Then, I can try it out and come back with specific questions.

p.s. Explanation of the plot. Every "row" is defined by 3 numbers. The point in the middle is the mean, it extends to either side by two numbers which might not be equal and give 95% CI.

  • 1
    It is called error plot. Check the error bars section (page 259) of pgfplots manual. – user11232 Oct 4 '15 at 14:40
  • Umm, thanks but I did take a look at that. I'm not sure if error bars serves the purpose here. The rows are independent of each other and there is no linearity on the x-axis. Maybe I'm completely misreading the text here. :( – recluze Oct 4 '15 at 15:46
  • Well, seeing the answer, you were right (of course). Just outside my skill level. – recluze Oct 5 '15 at 12:02
5

Here's one way using pgfplot's error bars:

\documentclass{article}

\usepackage{pgfplots}
\usepackage{filecontents}

\pgfplotsset{compat=1.12}

\begin{filecontents}{data.txt}
y x plusError minusError
sigma 0.55 0.05 0.1
sigmaLog -0.7 0.1 0.1
gamma 0.1 0.2 0.2
beta -0.1 0.1 0.1
alpha 1.7 1.2 1.2
\end{filecontents}

\begin{document}
\noindent
\begin{tikzpicture}
  \begin{axis}[ytick=data, yticklabels from table={data.txt}{y}, grid=both, 
               width=\textwidth, height=10cm, axis background/.style={fill=gray!20},
               major grid style={white}, xtick={-1,0,...,3}, tick style={draw=none},
               separate axis lines, axis line style={draw opacity=0}, title={\large 95\% Credible Intervals}]
    % Confidence intervals
    \addplot [blue, only marks, mark=*, error bars/.cd, error mark=none, x dir=both, x explicit]
             table [x=x, y expr=\coordindex, x error plus=plusError, x error minus=minusError] {data.txt};
    % 0 line
    \draw [thick, dashed] (0, -5) -- (0, 5);
  \end{axis}
\end{tikzpicture}
\end{document}

enter image description here

I did quite a lot of customizing to match your image exactly, the part you really want is the tick settings passed to axis and the \addplot command. The only hard coded part is the vertical line, but it's easy to change that (just modify the coordinates to make it longer).

  • That is amazing man. I will need some time to understand the code and fix it for many rows but it seems to be working well for a small number of rows. – recluze Oct 5 '15 at 12:02
  • For me, the code posted does not work. I need to change {draw opacity=0} to {draw opacity=100}, or at least {draw opacity=x} where x is positive. I don't know whether that's because of an idiosyncrasy of my setup. – Martin J. Osborne May 1 '17 at 2:25

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.