Package 'forplot'

Title: Forest plots
Description: The package generates forest plots.
Authors: Lukas Bütikofer [aut, cre] (ORCID: <https://orcid.org/0000-0002-0801-746X>)
Maintainer: Lukas Bütikofer <[email protected]>
License: MIT + file LICENSE
Version: 1.0.3
Built: 2026-07-03 17:27:10 UTC
Source: https://github.com/CTU-Bern/forplot

Help Index


b_axis

Description

Modify boxplot (b) items of a forest plot object (fobj). Passed to axis.

Usage

b_axis(fobj, item = NULL, ...)

Arguments

fobj

a forest plot object of class 'fobj'

item

item to be modified, either a number or the name of the column in fobj$dat. If NULL (the default), all items of type 'b' are affected.

...

options to be passed to axis.

Value

a forest plot object of class 'fobj'

Examples

fobj<-genfobj(layout = c("t","t","t","t","t","b","t","f","t"),
dat = forplotdata, obs = forplotdata_bp,
 lwidths = c(0.6,0.4,0.6,0.4,0.6,1,1,1,0.5))
fobj<-b_axis(fobj, at = seq(2,10,by=2), labels = seq(2,10,by=2))
plotfobj(fobj)

b_boxplot

Description

Modify boxplot (b) items of a forest plot object (fobj). Passed to boxplot.

Usage

b_boxplot(fobj, item = NULL, ...)

Arguments

fobj

a forest plot object of class 'fobj'

item

item to be modified, either a number or the name of the column in fobj$dat. If NULL (the default), all items of type 'b' are affected

...

options to be passed to boxplot

Value

a forest plot object of class 'fobj'

Examples

fobj<-genfobj(layout = c("t","t","t","t","t","b","t","f","t"),
dat = forplotdata, obs = forplotdata_bp,
 lwidths = c(0.6,0.4,0.6,0.4,0.6,1,1,1,0.5))
fobj<-b_boxplot(fobj, boxwex = 0.2)
plotfobj(fobj)

f_arrows

Description

Modify arrows (confidence intervals) in forest (f) items of a forest plot object (fobj). Passed to arrows.

Usage

f_arrows(fobj, item = NULL, ...)

Arguments

fobj

a forest plot object of class 'fobj'

item

item to be modified, either a number or the name of the column in fobj$dat. If NULL (the default), all items of type 'f' are affected

...

options to be passed to arrows

Value

a forest plot object of class 'fobj'

Examples

fobj<-genfobj(layout = c("t","t","t","t","t","t","f","t"),
	dat = forplotdata, lwidths = c(0.8,0.4,0.6,0.4,0.6,1,1,0.5))
fobj<-f_arrows(fobj = fobj, col = 2)
plotfobj(fobj)

f_axis

Description

Modify axis in forest (f) items of a forest plot object (fobj). Passed to axis.

Usage

f_axis(fobj, item = NULL, ...)

Arguments

fobj

a forest plot object of class 'fobj'

item

item to be modified, either a number or the name of the column in fobj$dat. If NULL (the default), all items of type 'f' are affected

...

options to be passed to axis

Value

a forest plot object of class 'fobj'

Examples

fobj<-genfobj(layout = c("t","t","t","t","t","t","f","t"),
	dat = forplotdata, lwidths = c(0.8,0.4,0.6,0.4,0.6,1,1,0.5))
fobj<-f_axis(fobj = fobj, at = seq(-1,0.2, by=0.4),
labels=seq(-1,0.2, by=0.4), tck=-0.03, mgp = c(2,0.5,0))
plotfobj(fobj)

f_direction

Description

Add and modify direction indicator in forest (f) items of a forest plot object (fobj). Passed to mtext.

Usage

f_direction(fobj, item = NULL, ...)

Arguments

fobj

a forest plot object of class 'fobj'

item

item to be modified, either a number or the name of the column in fobj$dat. If NULL (the default), all items of type 'f' are affected

...

options to be passed to mtext

Value

a forest plot object of class 'fobj'

Examples

fobj<-genfobj(layout = c("t","t","t","t","t","t","f","t"),
	dat = forplotdata, lwidths = c(0.8,0.4,0.6,0.4,0.6,1,1,0.5))
fobj<-f_direction(fobj = fobj, text = "A better    B better",
	line = 1.6)
plotfobj(fobj)

f_points

Description

Modify points in forest (f) items of a forest plot object (fobj). Passed to points.

Usage

f_points(fobj, item = NULL, ...)

Arguments

fobj

a forest plot object of class 'fobj'

item

item to be modified, either a number or the name of the column in fobj$dat. If NULL (the default), all items of type 'f' are affected

...

options to be passed to points

Value

a forest plot object of class 'fobj'

Examples

fobj<-genfobj(layout = c("t","t","t","t","t","t","f","t"),
	dat = forplotdata, lwidths = c(0.8,0.4,0.6,0.4,0.6,1,1,0.5))
fobj<-f_points(fobj = fobj, pch = 16, cex = 1.5)
plotfobj(fobj)

f_refline

Description

Add and modify the reference line in forest (f) items of a forest plot object (fobj). Passed to lines.

Usage

f_refline(fobj, item = NULL, ...)

Arguments

fobj

a forest plot object of class 'fobj'

item

item to be modified, either a number or the name of the column in fobj$dat. If NULL (the default), all items of type 'f' are affected

...

options to be passed to lines

Value

a forest plot object of class 'fobj'

Examples

fobj<-genfobj(layout = c("t","t","t","t","t","t","f","t"),
	dat = forplotdata, lwidths = c(0.8,0.4,0.6,0.4,0.6,1,1,0.5))
fobj<-f_refline(fobj = fobj, x = c(0,0))
plotfobj(fobj)

Demonstration data set with continous outcomes

Description

Simulated summary data from 10 outcomes to be presented in a forest plot. Each row represents the summary of one outcome variable for two treatment groups with columns for the variable name (vlabel), number of observations and mean (sd) for each group (n1, n2, n3 and n4), the difference between groups with 95% CI as a formatted text column (beta_format) and as numeric variables to draw the forest (beta, beta_lci, beta_uci), and a p-value (p1)

Usage

forplotdata

Format

A data frame with 10 columns: vlabel, n1, n2, n3, n4, beta_format, beta, beta_lci, beta_uci and p1.


Raw data for continous outcomes

Description

Simulated raw data from 10 outcomes in a long format. Includes columns for the outcome values (value), the outcome variable (variable) as factor, and the treatment arm (arm) as a factor.

Usage

forplotdata_bp

Format

A data frame with 3 columns: value, variable and arm.


Demonstration data set with binary outcomes

Description

Simulated summary data from 10 outcomes to be presented in a forest plot. Each row represents the summary of one outcome variable for two treatment groups with columns for the variable name (vlabel), number of observations and n (%) for each group (n1, n2, n3 and n4), the crude proporion in each group (prop1, prop2) the risk difference between groups (beta), with 95% CI (beta_lci and beta_uci), the formatted risk differnce (beta_format) and a p-value (p1)

Usage

forplotdata_prop

Format

A data frame with 12 columns: vlabel, n1, n2, n3, n4, prop1, prop2, beta, beta_lci, beta_uci, beta_format and p1.


forest

Description

produces forest plots

Usage

fplot(
  dat,
  nrows = 1,
  ncols = NA,
  nns = NA,
  lheights = c(0.1, 1, 0.05),
  lwidths = NA,
  y.at = NA,
  font = NA,
  arrow = TRUE,
  arrow_length = 0.05,
  arrow_angle = 30,
  cap_length = 0,
  shift_label_col = 0,
  center_label_col = NA,
  shift_textbeta_col = 0,
  shift_ymax = 0,
  shift_ymin = 0,
  xlim = NA,
  xlab = NA,
  xlab_text = NA,
  plim = NA,
  plab = NA,
  plab_text = NA,
  xlab_cex = 0.6,
  xlab_line = 0,
  tck = -0.04,
  shift_xaxis = 0,
  xtitle = NA,
  lwd = 1,
  pcol = rgb(0.1, 0.1, 0.1, 0.2),
  lscale = FALSE,
  ps = NA,
  prps = NA,
  header = NA,
  ref = list(x = NA, extend = 0, lty = 2, col = "grey50", lwd = lwd),
  bottomline = NA,
  headline = NA,
  headline_pos = c(0, 1),
  sideline = FALSE,
  bpdat = NULL,
  bpopt = NA,
  bplim = NA,
  bplab = NA,
  bplab_text = NA,
  beta2 = NULL,
  xlab2 = NA,
  xlab_text2 = NA,
  xlim2 = NA,
  xtitle2 = NA,
  ...
)

Arguments

dat

data frame with variables called vlabel (labels), nx with x=1,2,... (num or chr columns with strings, e.g. number of observations or descriptives), beta, beta_lci, beta_uci (num columns with point estimates and confidence interval) optionally beta_format (num or chr column with formatted text to be printed along forest, generated from beta if not given) px with x=1,2,.. (num or chr columns with p-value(s)) propx with x=1,2,.. (num columns with proportion in the groups, generates simple scatter plot)

nrows

number of rows, setting for layout, default is 1

ncols

number of columns, setting for layout, typically derived from the data

nns

number of n columns, derived from data if not given

lheights

lheights: relative height of rows, length is typically 3 (header, data ,footer)

lwidths

lwidths: relative width of columns, length correpsonds to columns in forest plus 2 (left and right margin)

y.at

position of lines, 1:nrow(dat) by default

font

font for variable vlabel, length has to correspond to rows of dat, default is rep(1,nrow(dat))

arrow

logical, whether to use arrows if xlim does not include confidence interval limits

arrow_length

length of the edges of the arrow head (in inches)

arrow_angle

angle from the shaft of the arrow to the edge of the arrow head

cap_length

length of the confidence interval cap, 0 if none

shift_label_col

inset of labels in the first column, default is 0

center_label_col

centering of label in first column

shift_textbeta_col

inset of formatted effects (beta_format)

shift_ymax

down-shift of maximal y-value, smaller space to top

shift_ymin

up-shift of minimal y-value, smaller space to bottom

xlim

limits for x-axis in forest plots, derived from beta by default

xlab

position of labels for x-axis, derived from beta by default

xlab_text

text at the labels, derived from beta by default

plim

limits for x-axis of the proportion scatterplot (if any), c(0,1) by default

plab

position of labels for x-axis of the proportion scatterplot (if any), pretty(plim) by default

plab_text

text at the labels, 100*plab by default (percentages from proportion)

xlab_cex

size of x-axis labels (not title)

xlab_line

position of x-axis label (not title)

tck

x-axis tick length

shift_xaxis

shift position of x-axis

xtitle

x axis title and format, list with x (xpos), y (ypos), textr and textl (text at the right/left side), cex (text size)

lwd

line widths

pcol

color if symbols are used, not active

lscale

logicial, beta given on log scale, use exp to format beta, default is FALSE

ps

points for plot, list with pch, cex, and col

prps

points for proportion scatterplot, list with pch, cex, and col

header

for table, either a character vector or a list with any of x (vector with xpos), y (single y-position), text (character vector with labels), cex (text size), col (text colour), the list can be >1 to define more than one header line

ref

reference line, list with x (xposition), extend (extension on top), lty (line type), col (line color), lwd (line width)

bottomline

line at the bottom if not NA

headline

line for header if not NA, 1 for one line at the bottom, 2 for a line at the bottom and top

headline_pos

vector with position of lower and upper headline (if applicable), default c(0,1)

sideline

logical, whether to show lines at the side of the proportion scatterplot

bpdat

Optional data frame in a long format used to generate boxplots, must have columns value (the outcome value), variable (the outcome variable, safest as a factor to preserve the order in the plot), and arm (the treatment arm, safest as a factor to preserve the order in the plot)

bpopt

List of options to pass to boxplot, with elements "col" and "boxwex"

bplim

limits for x-axis of the boxplot (if any), c(min,max) by default

bplab

position of labels for x-axis of the boxplot (if any), pretty(bplim) by default

bplab_text

text at the labels, bplab by default

beta2

if not NULL a second forest is generated, needs variables beta2, beta_lci2 and beta_uci2 in dat

xlab2

see xlab for 2nd forest

xlab_text2

see xlab_text for 2nd forest

xlim2

see xlim for 2nd forest

xtitle2

see xtitle for 2nd forest

...

options passed to ff_ci for formatting the effects (if beta_format not given)

Details

required input is a data frame with a column called vlabel, beta, beta_lci and beta_uci.

Value

forest plot

Examples

data(forplotdata)
# Minimal example
 fplot(dat=forplotdata[,c("vlabel","beta","beta_lci","beta_uci")])

# Standard example
fplot(dat=forplotdata)

# Set widths and heights
lwidths<-c(0.05,0.5,0.2,0.8,0.2,0.8,1.2,1.2,0.5,0.05)
lheights<-c(0.14,1,0.08)
fplot(dat=forplotdata,lwidths=lwidths,lheights=lheights)


# Include header:
header<-c("","Group1\nN","Group1\nmean (sd)","Group2\nN","Group2\nmean (sd)",
"Mean difference\n95% CI","","P-value")
fplot(dat=forplotdata,lwidths=lwidths,lheights=lheights,header=header)

header<-list(list(y=0.7,
	text=c("Group1","Group2","Mean difference (95% CI)","P-value"),
	x=c(0.10,0.32,0.7,0.98)),
	list(y=0.3,text=c("N","mean (sd)","N","mean (sd)"),
	x=c(0.07,0.18,0.28,0.38)))
fplot(dat=forplotdata,header=header,lwidths=lwidths,lheights=lheights)

# Reference line and xlim
xtitle<-list(x=0.86,y=0.2,textl="Group 1 better  ",textr="  Group 2 better")
fplot(dat=forplotdata,header=header,lwidths=lwidths,lheights=lheights,
xtitle=xtitle,ref=list(x=0),xlim=c(-1,0.5))

# Shift x-axis, labels and title
xtitle<-list(x=0.86,y=0.6,textl="Group 1 better  ",textr="  Group 2 better")
fplot(dat=forplotdata,header=header,lwidths=lwidths,lheights=lheights,
ref=list(x=0,col=2,extend=2),
xtitle=xtitle,xlim=c(-1,0.5),shift_xaxis=0.3,xlab_line=-0.8)

# Lines at header and bottom
fplot(dat=forplotdata,header=header,lwidths=lwidths,lheights=lheights,
 	ref=list(x=0,col=2,extend=2),
	xtitle=xtitle,xlim=c(-1,0.5),shift_xaxis=0.3,xlab_line=-0.8,
	headline=2,bottomline=1)

#Add boxplot for continuous outcomes
lwidths<-c(0.05,0.5,0.3,0.6,0.3,0.6,0.8,1.2,1.2,0.5,0.05)
lheights<-c(0.08,1,0.04)

header<-list(list(y=0.7,
	text=c("Group1","Group2","Mean difference (95% CI)","P-value"),
	x=c(0.10,0.3,0.7,0.98),col=c("red","blue","black","black")),
	list(y=0.3,text=c("N","mean (sd)","N","mean (sd)"),
	x=c(0.07,0.15,0.25,0.32)))

xtitle<-list(x=0.88,y=0.3,textl="Group 1 better  ",textr="  Group 2 better")

fplot(dat=forplotdata,header=header,lwidths=lwidths,lheights=lheights,
  	ref=list(x=0,col=2,extend=2),
	xtitle=xtitle,xlim=c(-1,0.5),shift_ymin=0.5, shift_xaxis=0.5,xlab_line=-1,
	headline=2,bottomline=1,
	bpdat=forplotdata_bp)

#Add a scatterplot for proportions
data(forplotdata)
lwidths<-c(0.05,0.5,0.2,0.6,0.2,0.6,1.0,1.2,1.0,0.5,0.05)
lheights<-c(0.08,1,0.04)
header<-list(list(y=0.7,
	text=c("Group1","Group2","Proportions (%)","Risk difference in %","P-value"),
	x=c(0.10,0.25,0.45,0.7,0.98)),
	list(y=0.3,text=c("N","n (%)","N","n (%)","(1: red, 2: blue)", "(95% CI)"),
	x=c(0.07,0.15,0.22,0.30,0.45,0.7)))
 xtitle<-list(x=0.83,y=0.4,textl="Group 1 better  ",textr="  Group 2 better")

prps<-list(list(pch=16,cex=1.5,col=rgb(1,0,0,0.5)),
            list(pch=16,cex=1.5,col=rgb(0,0,1,0.5)))

fplot(dat=forplotdata_prop,lwidths=lwidths,lheights=lheights,
	header=header,
	prps=prps,
	xtitle=xtitle,ref=list(x=0),
	xlab_line=-1.0,xlab=c(-0.2,0,0.2,0.4), xlab_text=c(-20,0,20,40),
	headline=2,bottomline=1,sideline=TRUE,
	shift_ymin=0.5, shift_xaxis=0.5)

genfobj

Description

First step to generate a forest plot. Generates a fobj that can be plotted.

Usage

genfobj(
  layout,
  dat,
  obs = NULL,
  lwidths = NA,
  lheights = NA,
  y.at = NA,
  ylim = NA
)

Arguments

layout

layout of the plot, character vector with 't' (text), 'f' (forest), 's' (strip), or 'b' (boxplot).

dat

data frame that should be plotted

obs

Optional data frame with the observations for the boxplot. Required of layout includes 'b'.

lwidths

Optional numeric vector with the relative widths of the columns. Must have the same length as layout.

lheights

Optional numeric vectotr of length 3 with the relative heights of header, main panel and footer.

y.at

Optional numeric vector with the position of the rows. Usually not required.

ylim

Optional limits of the rows. Usually not required.

Value

a forest plot object of class 'fobj'

Examples

fobj<-genfobj(layout = c("t","t","t","t","t","t","f","t"),
	dat = forplotdata,
lwidths = c(0.8,0.4,0.6,0.4,0.6,1,1,0.5))
plotfobj(fobj)

gridlines

Description

Add or modify horizontal lines to a forest plot object (fobj). Passed to abline.

Usage

gridlines(fobj, gridnr = NULL, ...)

Arguments

fobj

a forest plot object of class 'fobj'

gridnr

grid line to be modified. If NULL (the default), all grid lines are affected

...

options to be passed to Passed to abline.

Value

a forest plot object of class 'fobj'

Examples

fobj<-genfobj(layout = c("t","t","t","t","t","t","f","t"),
	dat = forplotdata,
lwidths = c(0.8,0.4,0.6,0.4,0.6,1,1,0.5))
fobj<-gridlines(fobj = fobj)
plotfobj(fobj)

plotfobj

Description

plotfobj

Usage

plotfobj(fobj)

Arguments

fobj

a forest plot object

Value

a plot

Examples

fobj<-genfobj(layout = c("t","t","t","t","t","t","f","t"), 
	dat = forplotdata,
lwidths = c(0.8,0.4,0.6,0.4,0.6,1,1,0.5))
plotfobj(fobj)

s_axis

Description

Modify axis of stripe (s) items of a forest plot object (fobj). Passed to axis.

Usage

s_axis(fobj, item = NULL, ...)

Arguments

fobj

a forest plot object of class 'fobj'

item

item to be modified, either a number or the name of the column in fobj$dat. If NULL (the default), all items of type 's' are affected

...

options to be passed to axis

Value

a forest plot object of class 'fobj'

Examples

fobj<-genfobj(layout = c("t","t","t","t","t","s2","t","f","t"),
dat = forplotdata_prop,
lwidths = c(0.6,0.4,0.6,0.4,0.6,1.0,1.2,1,0.5))
fobj<-s_axis(fobj=fobj, xlim = c(0,1),
 at = seq(0,1,by=0.25), labels = seq(0,100,by=25))
plotfobj(fobj)

s_borders

Description

Add and modify borders of a sripe (s) item of a forest plot object (fobj). Passed to abline.

Usage

s_borders(fobj, item = NULL, bordernr = NULL, ...)

Arguments

fobj

a forest plot object of class 'fobj'

item

item to be modified, either a number or the name of the column in fobj$dat. If NULL (the default), all items of type 's' are affected

bordernr

border to be modified, 1 (left) or 2 (right). If NULL (the default), all borders are affected.

...

options to be passed to abline

Value

a forest plot object of class 'fobj'

Examples

fobj<-genfobj(layout = c("t","t","t","t","t","s2","t","f","t"),
dat = forplotdata_prop,
lwidths = c(0.6,0.4,0.6,0.4,0.6,1.0,1.2,1,0.5))
fobj<-s_borders(fobj=fobj)
plotfobj(fobj)

s_hline

Description

Modify the hiorzontal line in stripe (s) items of forest plot object (fobj). Passed to abline.

Usage

s_hline(fobj, item = NULL, ...)

Arguments

fobj

a forest plot object of class 'fobj'

item

item to be modified, either a number or the name of the column in fobj$dat. If NULL (the default), all items of type 's' are affected

...

options to be passed to abline

Value

a forest plot object of class 'fobj'

Examples

fobj<-genfobj(layout = c("t","t","t","t","t","s2","t","f","t"),
dat = forplotdata_prop,
lwidths = c(0.6,0.4,0.6,0.4,0.6,1.0,1.2,1,0.5))
fobj<-s_hline(fobj=fobj, lty = 4)
plotfobj(fobj)

s_points

Description

Modify points in stripe (s) items of a forest plot object (fobj). Passed to points.

Usage

s_points(fobj, item = NULL, pointnr = NULL, ...)

Arguments

fobj

a forest plot object of class 'fobj'

item

item to be modified, either a number or the name of the column in fobj$dat. If NULL (the default), all items of type 's' are affected

pointnr

points to be modified. If NULL (the default), all points are affected.

...

options to be passed to points

Value

a forest plot object of class 'fobj'

Examples

fobj<-genfobj(layout = c("t","t","t","t","t","s2","t","f","t"),
dat = forplotdata_prop,
lwidths = c(0.6,0.4,0.6,0.4,0.6,1.0,1.2,1,0.5))
fobj<-s_points(fobj=fobj, pch = 16, cex=1.5)
fobj<-s_points(fobj=fobj, pointnr = 1, col = "red")
fobj<-s_points(fobj=fobj, pointnr = 2, col = "blue")
plotfobj(fobj)

stripes

Description

Add or modify stripes in a forest plot object (fobj). Passed to rect.

Usage

stripes(fobj, ...)

Arguments

fobj

a forest plot object of class 'fobj'

...

options to be passed to rect

Value

a forest plot object of class 'fobj'

Examples

fobj<-genfobj(layout = c("t","t","t","t","t","t","f","t"),
	dat = forplotdata,
lwidths = c(0.8,0.4,0.6,0.4,0.6,1,1,0.5))
fobj<-stripes(fobj = fobj)
plotfobj(fobj)

t_options

Description

Modify text (t) items of a forest plot object (fobj). Passed to text.

Usage

t_options(fobj, item = NULL, ...)

Arguments

fobj

a forest plot object of class fobj

item

item to be modified, either a number or the name of the column in fobj$dat. If NULL (the default), all items of type 't' are affected

...

options to be passed to text

Value

a forest plot object of class 'fobj'

Examples

fobj<-genfobj(layout = c("t","t","t","t","t","t","f","t"),
	dat = forplotdata, lwidths = c(0.8,0.4,0.6,0.4,0.6,1,1,0.5))
fobj<-t_options(fobj = fobj, item = 1, col = 2)
plotfobj(fobj)