Learn R Programming

scf (version 1.0.5)

scf_plot_dist: Plot a Univariate Distribution of an SCF Variable

Description

This function provides a unified plotting interface for visualizing the distribution of a single variable from multiply-imputed SCF data. Discrete variables produce bar charts of pooled proportions; continuous variables produce binned histograms. Use this function to visualize the univariate distribution of an SCF variable.

Usage

scf_plot_dist(
  design,
  variable,
  bins = 30,
  title = NULL,
  xlab = NULL,
  ylab = "Percent",
  angle = 30,
  fill = "#0072B2",
  labels = NULL
)

Value

A ggplot2 object.

Arguments

design

A scf_mi_survey object created by scf_load().

variable

A one-sided formula specifying the variable to plot.

bins

Number of bins for continuous variables. Default is 30.

title

Optional plot title.

xlab

Optional x-axis label.

ylab

Optional y-axis label. Default is "Percent".

angle

Angle for x-axis tick labels. Default is 30.

fill

Fill color for bars. Default is "#0072B2".

labels

Optional named vector of custom axis labels (for discrete variables only).

Implementation

For discrete variables (factor or numeric with <= 25 unique values), the function uses scf_freq() to calculate category proportions and produces a bar chart. For continuous variables, it bins values across implicates and estimates Rubin-pooled frequencies for each bin.

Users may supply a named vector of custom axis labels using the labels argument.

See Also

scf_theme()

Examples

Run this code
# Mock workflow for CRAN (demo only — not real SCF data)
td <- tempfile("plot_dist_")
dir.create(td)

src <- system.file("extdata", "scf2022_mock_raw.rds", package = "scf")
file.copy(src, file.path(td, "scf2022.rds"), overwrite = TRUE)
scf2022 <- scf_load(2022, data_directory = td)

scf_plot_dist(scf2022, ~own)
scf_plot_dist(scf2022, ~age, bins = 10)

unlink(td, recursive = TRUE, force = TRUE)

Run the code above in your browser using DataLab