Learn R Programming

scf (version 1.0.5)

scf_plot_cbar: Bar Plot of Summary Statistics by Grouping Variable in SCF Data

Description

Computes and plots a grouped summary statistic (either a mean, median, or quantile) for a continuous variable across a discrete factor. Estimates are pooled across implicates using scf_mean(), scf_median(), or scf_percentile(). Use this function to visualize the bivariate relationship between a discrete and a continuous variable.

Usage

scf_plot_cbar(
  design,
  yvar,
  xvar,
  stat = "mean",
  title = NULL,
  xlab = NULL,
  ylab = NULL,
  fill = "#0072B2",
  angle = 30,
  label_map = NULL
)

Value

A ggplot2 object.

Arguments

design

A scf_mi_survey object from scf_load().

yvar

One-sided formula for the continuous variable (e.g., ~networth).

xvar

One-sided formula for the grouping variable (e.g., ~racecl).

stat

"mean" (default), "median", or a quantile (numeric between 0 and 1).

title

Plot title (optional).

xlab

X-axis label (optional).

ylab

Y-axis label (optional).

fill

Bar fill color. Default is "#0072B2".

angle

Angle of x-axis labels. Default is 30.

label_map

Optional named vector to relabel x-axis category labels.

Implementation

The user specifies a continuous outcome (yvar) and a discrete grouping variable (xvar) via one-sided formulas. Group means are plotted by default. Medians or other percentiles can be specified via the stat argument.

Results are plotted using ggplot2::geom_col(), styled with scf_theme(), and optionally customized with additional arguments (e.g., axis labels, color, angles).

See Also

scf_mean(), scf_median(), scf_percentile(), scf_theme()

Examples

Run this code
# Do not implement these lines in real analysis:
# Use functions `scf_download()` and `scf_load()`
td <- tempfile("plot_cbar_")
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 mean net worth by education level
scf_plot_cbar(scf2022, ~networth, ~edcl, stat = "mean")

# Example for real analysis: Visualize 90th percentile of income by education
scf_plot_cbar(scf2022, ~income, ~edcl, stat = 0.9, fill = "#D55E00")

# 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