Learn R Programming

scf (version 1.0.5)

scf_plot_dbar: Plot Bar Chart of a Discrete Variable from SCF Data

Description

Creates a bar chart that visualizes the distribution of a discrete variable.

Usage

scf_plot_dbar(
  design,
  variable,
  title = NULL,
  xlab = NULL,
  ylab = "Percent",
  angle = 30,
  fill = "#0072B2",
  label_map = NULL
)

Value

A ggplot2 object representing the pooled bar chart.

Arguments

design

A scf_mi_survey object created by scf_load(). Must contain valid implicates.

variable

A one-sided formula specifying a categorical variable (e.g., ~racecl).

title

Optional character string for the plot title. Default: "Distribution of <variable>".

xlab

Optional x-axis label. Default: variable name.

ylab

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

angle

Integer. Rotation angle for x-axis labels. Default is 30.

fill

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

label_map

Optional named vector to relabel x-axis category labels.

Implementation

This function internally calls scf_freq() to compute population proportion estimates, which are then plotted using ggplot2::geom_col(). The default output is scaled to percent and can be customized via title, axis labels, angle, and color.

Dependencies

Requires the ggplot2 package.

Details

Produces a bar chart of category proportions from a one-way tabulation, pooled across SCF implicates using scf_freq(). This function summarizes weighted sample composition and communicates categorical distributions effectively in descriptive analysis.

See Also

scf_freq(), scf_plot_bbar(), scf_xtab()

Examples

Run this code
# Do not implement these lines in real analysis:
# Use functions `scf_download()` and `scf_load()`
td <- tempfile("plot_dbar_")
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: Bar chart of education categories
scf_plot_dbar(scf2022, ~edcl)

# 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