View source: R/export-ObservationCounts.R
plot_counts_2D | R Documentation |
Computes the number of observations for each combination of two specified variables, and displays the result as a heatmap using ggplot2. If a stratification variable is provided, counts are calculated per strata and strata-specific heatmaps are displayed in individual panels. If an additional stratification variable is provided, separate plot windows are created for each level.
plot_counts_2D(
data,
x,
y,
stratify_by = NULL,
for_each = NULL,
color_gradient = c("blue", "beige", "red"),
title = "Observation counts",
legend_title = NULL,
subtitle = NULL,
x_lab = NULL,
y_lab = NULL
)
data |
Data frame containing all input variables. |
x |
Variable in |
y |
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 |
color_gradient |
(Optional) Color gradient for the heatmap. Specified as a character vector of three colors, representing: c(<low_counts>, <middle_counts>, <high_counts>).
Defaults to |
title |
(Optional) Plot title; defaults to |
legend_title |
(Optional) Legend title for color gradient; defaults to "Count". |
subtitle |
(Optional) Plot subtitle; defaults to |
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 |
If for_each
is not supplied, a ggplot object
showing a heatmap of counts for each x
-y
combination, optionally
faceted by stratify_by
. If for_each
is supplied, a named list
of such ggplot
objects, one per unique value of for_each
.
plot_counts_1D
, plot_binned_counts
,
plot_counts_with_mean
, ggplot
data("toy_data")
# Heatmap of counts by age and period
plot_counts_2D(toy_data, x = age, y = period)
# Heatmap of counts by age and period, stratified by education
plot_counts_2D(toy_data, x = period, y = age,
stratify_by = education)
# Heatmap of counts by age and period, stratified by education, for each sex
plot_counts_2D(toy_data, x = period, y = age,
stratify_by = education, for_each = sex)
Add the following code to your website.
For more information on customizing the embed code, read Embedding Snippets.