Make a shape plot with ggplot2

make_shape_plot(
  data,
  col.x = "x",
  col.estimate = "estimate",
  col.stderr = "stderr",
  col.lci = NULL,
  col.uci = NULL,
  col.n = NULL,
  exponentiate = FALSE,
  logscale = exponentiate,
  scalepoints = FALSE,
  minse = NA,
  pointsize = 3,
  col.group = NULL,
  shape = NULL,
  plotcolour = "black",
  colour = NULL,
  cicolour = colour,
  fill = NULL,
  ciunder = NULL,
  lines = FALSE,
  xlims,
  ylims,
  gap = c(0.025, 0.025),
  ext = c(0.025, 0.025),
  ratio = 1.5,
  base_size = 11,
  base_line_size = base_size/22,
  stroke = base_size/22,
  xbreaks = NULL,
  ybreaks = NULL,
  xlab = "Risk factor",
  ylab = "Estimate (95% CI)",
  legend.name = "",
  legend.position = "top",
  title = "Figure",
  printplot = TRUE,
  showcode = TRUE,
  addcode = NULL,
  addaes = NULL,
  addarg = NULL,
  envir = NULL
)

Arguments

data

The data frame containing estimates to be plotted.

col.x

Name of column that provides the x-axis value (e.g. exposure, risk factor, dependent variable). (Default: "x")

col.estimate

Name of column that provides point estimates. (Default: "estimate")

col.stderr

Name of column that provides standard errors. (Default: "stderr")

col.lci

Name of column that provides lower limit of confidence intervals.

col.uci

Name of column that provides upper limit of confidence intervals.

col.n

Name of column that provides number to be plotted below CIs.

exponentiate

Exponentiate estimates (and CIs) before plotting, use log scale on the axis. (Default: FALSE)

logscale

Use log scale for vertical axis. (Default: exponentiate)

scalepoints

Should the points be scaled by inverse of the standard error? (Default: FALSE)

minse

Minimum standard error to use when scaling point size. (Default will use minimum in the data.)

pointsize

The (largest) size of box to use for plotting point estimates. (Default: 3)

col.group

Name of column that groups the estimates. (Default: NULL)

shape

Shape of points. An integer, or name of a column of integers. (Default will use shape 22 - squares with fill.)

plotcolour

Colour for non-data aspects of the plot. (Default: "black")

colour

Colour of points. Name of a colour, or name of a column of colour names. (Default will use plotcolour)

cicolour

Colour of CI lines. Colour of CI lines. Name of a colour, or name of a column of colour names. (Default will use plotcolour)

fill

Fill colour of points. Fill colour of points. Name of a colour, or name of a column of colour names. (Default will use plotcolour)

ciunder

Plot CI lines before points. A logical value, or name of a column of logical values. (Default will plot CI lines after points.)

lines

Plot lines (linear fit through estimates, weighted by inverse variance). (Default: FALSE)

xlims

A numeric vector of length two. The limits of the x-axis.

ylims

A numeric vector of length two. The limits of the y-axis.

gap

A numeric vector of length two. The gap between plotting area and axis to the left and bottom of the plot, as a proportion of the x-axis length. (Default: c(0.025, 0.025))

ext

A numeric vector of length two. The extensions to add to the right and top of the plot, as a proportion of the x-axis length. (Default: c(0.025, 0.025))

ratio

The ratio (y-axis:x-axis) to use for the plot. (Default: 1.5)

base_size

base font size, given in pts.

base_line_size

base size for line elements

stroke

Size of outline of shapes. (Default: base_size/22)

xbreaks

Breaks for the x axis. Passed to ggplots::scale_x_continuous. (Default: NULL)

ybreaks

Breaks for the y axis. Passed to ggplots::scale_y_continuous. (Default: NULL)

xlab

Label for x-axis. (Default: "Risk factor")

ylab

Label for y-axis. (Default: "Estimate (95% CI)")

legend.name

The name of the colour scale/legend for groups. (Default: "")

legend.position

Position of the legend for groups ("none", "left", "right", "bottom", "top", or two-element numeric vector). (Default: "top")

title

Plot title. (Default: "Figure")

printplot

Print the plot. (Default: TRUE)

showcode

Show the ggplot2 code to generate the plot in RStudio 'Viewer' pane. (Default: TRUE)

addcode

A character vector of code to add to the generated code. The first element should be a regular expression. The remaining elements are added to the generated code just before the first match of a line (trimmed of whitespace) with the regular expression. (Default: NULL)

addaes

Specify additional aesthetics for some ggplot layers.

addarg

Specify additional arguments for some ggplot layers.

envir

Environment in which to evaluate the plot code. May be useful when calling this function inside another function.

Value

A list:

plot

the plot

code

ggplot2 code to generate the plot