
Plot Missing Data Overview (Barplot, Longitudinal Heatmap or Record Map)
Source:R/rd_missplot.R
rd_missplot.RdGenerates a visual summary of missing data for variables in a REDCap dataset or list structure. With by = "vars" (default), cross-sectional (single-event) studies get a barplot showing the proportion or count of missing values per variable, and longitudinal studies (with the column redcap_event_name, also with a single event) get a heatmap illustrating missingness across events and variables. With by = "record", the plot shows one row per record and one column per variable, with the missing values painted.
Usage
rd_missplot(
data,
sort_by_missingness = TRUE,
max_vars = NULL,
show_percentages = TRUE,
use_field_labels = TRUE,
drop_complete = TRUE,
max_label_length = 40,
by = c("vars", "record"),
events = NULL,
forms = NULL
)Arguments
- data
A
data frameor a list containing adataelement retrieved from REDCap.- sort_by_missingness
Logical. Variables are ordered from highest to lowest number of missing values (the count, also when
show_percentages = TRUE; in longitudinal projects, the sum over the events). DefaultTRUE.- max_vars
Integer. If specified, only
max_varsvariables are plotted: the ones with the most missing values (by count, as insort_by_missingness), or the first ones in the original column order ifsort_by_missingness = FALSE. DefaultNULL(show all variables, asInf).- show_percentages
Logical. Displays missingness as percentages. Only used with
by = "vars". DefaultTRUE.- use_field_labels
Logical. Uses field labels from the dictionary for axis labels instead of variable names. Default
TRUE.- drop_complete
Logical. Variables with 0 missing values are excluded from the plot. Default
TRUE.- max_label_length
Integer. Maximum number of characters of the axis labels: longer labels are shortened. When several variables share a label, their variable name is then added to it in parentheses, so these labels can be longer. Use
NULLto keep them complete. Default40.- by
Character.
"vars"(default) summarises the missing values of each variable;"record"shows them record by record.- events
Character vector. Only for longitudinal projects: events to plot, by their unique name (e.g.
"baseline_arm_1") or the name shown in the plot (e.g."Baseline"). When several events have the same name (e.g. the same event in two arms), the plot adds their unique name to it in parentheses, and giving the shared name selects all of them. Use"all"to plot every event. DefaultNULL: every event withby = "vars"and the first event withby = "record".- forms
Character vector. Only plot the variables of these forms (instrument names as in the
form_namecolumn of the dictionary). Needs the dictionary. DefaultNULL(every form).
Value
A ggplot2 object representing a missingness barplot, a longitudinal missingness heatmap or a record-level missingness map.
Details
With by = "vars", the function automatically detects whether the input dataset contains longitudinal event information. If redcap_event_name is detected in the data columns, a missingness heatmap grouped by event is produced (with by = "record", see below).
When the event-form mapping is available (event_form element of the list), each variable is only evaluated in the events where its form is collected; the other cells of the heatmap are left blank.
With by = "record", longitudinal projects get one panel per event, each with the variables collected in that event. By default only the first event is plotted (with a warning); use events to choose others. Records without data in an event are shown in grey in that event's panel. With repeated instruments and repeating events, each repetition is a row of its own, and the cells of variables that are not collected in that row are shown in light grey. Every row is labelled with its record identifier.