Skip to content

Commit bd8b804

Browse files
authored
Merge pull request #439 from ishaan-arora-1/docs/data-functions-vignette-435
Add vignette sections for *_data() companion functions
2 parents 617650b + 8dc1dc9 commit bd8b804

File tree

3 files changed

+58
-3
lines changed

3 files changed

+58
-3
lines changed

NEWS.md

Lines changed: 2 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,10 +1,9 @@
11
# bayesplot (development version)
22

3+
* Added vignette sections demonstrating `*_data()` companion functions for building custom ggplot2 visualizations (#435)
34
* Extract `drop_singleton_values()` helper in `mcmc_nuts_treedepth()` to remove duplicated filtering logic.
45
* Eliminate redundant data processing in `mcmc_areas_data()` by reusing the prepared MCMC array for both interval and density computation.
5-
* Validate equal chain lengths in `validate_df_with_chain()`, reject missing
6-
chain labels, and renumber data-frame chain labels internally when converting
7-
to arrays.
6+
* Validate equal chain lengths in `validate_df_with_chain()`, reject missing chain labels, and renumber data-frame chain labels internally when converting to arrays.
87
* Added unit tests for previously untested edge cases in `param_range()`, `param_glue()`, and `tidyselect_parameters()` (no-match, partial-match, and negation behavior).
98
* Bumped minimum version for `rstantools` from `>= 1.5.0` to `>= 2.0.0` .
109
* Use `rlang::warn()` and `rlang::inform()` for selected PPC user messages instead of base `warning()` and `message()`.

vignettes/graphical-ppcs.Rmd

Lines changed: 39 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -314,6 +314,45 @@ See Figure 8 in [Gabry et al. (2019)](#gabry2019) for another example of using
314314

315315
<br>
316316

317+
## Using `*_data()` functions for custom plots
318+
319+
Many bayesplot plotting functions have a companion `*_data()` function that
320+
returns the pre-processed data as a tidy data frame instead of a plot. This is
321+
useful when you want to build a fully custom ggplot2 visualization using the
322+
same summary statistics that bayesplot computes internally.
323+
324+
For example, `ppc_intervals_data()` returns the quantile summaries that
325+
`ppc_intervals()` uses:
326+
327+
```{r data_intervals, eval=params$EVAL}
328+
d <- ppc_intervals_data(y, yrep_nb, prob = 0.5, prob_outer = 0.9)
329+
head(d)
330+
```
331+
332+
You can then use this data to create your own plot:
333+
334+
```{r data_intervals_custom, eval=params$EVAL}
335+
ggplot(d, aes(x = x, y = m)) +
336+
geom_linerange(aes(ymin = ll, ymax = hh), color = "skyblue", linewidth = 0.6) +
337+
geom_linerange(aes(ymin = l, ymax = h), color = "steelblue", linewidth = 1.2) +
338+
geom_point(aes(y = y_obs), shape = 21, fill = "red", size = 1.5) +
339+
labs(title = "Custom interval plot from ppc_intervals_data()",
340+
x = "Observation", y = "Value") +
341+
theme_minimal()
342+
```
343+
344+
Similarly, `ppc_stat_data()` returns the computed test statistics:
345+
346+
```{r data_stat, eval=params$EVAL, message=FALSE}
347+
stat_d <- ppc_stat_data(y, yrep_nb, stat = "median")
348+
head(stat_d)
349+
```
350+
351+
See `available_ppc(plots_only = FALSE)` and `available_mcmc(plots_only = FALSE)`
352+
for a full list of data-preparation functions.
353+
354+
<br>
355+
317356
## Providing an interface to bayesplot PPCs from another package
318357

319358
The **bayesplot** package provides the S3 generic function `pp_check`. Authors of

vignettes/plotting-mcmc-draws.Rmd

Lines changed: 17 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -367,6 +367,23 @@ mcmc_trace_highlight(posterior, pars = "sigma", highlight = 3)
367367
```
368368

369369

370+
<br>
371+
372+
## Using `*_data()` functions for custom plots
373+
374+
As with PPC functions, many MCMC plotting functions have `*_data()` companions
375+
that return the underlying data instead of a plot. For example,
376+
`mcmc_intervals_data()` returns the quantiles used by `mcmc_intervals()`:
377+
378+
```{r data_intervals_mcmc}
379+
d <- mcmc_intervals_data(posterior, pars = c("(Intercept)", "sigma"))
380+
d
381+
```
382+
383+
This can be used to build fully custom ggplot2 visualizations using the same
384+
summary statistics that bayesplot computes internally. See
385+
`available_mcmc(plots_only = FALSE)` for a full list of `*_data()` functions.
386+
370387
<br>
371388

372389
## References

0 commit comments

Comments
 (0)