Title: | Create CKB Plots |
---|---|
Description: | ckbplotr provides functions to help create and style plots in R. It is being developed by, and primarily for, China Kadoorie Biobank researchers. |
Authors: | Neil Wright [aut, cre] |
Maintainer: | Neil Wright <[email protected]> |
License: | GPL (>= 3) |
Version: | 0.11.3 |
Built: | 2025-01-01 06:38:47 UTC |
Source: | https://github.com/neilstats/ckbplotr |
Make a ggplot into CKB style
ckb_style( xlims = NULL, ylims = NULL, gap = c(0.025, 0.025), ext = c(0, 0), ratio = 1.5, width = NULL, height = NULL, base_size = 11, base_line_size = base_size/22, colour = "black", axis.title.margin = 1, plot.margin = margin(0.5, 1.5, 0.5, 0.5, "lines"), axes = "both" )
ckb_style( xlims = NULL, ylims = NULL, gap = c(0.025, 0.025), ext = c(0, 0), ratio = 1.5, width = NULL, height = NULL, base_size = 11, base_line_size = base_size/22, colour = "black", axis.title.margin = 1, plot.margin = margin(0.5, 1.5, 0.5, 0.5, "lines"), axes = "both" )
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, 0)) |
ratio |
The ratio (y-axis:x-axis) to use for the plot. Ignored if both width and height are set. (Default: 1.5) |
width |
A |
height |
A |
base_size |
base font size, given in pts. |
base_line_size |
base size for line elements |
colour |
Colour for non-data aspects of the plot. (Default: "black") |
axis.title.margin |
Margin between axis titles and plot. (Default: 1) |
plot.margin |
Margin around entire plot (Default: margin(0.5, 0, 0.5, 0, "lines")) |
axes |
Choice of axis lines to add to the plot, one of "both", "x" or "y". (Default: "both") |
Creates a forest plot with ggplot
forest_plot( panels, row.labels = NULL, row.labels.levels = NULL, rows = NULL, row.labels.heading = NULL, row.labels.space = c(0, 1, 0, 0), exponentiate = TRUE, logscale = exponentiate, panel.names = NULL, panel.headings = NULL, panel.headings.align = c("panel", "plot"), col.key = "key", col.estimate = c("estimate", "est", "beta", "loghr"), col.stderr = c("stderr", "std.err", "se"), col.lci = NULL, col.uci = NULL, col.left = NULL, col.right = NULL, col.right.parse = FALSE, col.left.heading = "", col.right.heading = as.list(xlab), col.left.pos = NULL, col.right.pos = NULL, col.left.hjust = 1, col.right.hjust = 0, col.left.gap = c("I", "W"), col.right.gap = c("I", "W"), col.heading.space = 0, col.heading.rule = FALSE, estcolumn = TRUE, col.keep = NULL, ci.delim = ", ", digits = 2, title = "", xlab = "HR (95% CI)", xlim = NULL, xticks = NULL, nullval = NULL, col.diamond = NULL, diamond = NULL, col.bold = NULL, bold.labels = NULL, scalepoints = FALSE, minse = NULL, pointsize = 3, shape = 15, plotcolour = "black", colour = plotcolour, cicolour = colour, fill = colour, ciunder = NULL, addtext = NULL, bottom.space = 0.7, left.space = NULL, right.space = NULL, mid.space = unit(5, "mm"), plot.margin = margin(2, 8, 2, 8, "mm"), panel.width = NULL, panel.height = NULL, base_size = 11, base_line_size = base_size/22, stroke = 0, diamonds.linewidth = base_line_size, quiet = FALSE, printplot = !quiet, showcode = !quiet, data.function = NULL, addaes = NULL, addarg = NULL, add = NULL, envir = NULL, blankrows = NULL )
forest_plot( panels, row.labels = NULL, row.labels.levels = NULL, rows = NULL, row.labels.heading = NULL, row.labels.space = c(0, 1, 0, 0), exponentiate = TRUE, logscale = exponentiate, panel.names = NULL, panel.headings = NULL, panel.headings.align = c("panel", "plot"), col.key = "key", col.estimate = c("estimate", "est", "beta", "loghr"), col.stderr = c("stderr", "std.err", "se"), col.lci = NULL, col.uci = NULL, col.left = NULL, col.right = NULL, col.right.parse = FALSE, col.left.heading = "", col.right.heading = as.list(xlab), col.left.pos = NULL, col.right.pos = NULL, col.left.hjust = 1, col.right.hjust = 0, col.left.gap = c("I", "W"), col.right.gap = c("I", "W"), col.heading.space = 0, col.heading.rule = FALSE, estcolumn = TRUE, col.keep = NULL, ci.delim = ", ", digits = 2, title = "", xlab = "HR (95% CI)", xlim = NULL, xticks = NULL, nullval = NULL, col.diamond = NULL, diamond = NULL, col.bold = NULL, bold.labels = NULL, scalepoints = FALSE, minse = NULL, pointsize = 3, shape = 15, plotcolour = "black", colour = plotcolour, cicolour = colour, fill = colour, ciunder = NULL, addtext = NULL, bottom.space = 0.7, left.space = NULL, right.space = NULL, mid.space = unit(5, "mm"), plot.margin = margin(2, 8, 2, 8, "mm"), panel.width = NULL, panel.height = NULL, base_size = 11, base_line_size = base_size/22, stroke = 0, diamonds.linewidth = base_line_size, quiet = FALSE, printplot = !quiet, showcode = !quiet, data.function = NULL, addaes = NULL, addarg = NULL, add = NULL, envir = NULL, blankrows = NULL )
panels |
A list of data frames. These should include columns or point estimates, and standard errors or confidence interval limits. If you specify a row.labels data frame, then they must also all contain a key column with the same name (which can be specified by col.key). |
row.labels |
A data frame that contains the labels to be used for the rows of the plot. Use NA if a lower level heading is not required for a given row. |
row.labels.levels |
A character vector. The names of columns in row.labels to use as headings/subheadings/labels for labelling rows. |
rows |
If set, then only rows matching these labels (at the first level) will be included. |
row.labels.heading |
Title to be placed above row labels. |
row.labels.space |
A numeric vector specifying the space after a row label heading, at the end of a row label heading 'section'. (Default: c(0, 1, 0, 0)) |
exponentiate |
Exponentiate estimates (and CIs) before plotting. (Default: TRUE) |
logscale |
Use log scale on the axis, and add a line at null effect. (Default: exponentiate) |
panel.names |
A character vector. The names to be used for each forest plot panel. If none provided, then they will be numbered 1, 2, 3 ... |
panel.headings |
Titles to be placed above each forest plot. |
panel.headings.align |
Panel headings are by default centred over the plotting area ("panel"). Set to "plot" to centre over plotting area and text columns. |
col.key |
Name of column that links the results given in each data frame provided in panels and the labels given in row.labels. If row.labels data frame is not given, then this column will be used as row labels. (Default: "key") |
col.estimate , col.stderr , col.lci , col.uci
|
Names of columns for: point estimates, standard errors, lower and upper limits of confidence intervals. |
col.left , col.right
|
Names of columns to be printed to the left/right of the plot. |
col.right.parse |
A logical vector, the same length as col.right (+ 1 if estcolumn = TRUE). Should the contents of the columns be parsed into expressions. (Default: FALSE) |
col.left.heading , col.right.heading
|
Headings for columns. |
col.left.pos , col.right.pos
|
A unit vector to position col.left/col.right columns. |
col.left.hjust , col.right.hjust
|
A numeric vector. The horizontal justification of col.left/col.right columns. (Default: 1) |
col.left.gap , col.right.gap
|
A character vector of length two. The two characters control the gaps between the first text column and the panel, and successive text columns. (Default: c("I", "W")) |
col.heading.space |
Position of the titles given by col.left.heading and col.right.heading. Increase to move them up. (Default: 0) |
col.heading.rule |
Include a horizontal rule below column headings? (Default: FALSE) |
estcolumn |
Include column of estimates and confidence intervals to the right of each plot. (Default: TRUE) |
col.keep |
Names of additional columns to be kept in returned data frame. |
ci.delim |
Character string to separate lower and upper limits of confidence interval. (Default: ", ") |
digits |
Number of digits after decimal point to show for estimates and confidence intervals. (Default: 2) |
title |
Title to appear at the top of the plot. |
xlab |
Label to appear below the x-axis. (Default: "HR (95% CI)") |
xlim |
A numeric vector. The limits of the x axis. |
xticks |
A numeric vector. The tick points of the x axis. |
nullval |
Add a vertical reference line at this value. (If logscale == TRUE then by default it will be added at 1, but use NA not to plot this line.) |
col.diamond |
Plot estimates and CIs as diamonds. Name of a column of logical values. |
diamond |
Alternative to col.diamond. A character vectors identify the rows (using the key values) for which the estimate and CI should be plotted using a diamond. |
col.bold |
Plot text as bold. Name of a column of logical values. |
bold.labels |
A character vector identifying row labels (using key values) which should additionally be bold. (Default: NULL) |
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) |
shape |
Shape of points. An integer, or name of a column of integers. (Default: 15 (square)) |
plotcolour |
Colour for all parts 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 colour.) |
fill |
Fill colour of points. Name of a colour, or name of a column of colour names. (Default will use colour.) |
ciunder |
Plot CI lines before points. A logical value, or name of a column of logical values. (Default will plot CI lines after points.) |
addtext |
A list of data frames. List must be the same length as panels. Data frames should contain a column with the name specified in col.key, and one or more of:
The character strings, heterogeneity test, and trend test results will be plotted in the column of estimates and CIs, below the row with the key given in the col.key column. |
bottom.space |
Space between bottom row and axis. (Default: 0.7) |
left.space , right.space , mid.space
|
Space to the left/right/between panels. (Default mid.space: unit(5, "mm")) |
plot.margin |
Plot margin, given as margin(top, right, bottom, left, units). (Default: margin(8, 8, 8, 8, "mm")) |
panel.width , panel.height
|
Set width/height of panels. A grid::unit object, if a numeric is given assumed to be in mm. If panel.width is used, will alsovapply different formatting to narrow CIs. |
base_size |
base font size, given in pts. |
base_line_size |
base size for line elements |
stroke |
Size of outline of shapes. (Default: 0) |
diamonds.linewidth |
Line width for diamonds. (Default: base_line_size) |
quiet |
Set to TRUE to not print the plot nor show generated code in the RStudio 'Viewer' pane. (Default: FALSE) |
printplot |
Print the plot. (Default: !quiet) |
showcode |
Show the ggplot2 code to generate the plot in RStudio 'Viewer' pane. (Default: !quiet) |
data.function |
Name of a function to apply to data frame before plotting. |
addaes , addarg , add
|
Methods for customising the plot. See documentation for details. |
envir |
Environment in which to evaluate the plot code. May be useful when calling this function inside another function. |
blankrows |
DEPRECATED |
The function returns the plot and ggplot2 code to create the plot. In RStudio, the ggplot2 code will be shown in the viewer.
A list:
the plot
ggplot2 code to generate the plot
This geom adds a fixed horizontal and/or vertical move to ggplot2::geom_text()
geom_text_move( mapping = NULL, data = NULL, stat = "identity", position = "identity", ..., parse = FALSE, nudge_x = 0, nudge_y = 0, move_x = unit(0, "pt"), move_y = unit(0, "pt"), check_overlap = FALSE, na.rm = FALSE, show.legend = NA, inherit.aes = TRUE )
geom_text_move( mapping = NULL, data = NULL, stat = "identity", position = "identity", ..., parse = FALSE, nudge_x = 0, nudge_y = 0, move_x = unit(0, "pt"), move_y = unit(0, "pt"), check_overlap = FALSE, na.rm = FALSE, show.legend = NA, inherit.aes = TRUE )
mapping |
Set of aesthetic mappings created by |
data |
The data to be displayed in this layer. There are three options: If A A |
stat |
The statistical transformation to use on the data for this
layer, either as a |
position |
Position adjustment, either as a string, or the result of
a call to a position adjustment function. Cannot be jointly specified with
|
... |
Other arguments passed on to |
parse |
If |
nudge_x , nudge_y
|
Horizontal and vertical adjustment to nudge labels by.
Useful for offsetting text from points, particularly on discrete scales.
Cannot be jointly specified with |
move_x |
Unit value to move text horizontally (Default: unit(0, "pt")) |
move_y |
Unit value to move text vertically (Default: unit(0, "pt")) |
check_overlap |
If |
na.rm |
If |
show.legend |
logical. Should this layer be included in the legends?
|
inherit.aes |
If |
geom_text_move()
understands the same aesthetics as ggplot2::geom_text()
This function saves a ggplot2 plot to a temporary PNG file and then embeds it in an HTML page, which is opened in the Viewer pane.
ggpreview(...)
ggpreview(...)
... |
Arguments passed to |
The plot is saved using ggsave
with the png
device, regardless of what is specified in
the call, so any arguments not used by ggsave
or png
are ignored.
Make a ggplot into CKB style
plot_like_ckb(plot, ...)
plot_like_ckb(plot, ...)
plot |
A ggplot2 plot |
... |
Arguments passed to ckb_style() |
A ggplot2 plot.
Prepare figure for saving
prepare_figure( figure, title = "", title.pos = grid::unit.c(unit(1.27/2, "cm"), unit(1, "npc") - unit(1.27/2, "cm")), title.just = c(0, 1), title.gpar = list(fontsize = 12, fontface = "bold"), footer = "", footer.pos = grid::unit.c(unit(1.27/2, "cm"), unit(1.27/3, "cm")), footer.just = c(0, 0), footer.gpar = list(fontsize = 9), margin = unit(c(2.27, 1.27, 1.27, 1.27), units = "cm"), size = NULL, valign = 0.5, halign = 0.5, pagesize = c("A4", "A5"), landscape = FALSE, pagedim = NULL )
prepare_figure( figure, title = "", title.pos = grid::unit.c(unit(1.27/2, "cm"), unit(1, "npc") - unit(1.27/2, "cm")), title.just = c(0, 1), title.gpar = list(fontsize = 12, fontface = "bold"), footer = "", footer.pos = grid::unit.c(unit(1.27/2, "cm"), unit(1.27/3, "cm")), footer.just = c(0, 0), footer.gpar = list(fontsize = 9), margin = unit(c(2.27, 1.27, 1.27, 1.27), units = "cm"), size = NULL, valign = 0.5, halign = 0.5, pagesize = c("A4", "A5"), landscape = FALSE, pagedim = NULL )
figure |
Plot (or graphical object). |
title |
Title to be added to the page. (Default: "") |
title.pos |
Position of the title text. Default is 1/4 inch from top left of page. (Default: unit.c(unit(1.27/2, "cm"), unit(1, "npc") - unit(1.27/2, "cm"))) |
title.just |
Justification of the title text. (Default: c(0, 1)) |
title.gpar |
Graphical parameters for title. (Default: list(fontsize = 12, fontface = "bold")) |
footer |
Footer to be added to the page. (Default: "") |
footer.pos |
Position of the footer text. Default is 1/6 inch from bottom and 1/4 inch from left of page. (Default: unit.c(unit(1.27/2, "cm"), unit(1.27/3, "cm"))) |
footer.just |
Justification of the footer text. (Default: c(0, 0)) |
footer.gpar |
Graphical parameters for footer. (Default: list(fontsize = 9)) |
margin |
Margin to be placed around the plot. Default is 2.27cm top, 1.27cm (1/2 inch) other sides. (Default: unit(c(2.27, 1.27, 1.27, 1.27), units = "cm")) |
size |
A unit vector of length two (width, height). Size of plot (a width/height larger than page weight/height minus margins will be ignored), centred within margins. By default, plot will fill the space within margins. |
valign |
If size is set, where to place figure within margins. 1 = top, 0.5 = middle, 0 = bottom. (Default: 0.5) |
halign |
If size is set, where to place figure within margins. 1 = right, 0.5 = middle, 0 = left (Default: 0.5) |
pagesize |
Page size of output: "A4" or "A5". (Default: "A4") |
landscape |
Landscape page orientation? (Default: False) |
pagedim |
Dimensions (width, height) of output. Overrides pagesize and landscape arguments if used. |
Output plots as files
save_figure( figure, filename, cropped = NULL, args = NULL, args_cropped = NULL, preview = FALSE, ... )
save_figure( figure, filename, cropped = NULL, args = NULL, args_cropped = NULL, preview = FALSE, ... )
figure |
Plot (or graphical object). |
filename |
Name of file to create. |
cropped |
Name of second output file of the figure without margins or title. |
args |
List of arguments passed to |
args_cropped |
List of arguments passed to |
preview |
Preview the output in the RStudio Viewer pane. (Default: False) |
... |
Other arguments passed to prepare_figure. |
Make a shape plot with ggplot2
shape_plot( data, col.x = "x", col.estimate = c("estimate", "est", "beta", "loghr"), col.stderr = c("stderr", "std.err", "se"), col.lci = NULL, col.uci = NULL, col.n = NULL, exponentiate = FALSE, logscale = exponentiate, scalepoints = FALSE, digits = 2, minse = NA, pointsize = 3, col.group = NULL, shape = 15, plotcolour = "black", colour = plotcolour, cicolour = colour, fill = colour, ciunder = NULL, lines = FALSE, xlims, ylims, height = NULL, width = NULL, 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, axis.title.margin = 1, xbreaks = NULL, ybreaks = NULL, xlab = "Risk factor", ylab = "Estimate (95% CI)", legend.name = "", legend.position = "top", title = NULL, quiet = FALSE, printplot = !quiet, showcode = !quiet, addaes = NULL, addarg = NULL, add = NULL, envir = NULL )
shape_plot( data, col.x = "x", col.estimate = c("estimate", "est", "beta", "loghr"), col.stderr = c("stderr", "std.err", "se"), col.lci = NULL, col.uci = NULL, col.n = NULL, exponentiate = FALSE, logscale = exponentiate, scalepoints = FALSE, digits = 2, minse = NA, pointsize = 3, col.group = NULL, shape = 15, plotcolour = "black", colour = plotcolour, cicolour = colour, fill = colour, ciunder = NULL, lines = FALSE, xlims, ylims, height = NULL, width = NULL, 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, axis.title.margin = 1, xbreaks = NULL, ybreaks = NULL, xlab = "Risk factor", ylab = "Estimate (95% CI)", legend.name = "", legend.position = "top", title = NULL, quiet = FALSE, printplot = !quiet, showcode = !quiet, addaes = NULL, addarg = NULL, add = NULL, envir = NULL )
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) |
digits |
Number of digits to use in text of estimates. |
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: 15) |
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. |
height |
Panel height to use and apply different formatting to short CIs. A grid::unit() object, or if numeric is assumed to be in mm. |
width |
Panel width.A grid::unit() object, or if numeric is assumed to be in mm. |
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) |
axis.title.margin |
Margin between axis titles and plot. (Default: 1) |
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: NULL) |
quiet |
Set to TRUE to not print the plot nor show generated code in the RStudio 'Viewer' pane. (Default: FALSE) |
printplot |
Print the plot. (Default: !quiet) |
showcode |
Show the ggplot2 code to generate the plot in RStudio 'Viewer' pane. (Default: !quiet) |
addaes , addarg , add
|
Methods for customising the plot. See documentation for details. |
envir |
Environment in which to evaluate the plot code. May be useful when calling this function inside another function. |
A list:
the plot
ggplot2 code to generate the plot
Based on theme_bw
theme_ckb( base_size = 11, base_line_size = base_size/22, colour = "black", axis.title.margin = 1, plot.margin = margin(0.5, 1.5, 0.5, 0.5, "lines") )
theme_ckb( base_size = 11, base_line_size = base_size/22, colour = "black", axis.title.margin = 1, plot.margin = margin(0.5, 1.5, 0.5, 0.5, "lines") )
base_size |
base font size, given in pts. |
base_line_size |
base size for line elements |
colour |
Colour for non-data aspects of the plot. (Default: "black") |
axis.title.margin |
Margin between axis titles and plot. (Default: 1) |
plot.margin |
Margin around entire plot (Default: margin(0.5, 0, 0.5, 0, "lines")) |