| Type: | Package |
| Title: | BiMaU Miscellaneous |
| Version: | 0.1.0 |
| Maintainer: | Anna Felip-Badia <annafelipibadia@gmail.com> |
| Description: | Contains a function to plot publication-ready survival curves with the Kaplan-Meier method (1958) <doi:10.2307/2281868> and a function to format p-values, which are useful for repetitive analyses. BiMaU stands for the Biostatistics and Mathematics Research Unit at the Sant Joan de Déu - Pediatric Cancer Center Barcelona https://github.com/BiMaU-PCCB. |
| License: | GPL-2 | GPL-3 [expanded from: GPL (≥ 2)] |
| Depends: | R (≥ 4.1.0) |
| Encoding: | UTF-8 |
| Imports: | exams, survival (≥ 3.8-6), latex2exp, plotfunctions, graphics, utils |
| Config/roxygen2/version: | 8.0.0 |
| NeedsCompilation: | no |
| Packaged: | 2026-07-24 09:28:39 UTC; afelipb |
| Author: | Anna Felip-Badia |
| Repository: | CRAN |
| Date/Publication: | 2026-08-04 09:50:12 UTC |
BiMaU Miscellaneous
Description
This package contains useful data analysis tools, such as a function to plot survival curves, a function to format p-values, and a function (work in progress) to automatically select the test to apply when assessing differences in continuous variables between groups.
Functions
Author(s)
Maintainer: Anna Felip-Badia annafelipibadia@gmail.com (ORCID)
Authors:
Anna Felip-Badia annafelipibadia@gmail.com (ORCID)
Aleix Martín-Moral (ORCID)
Sara Perez-Jaume (ORCID)
Format p-values for display
Description
Convert p-values to a compact textual representation. The function prints decimals up to the first non-zero decimal and the following one, applying standard rounding.
Usage
show.p(p, add.p = FALSE)
Arguments
p |
a value or vector of the p-value(s) to be shown, where |
add.p |
logical indicating whether the text p= (or p<, if it applies) should be added before the p-value. |
Details
The number of decimals shown depends on the magnitude of the p-value so that two meaningful digits are displayed after the leading zeros.
Specifically:
For
p \ge 0.1, two decimal places are shown.For
0.01 \le p < 0.1, three decimal places are shown.For
0.001 \le p < 0.01, four decimal places are shown.For
0.0001 \le p < 0.001, five decimal places are shown.For
p < 0.0001, the string"<0.0001"is returned.
This formatting ensures that the first non-zero decimal of the p-value and the following digit are displayed,
while avoiding an excessive number of leading zeros.
If add.p = TRUE, the prefix "*p*=" (or "*p*<" when applicable) is added to the formatted value.
This will ensure the p appears in cursive when calling the function in line on R markdown documents.
Value
A character vector with the formatted p-values.
Examples
show.p(0.00785)
show.p(c(0.03042, 0.1579, 0.0000025))
show.p(0.00785, add.p = TRUE)
show.p(c(0.03042, 0.1579, 0.0000025), add.p = TRUE)
Plot survival curves
Description
This function produces an elegant survival curve plot.
It can automatically append the number-at-risk table and display the log-tank test p-value (obtained from a call to survdiff).
Highly customizable, it offers numerous arguments to easily modify the plot's appearance.
Usage
survival.plot(data, time, status, strata = 1,
legend = TRUE, legend.pos = "bottomleft", legend.labs = NULL,
legend.title = NULL, legend.cex = 1,
events = FALSE, events.pos = NULL, events.text = NULL,
mark.time = TRUE, lwd = 3,
risk = TRUE, risk.text = TRUE, risk.labs = NULL, risk.cex = 1,
p.value = TRUE, p.value.pos = NULL,
units = "days", xlab = NULL, times = NULL,
yscale = 1, ylab = NULL,
main = "Survival curve", col = NULL, ...)
Arguments
data |
name of the dataset, of class |
time |
follow-up time column name. Can be specified as a character string (with quotes) or as an unquoted symbol. |
status |
status indicator column name. Can be specified as a character string (with quotes) or as an unquoted symbol. |
strata |
strata indicator column name. Can be specified as a character string (with quotes) or as an unquoted symbol. Up to four categories. Must be a factor. |
legend |
logical indicating whether a legend should be added to the plot. If |
legend.pos |
this argument can be a single keyword from the list in |
legend.labs |
a character vector of length |
legend.title |
title for the legend. If |
legend.cex |
legend's character expansion factor. A numerical value giving the amount by which the legend text should be magnified relative to the default.
If |
events |
logical indicating whether the number of events should be added to the text in the legend.
If |
events.pos |
2-dimensional numerical vector indicating the x-y coordinates where the number of events should be added to the plot.
If |
events.text |
a character indicating the text to display before the number of events in the plot.
If |
mark.time |
logical indicating whether the curves should be marked at each censoring time. |
lwd |
a number indicating the line width(s). Alternatively, when |
risk |
logical indicating whether the number-at-risk table should be appended to the plot. |
risk.text |
logical indicating whether the legend for the number-at-risk table should display text labels. When |
risk.labs |
|
risk.cex |
number-at-risk table's character expansion factor. A numerical value giving the amount by which the number-at-risk table text should be magnified relative to the default.
If |
p.value |
logical indicating whether the p-value obtained from a call to |
p.value.pos |
2-dimensional numerical vector indicating the x-y coordinates where the number of events should be added to the plot.
If |
units |
character indicating the time units. Options are: "days", "weeks", "months" or "years".
This argument will be used to create the default label and tick-marks for the x-axis. This argument is not case-sensitive and is passed to |
xlab |
character indicating a label for the x-axis. |
times |
numerical vector with the time points at which tick-marks are to be drawn in the x-axis.
|
yscale |
a numeric value used to multiply the labels on the y-axis. A value of 100, for instance, would be used to give a percent scale.
Notice only the labels are changed, not the actual plot coordinates. |
ylab |
character indicating a label for the y-axis. |
main |
overall title for the plot. |
col |
colour palette to be used. |
... |
other graphical parameters (to be passed to |
Value
A customized survival curve plot displaying the requested graphical and statistical modifications.
Note
A call to par is used in this function. Notice that the arguments
font.axis, font.lab, cex.lab, las and xpd are always set to 2, 2, 1.2, 1, and TRUE, respectively. Moreover,
the argument mar is always modified and depends on the number of levels in strata as well as whether or not the number-at-risk table is appended.
For optimal resolution and layout alignment when using the function in an R file, adjust the plot window.
For example, ideal dimensions are approximately 7 x 5” for 24” screens and 5.9 x 4.2” for 13” screens.
For optimal resolution and layout alignment when using the function in an R markdown file or similar, use fig.dim = c(7, 5) in the corresponding chunk.
Examples
lung <- survival::cancer
lung$time_y <- lung$time/365.25
lung$sex <- factor(lung$sex, levels = 1:2, labels = c("Male", "Female"))
# no strata
survival.plot(lung, time_y, status, units = "y")
# customizing
survival.plot(lung, time_y, status, units = "y",
xlab = "Time since diagnosis (in years)",
yscale = 100,
main = "Overall survival", mark.col = "darkgray")
# 2 strata
survival.plot(lung, time_y, status, sex, units = "y")
# customizing
survival.plot(lung, time_y, status, sex,
legend.pos = "topright", legend.title = "Sex",
events = TRUE, risk.text = FALSE, p.value.pos = c(2.4, 0.4),
units = "y", xlab = "Time since diagnosis (in years)",
main = "Overall survival", col = c("darkviolet", "darkgreen"))