The viz_histogram() function visualizes the distribution
of continuous numeric variables. Unlike bar charts (which count
categories), histograms show how values are spread across a range by
grouping them into bins.
Create a simple histogram showing the distribution of age:
The bins parameter controls granularity. More bins =
more detail but noisier; fewer bins = smoother but less detail.
By default, histograms show counts. Use
histogram_type = "percent" to show percentages:
By default, histograms show count/percentage labels on each bar. Use
data_labels_enabled = FALSE to hide them for a cleaner
look:
plot <- viz_histogram(
data = gss,
x_var = "age",
bins = 25,
title = "Histogram Without Data Labels",
data_labels_enabled = FALSE
)
plotThis is useful when you have many bins or want a simpler visualization.
Customize axis labels and tooltip text for a polished presentation:
plot <- viz_histogram(
data = gss,
x_var = "age",
bins = 25,
title = "Age Distribution",
x_label = "Age (years)",
y_label = "Number of Respondents",
tooltip_suffix = " people",
x_tooltip_suffix = " years old"
)
plot| Parameter | Description | Example |
|---|---|---|
x_label |
Custom x-axis label | "Age (years)" |
y_label |
Custom y-axis label | "Frequency", "Percentage" |
tooltip_prefix |
Text before tooltip value | "Count: " |
tooltip_suffix |
Text after tooltip value | " respondents" |
x_tooltip_suffix |
Text after x value in tooltip | " years" |
Integrate histograms into dashboards using
type = "histogram":
content <- create_content(data = gss, type = "histogram") %>%
add_viz(
x_var = "age",
bins = 25,
title = "Age Distribution"
)
content %>% preview()Compare distributions across groups using filters:
content <- create_content(data = gss, type = "histogram", bins = 20) %>%
add_viz(
x_var = "age",
title = "Male",
filter = ~ sex == "male",
tabgroup = "By Sex"
) %>%
add_viz(
x_var = "age",
title = "Female",
filter = ~ sex == "female",
tabgroup = "By Sex"
)
content %>% preview()# Prepare data with year as numeric
gss_numeric <- gss %>%
mutate(year_num = as.numeric(year))
content <- create_content(data = gss_numeric, type = "histogram", bins = 20) %>%
add_viz(x_var = "age", title = "Age Distribution", tabgroup = "Distributions")
content %>% preview()| Shape | Meaning | Example |
|---|---|---|
| Normal (bell) | Symmetric around mean | Test scores, heights |
| Right-skewed | Long tail to the right | Income, response times |
| Left-skewed | Long tail to the left | Age at retirement |
| Bimodal | Two peaks | Mixed populations |
| Uniform | Flat, equal frequencies | Random numbers |
Use viz_histogram() when: - Showing
distribution of a continuous variable - Analyzing spread and shape of
data - Looking for outliers or unusual patterns
Use viz_bar() when: - Counting
categorical values - Comparing groups side-by-side
?viz_histogram - Full function documentationvignette("bar_vignette") - For categorical datavignette("content-collections") - For dashboard
integration