---
title: "Row Groups"
---
```{r}
#| include: false
library(tabviz)
```
Organize your forest plot into collapsible sections using the `group` parameter. This is ideal for meta-analyses with natural categories like study design, region, or subpopulation.
## Basic Grouping
Pass a column name to `group` to create collapsible sections:
```{r}
grouped_data <- data.frame(
study = c("Study A1", "Study A2", "Study B1", "Study B2", "Study B3"),
region = c("North", "North", "South", "South", "South"),
or = c(0.8, 0.9, 1.1, 1.3, 0.95),
lower = c(0.6, 0.7, 0.85, 1.0, 0.7),
upper = c(1.1, 1.15, 1.4, 1.7, 1.3)
)
forest_plot(grouped_data,
point = "or", lower = "lower", upper = "upper",
label = "study",
group = "region",
null_value = 1
)
```
Click the group headers to collapse or expand sections. Groups are rendered with:
- Distinct header rows with chevron indicators
- Automatic indentation of child rows
- Subtle background shading for visual hierarchy
---
## Hierarchical Grouping
For multi-level organization, pass multiple column names as a vector. tabviz creates nested, collapsible hierarchies:
```{r}
hierarchical_data <- data.frame(
study = c(
"Northeast Trial", "Southeast Trial", "Midwest Trial",
"Ontario Study", "Quebec Study",
"England RCT", "Scotland RCT",
"Bavaria Trial", "Berlin Trial"
),
region = c(
rep("Americas", 5),
rep("Europe", 4)
),
country = c(
rep("USA", 3), rep("Canada", 2),
rep("UK", 2), rep("Germany", 2)
),
or = c(0.82, 0.91, 0.78, 0.85, 0.88, 0.92, 0.87, 0.79, 0.84),
lower = c(0.68, 0.75, 0.62, 0.70, 0.72, 0.76, 0.71, 0.64, 0.68),
upper = c(0.99, 1.10, 0.98, 1.03, 1.07, 1.11, 1.07, 0.98, 1.04)
)
forest_plot(hierarchical_data,
point = "or", lower = "lower", upper = "upper",
label = "study",
group = c("region", "country"),
null_value = 1, scale = "log",
theme = web_theme_modern()
)
```
Each level of the hierarchy:
- Can be collapsed independently
- Has progressively deeper indentation
- Uses subtle visual differentiation (font weight, background)
---
## Customizing Group Headers
Control the visual styling of group headers at each level using theme modifiers:
```{r}
#| eval: false
forest_plot(hierarchical_data,
point = "or", lower = "lower", upper = "upper",
label = "study",
group = c("region", "country"),
null_value = 1, scale = "log"
) |>
set_group_headers(
level1_font_size = "1.1rem",
level1_font_weight = 700,
level1_background = "#e0f2fe",
level2_font_size = "0.95rem",
level2_italic = TRUE,
level2_background = "#f0f9ff",
indent_per_level = 20
)
```
See [Themes Reference](../reference/themes.qmd#set_group_headers) for all available options.
---
## Starting Collapsed
To have groups start in a collapsed state, you'll need to use explicit group definitions with `web_group()`:
```{r}
#| eval: false
forest_plot(grouped_data,
point = "or", lower = "lower", upper = "upper",
label = "study",
group = list(
web_group("North", collapsed = FALSE),
web_group("South", collapsed = TRUE)
),
null_value = 1
)
```
This is useful for large datasets where you want users to expand only the sections they're interested in.
---
## Grouping vs Row Styling
tabviz provides two complementary ways to organize data:
| Feature | Purpose | Best For |
|---------|---------|----------|
| **`group`** | Automatic collapsible sections from column values | Categorical data with natural groupings |
| **`row_type`** | Manual section headers, summaries, spacers | Structured layouts with subtotals |
Use `group` when your data has a grouping column. Use `row_type` when you need precise control over the layout structure, such as meta-analysis summary rows. See [Row Styling](row-styling.qmd) for the manual approach.
You can combine both approaches—use `group` for automatic organization and `row_type` for summary diamonds within each group.
---
## See Also
- [Row Styling](row-styling.qmd) — Manual row types, headers, summaries, and visual styling
- [Themes](themes.qmd) — Customize group header appearance
- [forest_plot()](../reference/tabviz.qmd) — Complete argument reference