The viz_scatter() function visualizes the relationship
between two numeric variables. Each point represents one observation,
positioned by its x and y values. Essential for exploring correlations,
clusters, and outliers.
library(dashboardr)
library(dplyr)
library(gssr)
library(haven)
# Load GSS data - we need numeric variables for scatter plots
data(gss_all)
gss <- gss_all %>%
select(year, age, sex, race, degree, happy, polviews, educ, realinc) %>%
filter(year == 2022, # Use 2022 which has realinc data
!is.na(age), !is.na(educ), !is.na(realinc),
realinc > 0, educ > 0) %>%
mutate(
sex = droplevels(as_factor(sex)),
degree = droplevels(as_factor(degree))
)Create a simple scatter plot showing the relationship between education (years) and income:
Use show_trend = TRUE to add a regression line:
Use color_var to color points by a categorical
variable:
Another relationship to explore - age and years of education:
For dense data, use alpha to reveal patterns:
Customize axis labels and tooltip formatting for better readability:
plot <- viz_scatter(
data = gss,
x_var = "educ",
y_var = "realinc",
title = "Education vs Income",
x_label = "Years of Education",
y_label = "Annual Income (USD)",
tooltip_format = "Education: {x} years, Income: ${y}"
)
plot| Parameter | Description | Example |
|---|---|---|
x_label |
Custom x-axis label | "Years of Education" |
y_label |
Custom y-axis label | "Income (USD)" |
tooltip_format |
Custom tooltip template | "x: {x}, y: {y}" |
The tooltip_format parameter supports placeholders:
{x} for x-value, {y} for y-value, and
{color} for the color group.
Integrate scatter plots into dashboards:
content <- create_content(data = gss, type = "scatter") %>%
add_viz(
x_var = "educ",
y_var = "realinc",
show_trend = TRUE,
title = "Education vs Income"
)
content %>% preview()Compare relationships across groups:
content <- create_content(data = gss, type = "scatter", alpha = 0.5) %>%
add_viz(
x_var = "educ",
y_var = "realinc",
title = "Male",
filter = ~ sex == "male",
tabgroup = "By Sex"
) %>%
add_viz(
x_var = "educ",
y_var = "realinc",
title = "Female",
filter = ~ sex == "female",
tabgroup = "By Sex"
)
content %>% preview()content <- create_content(data = gss, type = "scatter", alpha = 0.4, show_trend = TRUE) %>%
add_viz(
x_var = "educ",
y_var = "realinc",
title = "Education → Income",
tabgroup = "Relationships"
) %>%
add_viz(
x_var = "age",
y_var = "realinc",
title = "Age → Income",
tabgroup = "Relationships"
)
content %>% preview()| Pattern | Meaning |
|---|---|
| Upward slope | Positive relationship |
| Downward slope | Negative relationship |
| No pattern | No linear relationship |
| Tight cluster | Strong relationship |
| Wide scatter | Weak relationship |
Use viz_scatter() when: - Exploring
relationship between two numeric variables - Looking for correlations -
Identifying outliers - Showing individual-level data
Use viz_histogram() when: - Showing
distribution of a single variable
Use viz_heatmap() when: - Data is
aggregated (means, counts) - Many overlapping points
?viz_scatter - Full function documentationvignette("histogram_vignette") - For single-variable
distributionsvignette("heatmap_vignette") - For aggregated
relationshipsvignette("content-collections") - For dashboard
integration