Treemaps display hierarchical data as nested rectangles. The size of each rectangle represents its value - larger rectangles mean larger values. Theyβre excellent for showing composition and proportions across many categories at once.
library(dashboardr)
library(dplyr)
library(gssr)
library(haven)
# Load GSS data
data(gss_all)
gss <- gss_all %>%
select(year, age, sex, race, degree, happy, polviews, region) %>%
filter(year == max(year, na.rm = TRUE),
!is.na(degree), !is.na(sex), !is.na(race), !is.na(region)) %>%
mutate(
degree = droplevels(as_factor(degree)),
sex = droplevels(as_factor(sex)),
race = droplevels(as_factor(race)),
region = droplevels(as_factor(region))
)Important: Treemaps require pre-aggregated
data with a value column. You typically need to
count() or summarize() your data first.
First, aggregate your data to get counts per category:
# Aggregate - treemaps need pre-summarized data!
degree_counts <- gss %>%
count(degree, name = "n")
degree_counts
#> # A tibble: 5 Γ 2
#> degree n
#> <fct> <int>
#> 1 less than high school 287
#> 2 high school 1476
#> 3 associate/junior college 290
#> 4 bachelor's 708
#> 5 graduate 465Now create the treemap:
Treemaps shine when showing nested categories. Use
subgroup_var for a second level:
# Two-level: degree within sex
degree_by_sex <- gss %>%
count(sex, degree, name = "n")
degree_by_sex
#> # A tibble: 10 Γ 3
#> sex degree n
#> <fct> <fct> <int>
#> 1 male less than high school 110
#> 2 male high school 692
#> 3 male associate/junior college 112
#> 4 male bachelor's 322
#> 5 male graduate 203
#> 6 female less than high school 177
#> 7 female high school 784
#> 8 female associate/junior college 178
#> 9 female bachelor's 386
#> 10 female graduate 262Customize what appears in the treemap cells and hover tooltips:
plot <- viz_treemap(
data = degree_counts,
group_var = "degree",
value_var = "n",
title = "Education Distribution",
value_label = "Respondents", # Label for the value in tooltips
tooltip_suffix = " people"
)
plot| Parameter | Description | Example |
|---|---|---|
value_label |
Label for value in tooltip | "Count", "Revenue" |
tooltip_prefix |
Text before value | "$", "N = " |
tooltip_suffix |
Text after value | " units", "%" |
For hierarchical treemaps, both group and subgroup appear in tooltips automatically.
content <- create_content(data = degree_counts, type = "treemap") %>%
add_viz(
group_var = "degree",
value_var = "n",
title = "Education Breakdown"
)
content %>% preview()# Prepare different aggregations
sex_counts <- gss %>% count(sex, name = "n")
race_counts <- gss %>% count(race, name = "n")
region_counts <- gss %>% count(region, name = "n")
# Combine into one dataset with a category indicator
all_counts <- bind_rows(
sex_counts %>% rename(category = sex) %>% mutate(type = "Sex"),
race_counts %>% rename(category = race) %>% mutate(type = "Race"),
region_counts %>% rename(category = region) %>% mutate(type = "Region")
)
content <- create_content(data = all_counts, type = "treemap") %>%
add_viz(
group_var = "category",
value_var = "n",
title = "By Sex",
filter = ~ type == "Sex",
tabgroup = "Demographics"
) %>%
add_viz(
group_var = "category",
value_var = "n",
title = "By Race",
filter = ~ type == "Race",
tabgroup = "Demographics"
) %>%
add_viz(
group_var = "category",
value_var = "n",
title = "By Region",
filter = ~ type == "Region",
tabgroup = "Demographics"
)
content %>% preview()Treemaps need aggregated data. Hereβs how to prepare GSS data:
# Count by two variables for nested treemap
gss %>% count(sex, degree, name = "n")
#> # A tibble: 10 Γ 3
#> sex degree n
#> <fct> <fct> <int>
#> 1 male less than high school 110
#> 2 male high school 692
#> 3 male associate/junior college 112
#> 4 male bachelor's 322
#> 5 male graduate 203
#> 6 female less than high school 177
#> 7 female high school 784
#> 8 female associate/junior college 178
#> 9 female bachelor's 386
#> 10 female graduate 262# Average age by education level
gss %>%
group_by(degree) %>%
summarize(avg_age = mean(age, na.rm = TRUE), .groups = "drop")
#> # A tibble: 5 Γ 2
#> degree avg_age
#> <fct> <dbl>
#> 1 less than high school 49.9
#> 2 high school 49.8
#> 3 associate/junior college 49.8
#> 4 bachelor's 51.1
#> 5 graduate 52.4Use treemaps when: - Showing part-to-whole relationships - Comparing sizes across many categories - Displaying hierarchical data - Space is limited (more compact than bar charts)
Use bar charts instead when: - Precise comparisons are important - You have few categories (< 5) - Order matters (rankings)
?viz_treemap - Full function documentationvignette("bar_vignette") - For categorical
comparisonsvignette("content-collections") - For dashboard
integration