Pimping your forest plot

A forest plot using different markers for the two groups
A forest plot using different markers for the two groups

In order to celebrate my Gmisc-package being on CRAN I decided to pimp up the forestplot2 function. I had a post on this subject and one of the suggestions I got from the comments was the ability to change the default box marker to something else. This idea had been in my mind for a while and I therefore put it into practice.

Forest plots (sometimes concatenated into forestplot) date back at least to the wild ’70s. They are an excellent tool to display lots of estimates and confidence intervals. Traditionally they’ve been used in meta-analyses although I think the use is spreading and I’ve used them a lot in my own research.

For convenience I’ll use the same setup in the demo as in the previous post:

Here is the basic multi-line forest plot:


The package comes with the following functions for drawing each confidence interval:

  • fpDrawNormalCI – the regular box confidence interval
  • fpDrawCircleCI – draws a circle instead of a box
  • fpDrawDiamondCI – draws a diamond instead of a box
  • fpDrawPointCI – draws a point instead of a box
  • fpDrawSummaryCI – draws a summary diamond

Below you can find the all four alternatives:


The fn.ci_norm accepts either a single function, a list of functions, a function name, or a vector/matrix of names. If the list is one-leveled or you have a vector in a multi-line situation it will try to identify if the length matches row or column. The function tries to rewrite to match your the dimension of your multi-line plot, hence you can write:

If you use the same number of lines as the lines the legend will automatically use your custom markers although you can always just use the legendMarkerFn argument:


Now for the pch-argument in the fpDrawPointCI you can use any of the predefined integers:


If you are still not satisfied you have always the option of writing your own function. I suggest you start with copying the fpDrawNormalCI and see what you want to change:

The estimate and the confidence interval points are provided as raw numbers and are assumed to be “native”, that is their value is the actual coefficient and is mapped correctly as is.

Note that all the regression functions in the Gmisc-package have moved to the Greg-package, soon to be available on CRAN… but until I have added some more unit tests you need to use the GitHub version.

Flattr this!

This entry was posted in R, Tutorial. Bookmark the permalink.

16 Responses to Pimping your forest plot

  1. sokha says:

    Could you make x-axis coordinate for one line?

    • Max Gordon says:

      I’m not sure what you mean x-axis coordinate.

      • sokha says:

        Many thanks for your reply. Mean that zero=0, I want zero=1.

        • Max Gordon says:

          Just set the zero option to 1. Below is an example where the zero effect is 0.15 with a tick mark at that point (the example is using the tutorial data):

  2. sokha says:

    Dear Dr.Max Gordon,

    I would like to thank you very much for your forest plot. I have found it very useful.
    And, I have learn from the forest plot you posted.

  3. Manny says:

    Dear Max,
    I found the forestplot2 very useful.
    How can I combine multiple forestplot2 into a single plot?
    Since Gmisc is not a base graphics I cannot use layout, par(mfrow), or even grid.arrange..

    Many thanks.

    • Max Gordon says:

      All the graphs rely heavily on the grid package, you need to use the viewport system. It may seem complicated but the structure is actually rather simple and to get the plots aligned side by side you simply specify:

      • Manny says:

        Hi Max,
        Many thanks for the prompt response and sorry for the delay.
        When using the your code above I got an error after ‘popViewport()’ saying:
        Error in grid.Call.graphics(L_unsetviewport, as.integer(n)) :
        cannot pop the top-level viewport (‘grid’ and ‘graphics’ output mixed?)

        Any clue on what’s wrong?
        (to quickly try things I’m using your forestplot2 code: http://gforge.se/2014/02/pimping-your-forest-plot/#more-1160)

        • Max Gordon says:

          You have most likely omitted a pushViewport(). If you just run:

          It will give the error message:

          Error in grid.Call.graphics(L_unsetviewport, as.integer(n)) :
          cannot pop the top-level viewport ('grid' and 'graphics' output mixed?)

        • Manny says:

          Hi Max,
          Please ignore the query above. In the forestplot2 code, I set the option new_page=FALSE, and the viewport() worked fine, thanks.

          On another note, I noticed CRAN removed the package ‘grid’ so I couldn’t find too much detail about it. For example, do you know how to specify a common legend for a grid of plots?

          • Max Gordon says:

            The grid-package is now part of the source code, you can find it here. Paul Murrell has a page on the grid-package although it is slightly outdated. There are some excellent vignettes for the grid package, just write vignette(package="grid")

  4. Lukasz says:

    Hi Max, thanks for the helpful post. Do you know if there’s any way to create a range for the zero effect, which would be visualized as a wider bar than just a line? For example, if zero effect was anywhere from 50-55 degrees.

  5. Pilar Endara says:

    Hello Dr Gordon! thank ou very much for this post, it has been extremely useful. I’m now trying to plot the summary effect size in the very last row of the forestplot I’ve got. I tried to use the function fpDrawSummaryCI(lower_limit, estimate, upper_limit, size, col, y.offset = 0.5,

    I’ve gotten the errors
    “Error in prFpGetConfintFnList(fn = confintNormalFn, no_rows = NROW(mean), :
    You do not have the same number of confidence interval functions as you have number of rows: 7!=62 You should provide the same number.”


    “Error in unit(x, default.units) : ‘x’ and ‘units’ must have length > 0”

    So, maybe in the first error it means I have to add the values of the summary effect on the matrix long before or can I put them just directly into the function (that’s actuall what I’m doing).

    For the second error, I made a conversion to npc as suggested, but apparently the values are fine.

    could you please tell me how can I put this summary effect size? maybe I’m too lost and has nothing to do with the function fpDrawSummaryCI. Therefore, any help would be highly appreciated! thank you

    • Max Gordon says:

      The fpDrawSummaryCI is only used for drawing the confidence intervals. I think that what you want is a to set the parameter is.summary. If you have 7 rows and the last one is a summary row then you set the variable to is.summary = c(rep(FALSE, time=6), TRUE).


  6. Branwen Owen says:

    Hi Max, I really like the look of these plots and I’ve been trying to work out if the forestplot package can do what I want. I’d like to use it as a traditional forest plot for a meta-analysis, but with several estimates per study, with columns for both the study names and the various variables that I’m comparing. I’d like to colour code the different types of variables. Am I right in understanding that the forestplot package can set different colours when there’s more than one estimate per row, but can not set different across rows?

Leave a Reply

Your email address will not be published. Required fields are marked *


This site uses Akismet to reduce spam. Learn how your comment data is processed.