View source: R/export-ObservationCounts.R
plot_binned_counts | R Documentation |
Bins a specified numeric variable into intervals, counts observations per value of a specified variable and bin groups, and plots lines for each bin group using ggplot2. If a stratification variable is provided, counts are calculated per strata and plotted as separate colored lines. If an additional stratification variable is provided, separate plot windows are created for each level.
plot_binned_counts(
data,
x,
bin_by,
stratify_by = NULL,
for_each = NULL,
n_bins = 8,
bin_width = NULL,
title = "Observation counts",
subtitle = NULL,
legend_title = NULL,
x_lab = NULL,
y_lab = NULL,
viridis_color_option = "D"
)
data |
Data frame containing all input variables. |
x |
Variable in |
bin_by |
Numeric variable in |
stratify_by |
(Optional) Stratification variable. If
supplied, counts are computed for each combination of |
for_each |
(Optional) Additional stratification variable. If supplied,
separate plot windows are created per level of |
n_bins |
(Optional) Number of bins to create across |
bin_width |
(Optional) Width of the bins created across |
title |
(Optional) Plot title; defaults to |
subtitle |
(Optional) Plot subtitle; defaults to |
legend_title |
(Optional) Legend title; defaults to name of |
x_lab |
(Optional) Label for the x-axis; defaults to the name of |
y_lab |
(Optional) Label for the y-axis; defaults to the name of |
viridis_color_option |
(Optional) Option for color gradient; defaults to "D". Options are "A", "B", "C", "D", E", "F", "G", "H". See viridis for information, or experiment yourself. |
If for_each
is not supplied, a ggplot object showing counts
per x
and bin groups, optionally faceted by stratify_by
. If for_each
is supplied, a named list of such plots.
plot_counts_1D
, plot_counts_2D
,
plot_counts_with_mean
, ggplot
data("toy_data")
# Counts by period, binned by age
plot_binned_counts(toy_data, x = period,
bin_by = age, n_bins = 4)
# Counts by period, binned by age, stratified by education levels
plot_binned_counts(toy_data, period,
bin_by = age, n_bins = 4,
stratify_by = education)
# Counts by period, binned by age, stratified by education levels, for each sex
plot_binned_counts(toy_data, period,
bin_by = age, n_bins = 4,
stratify_by = education, for_each = sex)
Add the following code to your website.
For more information on customizing the embed code, read Embedding Snippets.