forrest() draws a forest plot from any data frame with one row per estimate and columns for the point estimate and the confidence limits. The rows can be regression coefficients, subgroup estimates, studies in a meta-analysis or exposure categories. The package’s only dependency is tinyplot, and it accepts data frames, tibbles and data.tables.
This vignette shows each feature on a small example. The data in every example are invented for illustration.
Basic forest plot
A call needs three column names: estimate, lower and upper. Here they hold coefficients from a linear model of systolic blood pressure (SBP).
When section names a column, forrest() inserts a bold header row wherever that column’s value changes, indents the labels beneath it, and adds a blank row after each section. Rows stay in the order they have in the data, so sort the data first if the sections should appear in a particular order.
Rows where the is_summary column is TRUE are drawn as filled diamonds spanning the confidence interval, the usual mark for a pooled or overall estimate.
A group column colours each row by its value and adds a legend. The default palette is Okabe-Ito, which stays distinguishable under the common forms of colour blindness. To choose the colours yourself, pass col, either as an unnamed vector used in order of appearance or as a vector named by group level.
With dodge = TRUE, consecutive rows that share a label value are drawn as one band: their intervals are offset vertically around the band’s centre and the label appears once. Pair it with group so the reader can tell the series apart.
A number instead of TRUE sets the distance between neighbouring intervals in a band, in units of one row; TRUE means 0.25. Section and subsection headers and spacer rows are never merged into a band.
One text column per series
By default each text column in cols prints a value beside every interval, at that interval’s offset position. With cols_by_group = TRUE, each text column instead prints one value per band, at the band’s centre: the first non-empty value among the band’s rows. Giving each series its own column, filled only on that series’ rows, produces a wide table with one row per label and one column per series.
A shape column gives each of its values a different point character and adds a second legend. Combined with group and dodge, a single band can then encode two variables, here period by colour and sex by shape.
The shape legend sits at legend_shape_pos, "bottomright" by default, and legend_shape_pos = NULL removes it. legend_pos does the same for the colour legend.
Text columns
cols adds text columns to the right of the plot. It is a named character vector: each name is a column header and each value is the name of a column in data. Format the text yourself, for instance with sprintf(), so that it shows exactly the digits you want. widths sets the relative widths of the label panel, the plot and each text column.
Text columns are empty on the header rows that section inserts. section_cols fills chosen text columns on those rows with a section-level value. Its names must be names of cols, and its values are columns in data; on each header row, the named text column shows the first non-empty value of that data column within the section. subsection_cols does the same for subsection headers. Here each row shows its own sample size and each header shows the section’s total.
theme takes the name of a built-in theme ("default", "minimal" or "classic") or a named list that overrides individual keys of the default theme. The keys are grid_col, grid_lty and grid_lwd for the horizontal gridlines, ref_col and ref_lty for the reference line, and stripe_col for striped rows.
Graphical parameters that forrest() does not name, such as cex.axis and cex.lab, are passed through ... to the call that sets up the plot region.
Saving plots
save_forrest() writes a plot to PDF, PNG, SVG or TIFF, choosing the device from the file extension. It takes a function with no arguments that draws the plot, so the device is opened before drawing starts and closed even if drawing fails. dpi sets the resolution of PNG and TIFF files.