Code
forest_plot(meta_data,
point = "hr", lower = "lower", upper = "upper",
label = "study", null_value = 1, scale = "log",
axis_range = c(0.3, 2.0),
theme = web_theme_modern()
)webforest automatically calculates appropriate axis ranges and tick positions based on your data. Only override when you need specific control.
By default, the axis range is auto-calculated to fit your data. Override with axis_range:
Specify exact tick positions with axis_ticks:
Add vertical gridlines at tick positions:
Customize gridline style via theme:
Gridline styles: "solid", "dashed", "dotted".
Add a label below the axis:
Default for differences (e.g., mean difference, risk difference):
diff_data <- data.frame(
study = c("Study A", "Study B", "Study C"),
md = c(-0.5, 0.2, -0.3),
lower = c(-0.9, -0.2, -0.7),
upper = c(-0.1, 0.6, 0.1)
)
forest_plot(diff_data,
point = "md", lower = "lower", upper = "upper",
label = "study", null_value = 0,
axis_label = "Mean Difference (95% CI)",
theme = web_theme_modern()
)Use for ratios (odds ratio, hazard ratio, risk ratio):
The null_value parameter draws a dashed vertical reference line:
null_value = 1 for ratios (OR, HR, RR)null_value = 0 for differences (MD, RD)Add additional reference lines with forest_refline():
Reference line options:
spec <- web_spec(meta_data,
point = "hr", lower = "lower", upper = "upper",
label = "study", null_value = 1, scale = "log",
title = "Treatment Effect on Primary Outcome",
subtitle = "Randomized controlled trials, 2020-2024",
caption = "HR < 1 favors treatment",
footnote = "Random effects meta-analysis with DerSimonian-Laird estimator"
)
forest_plot(spec, axis_label = "Hazard Ratio (95% CI)")| Parameter | Description | Example |
|---|---|---|
axis_range |
Min/max axis values | c(0.3, 2.0) |
axis_ticks |
Custom tick positions | c(0.5, 1, 2) |
axis_gridlines |
Show vertical gridlines | TRUE |
axis_label |
X-axis label | "Odds Ratio" |
scale |
Linear or log scale | "log" |
null_value |
Reference line position | 1 |
title |
Plot title | "My Analysis" |
subtitle |
Secondary title | "2020-2024" |
caption |
Bottom caption | "HR < 1 favors..." |
footnote |
Footnote text | "Random effects" |
---
title: "Axis & Annotations"
---
```{r}
#| include: false
library(webforest)
# Sample data
meta_data <- data.frame(
study = c("Smith 2020", "Jones 2021", "Lee 2022", "Chen 2023", "Park 2024"),
hr = c(0.72, 0.85, 0.91, 0.68, 0.79),
lower = c(0.55, 0.70, 0.75, 0.52, 0.61),
upper = c(0.95, 1.03, 1.10, 0.89, 1.02)
)
```
::: {.callout-note}
## Auto-Scaling by Default
webforest automatically calculates appropriate axis ranges and tick positions based on your data. Only override when you need specific control.
:::
## Axis Control
### Axis Range
By default, the axis range is auto-calculated to fit your data. Override with `axis_range`:
```{r}
forest_plot(meta_data,
point = "hr", lower = "lower", upper = "upper",
label = "study", null_value = 1, scale = "log",
axis_range = c(0.3, 2.0),
theme = web_theme_modern()
)
```
### Custom Tick Values
Specify exact tick positions with `axis_ticks`:
```{r}
forest_plot(meta_data,
point = "hr", lower = "lower", upper = "upper",
label = "study", null_value = 1, scale = "log",
axis_range = c(0.3, 2.0),
axis_ticks = c(0.5, 0.7, 1.0, 1.5, 2.0),
theme = web_theme_modern()
)
```
### Gridlines
Add vertical gridlines at tick positions:
```{r}
forest_plot(meta_data,
point = "hr", lower = "lower", upper = "upper",
label = "study", null_value = 1, scale = "log",
axis_gridlines = TRUE,
theme = web_theme_modern()
)
```
Customize gridline style via theme:
```{r}
custom_theme <- web_theme_modern() |>
set_axis(gridlines = TRUE, gridline_style = "dotted")
forest_plot(meta_data,
point = "hr", lower = "lower", upper = "upper",
label = "study", null_value = 1, scale = "log",
theme = custom_theme
)
```
Gridline styles: `"solid"`, `"dashed"`, `"dotted"`.
### Axis Label
Add a label below the axis:
```{r}
forest_plot(meta_data,
point = "hr", lower = "lower", upper = "upper",
label = "study", null_value = 1, scale = "log",
axis_label = "Hazard Ratio (95% CI)",
theme = web_theme_modern()
)
```
## Scale Types
### Linear Scale
Default for differences (e.g., mean difference, risk difference):
```{r}
diff_data <- data.frame(
study = c("Study A", "Study B", "Study C"),
md = c(-0.5, 0.2, -0.3),
lower = c(-0.9, -0.2, -0.7),
upper = c(-0.1, 0.6, 0.1)
)
forest_plot(diff_data,
point = "md", lower = "lower", upper = "upper",
label = "study", null_value = 0,
axis_label = "Mean Difference (95% CI)",
theme = web_theme_modern()
)
```
### Log Scale
Use for ratios (odds ratio, hazard ratio, risk ratio):
```{r}
forest_plot(meta_data,
point = "hr", lower = "lower", upper = "upper",
label = "study", null_value = 1, scale = "log",
axis_label = "Hazard Ratio (95% CI)",
theme = web_theme_modern()
)
```
## Reference Lines
### Null Value Line
The `null_value` parameter draws a dashed vertical reference line:
- Use `null_value = 1` for ratios (OR, HR, RR)
- Use `null_value = 0` for differences (MD, RD)
### Custom Reference Lines
Add additional reference lines with `forest_refline()`:
```{r}
spec <- web_spec(meta_data,
point = "hr", lower = "lower", upper = "upper",
label = "study", null_value = 1, scale = "log",
annotations = list(
forest_refline(0.8, label = "Threshold", style = "dashed", color = "#e11d48")
)
)
forest_plot(spec, axis_label = "Hazard Ratio (95% CI)")
```
Reference line options:
```r
forest_refline(
value, # X-axis position
label = NULL, # Optional label text
style = "dashed", # "solid", "dashed", "dotted"
color = "#94a3b8" # Line color
)
```
## Labels
### Title and Subtitle
```{r}
forest_plot(meta_data,
point = "hr", lower = "lower", upper = "upper",
label = "study", null_value = 1, scale = "log",
title = "Treatment Effect on Primary Outcome",
subtitle = "Randomized controlled trials, 2020-2024",
theme = web_theme_modern()
)
```
### Caption and Footnote
```{r}
forest_plot(meta_data,
point = "hr", lower = "lower", upper = "upper",
label = "study", null_value = 1, scale = "log",
title = "Treatment Effect",
caption = "HR < 1 favors treatment",
footnote = "Random effects meta-analysis",
theme = web_theme_modern()
)
```
### All Labels Together
```{r}
spec <- web_spec(meta_data,
point = "hr", lower = "lower", upper = "upper",
label = "study", null_value = 1, scale = "log",
title = "Treatment Effect on Primary Outcome",
subtitle = "Randomized controlled trials, 2020-2024",
caption = "HR < 1 favors treatment",
footnote = "Random effects meta-analysis with DerSimonian-Laird estimator"
)
forest_plot(spec, axis_label = "Hazard Ratio (95% CI)")
```
## Summary
| Parameter | Description | Example |
|-----------|-------------|---------|
| `axis_range` | Min/max axis values | `c(0.3, 2.0)` |
| `axis_ticks` | Custom tick positions | `c(0.5, 1, 2)` |
| `axis_gridlines` | Show vertical gridlines | `TRUE` |
| `axis_label` | X-axis label | `"Odds Ratio"` |
| `scale` | Linear or log scale | `"log"` |
| `null_value` | Reference line position | `1` |
| `title` | Plot title | `"My Analysis"` |
| `subtitle` | Secondary title | `"2020-2024"` |
| `caption` | Bottom caption | `"HR < 1 favors..."` |
| `footnote` | Footnote text | `"Random effects"` |