Learn R Programming

scf (version 1.0.5)

scf_plot_smooth: Smoothed Distribution Plot of a Continuous Variable in SCF Data

Description

Draws a smoothed distribution plot of a continuous variable in the SCF. Use this function to visualize a single continuous variable's distribution.

Usage

scf_plot_smooth(
  design,
  variable,
  binwidth = NULL,
  xlim = NULL,
  method = "loess",
  span = 0.2,
  color = "blue",
  xlab = NULL,
  ylab = "Percent of Households",
  title = NULL
)

Value

A ggplot2 object.

Arguments

design

A scf_mi_survey object created by scf_load().

variable

A one-sided formula specifying a continuous variable (e.g., ~networth).

binwidth

Optional bin width. Default uses Freedman–Diaconis rule.

xlim

Optional numeric vector of length 2 to truncate axis.

method

Character. Smoothing method: "loess" (default) or "lm".

span

Numeric LOESS span. Default is 0.2. Ignored if method = "lm".

color

Line color. Default is "blue".

xlab

Optional label for x-axis. Defaults to the variable name.

ylab

Optional label for y-axis. Defaults to "Percent of Households".

title

Optional plot title.

Implementation

Visualizes the weighted distribution of a continuous SCF variable by stacking implicates, binning observations, and smoothing pooled proportions. This function is useful for examining distribution shape, skew, or modality in variables like income or wealth.

All implicates are stacked and weighted, binned across a data-driven or user-specified bin width. Each bin's weight share is calculated, and a smoothing curve is fit to the resulting pseudo-density.

See Also

scf_theme(), scf_plot_dist()

Examples

Run this code
# Do not implement these lines in real analysis:
# Use functions `scf_download()` and `scf_load()`
td <- tempfile("plot_smooth_")
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)

# Example for real analysis: Plot smoothed distribution
scf_plot_smooth(scf2022, ~networth, xlim = c(0, 2e6),
                method = "loess", span = 0.25)
     
# Do not implement these lines in real analysis: Cleanup for package check           
unlink(td, recursive = TRUE, force = TRUE)

Run the code above in your browser using DataLab