Skip to content

Commit a947141

Browse files
authored
Merge pull request #187 from stan-dev/hexbin-to-suggests
Move hexbin package to Suggests
2 parents 5eced5b + f27b2e9 commit a947141

File tree

4 files changed

+37
-20
lines changed

4 files changed

+37
-20
lines changed

DESCRIPTION

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -24,15 +24,15 @@ Depends:
2424
Imports:
2525
dplyr (>= 0.8.0),
2626
ggplot2 (>= 2.2.1),
27+
ggridges,
2728
reshape2,
28-
stats,
29-
utils,
3029
rlang (>= 0.3.0),
31-
ggridges,
30+
stats,
3231
tibble,
33-
hexbin
32+
utils
3433
Suggests:
3534
gridExtra (>= 2.2.1),
35+
hexbin,
3636
knitr (>= 1.16),
3737
loo (>= 2.0.0),
3838
RColorBrewer,

R/mcmc-scatterplots.R

Lines changed: 9 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -114,11 +114,13 @@
114114
#' size = .75, linetype = 2)
115115
#'
116116
#' \donttest{
117-
#' # hexagonal heatmap
118-
#' color_scheme_set("brightblue")
119-
#' (p <- mcmc_hex(x, pars = c("sigma", "alpha"), transform = list(sigma = "log")))
120-
#' p + plot_bg(fill = "gray95")
121-
#' p + plot_bg(fill = "gray95") + panel_bg(fill = "gray70")
117+
#' if (requireNamespace("hexbin", quietly = TRUE)) {
118+
#' # hexagonal heatmap
119+
#' color_scheme_set("brightblue")
120+
#' (p <- mcmc_hex(x, pars = c("sigma", "alpha"), transform = list(sigma = "log")))
121+
#' p + plot_bg(fill = "gray95")
122+
#' p + plot_bg(fill = "gray95") + panel_bg(fill = "gray70")
123+
#' }
122124
#' }
123125
NULL
124126

@@ -157,6 +159,8 @@ mcmc_hex <- function(x,
157159
transformations = list(),
158160
...,
159161
binwidth = NULL) {
162+
suggested_package("scales")
163+
suggested_package("hexbin")
160164
check_ignored_arguments(...)
161165
.mcmc_scatter(
162166
x,
@@ -697,7 +701,6 @@ pairs_condition <- function(chains = NULL, draws = NULL, nuts = NULL) {
697701
)
698702
}
699703
} else { # hex binning
700-
suggested_package("scales")
701704
graph <- graph +
702705
geom_hex(
703706
aes_(fill = ~ scales::rescale(..density..)),

tests/testthat/test-mcmc-scatter-and-parcoord.R

Lines changed: 20 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
library(bayesplot)
22
suppressPackageStartupMessages(library(rstanarm))
3-
context("MCMC: scatter and parallel coordinates plots")
3+
context("MCMC: scatter, hex, and parallel coordinates plots")
44

55
source(test_path("data-for-mcmc-tests.R"))
66

@@ -10,6 +10,8 @@ post <- as.array(fit)
1010
lp <- log_posterior(fit)
1111
np <- ensure_divergences(nuts_params(fit))
1212

13+
14+
# mcmc_scatter/hex --------------------------------------------------------
1315
test_that("mcmc_scatter returns a ggplot object", {
1416
expect_gg(mcmc_scatter(arr, pars = c("beta[1]", "beta[2]")))
1517
expect_gg(mcmc_scatter(arr1chain, regex_pars = "beta", size = 3, alpha = 0.5))
@@ -19,17 +21,11 @@ test_that("mcmc_scatter returns a ggplot object", {
1921
pars = c("sigma", "(Intercept)")))
2022
})
2123

22-
test_that("mcmc_hex returns a ggplot object", {
23-
expect_gg(mcmc_hex(arr, pars = c("beta[1]", "beta[2]")))
24-
expect_gg(mcmc_hex(arr1chain, regex_pars = "beta", binwidth = c(.5,.5)))
25-
})
26-
27-
test_that("mcmc_scatter & mcmc_hex throw error if only 1 parameter", {
24+
test_that("mcmc_scatter throws error if number of parameters is not 2", {
25+
expect_error(mcmc_scatter(arr, pars = c("sigma", "beta[1]", "beta[2]")), "exactly 2 parameters")
2826
expect_error(mcmc_scatter(arr, pars = "sigma"), "exactly 2 parameters")
2927
expect_error(mcmc_scatter(arr1), "exactly 2 parameters")
3028
expect_error(mcmc_scatter(mat1), "exactly 2 parameters")
31-
expect_error(mcmc_hex(dframe1), "exactly 2 parameters")
32-
expect_error(mcmc_hex(chainlist1), "exactly 2 parameters")
3329
})
3430

3531
test_that("mcmc_scatter accepts NUTS info", {
@@ -42,6 +38,21 @@ test_that("mcmc_scatter accepts NUTS info", {
4238
expect_named(g$data, c("x", "y", "Divergent"))
4339
})
4440

41+
test_that("mcmc_hex returns a ggplot object", {
42+
skip_if_not_installed("hexbin")
43+
expect_gg(mcmc_hex(arr, pars = c("beta[1]", "beta[2]")))
44+
expect_gg(mcmc_hex(arr1chain, regex_pars = "beta", binwidth = c(.5,.5)))
45+
})
46+
47+
test_that("mcmc_hex throws error if number of parameters is not 2", {
48+
skip_if_not_installed("hexbin")
49+
expect_error(mcmc_hex(arr, pars = c("sigma", "beta[1]", "beta[2]")), "exactly 2 parameters")
50+
expect_error(mcmc_hex(arr, pars = "sigma"), "exactly 2 parameters")
51+
expect_error(mcmc_hex(arr1), "exactly 2 parameters")
52+
expect_error(mcmc_hex(mat1), "exactly 2 parameters")
53+
})
54+
55+
4556

4657
# mcmc_pairs -------------------------------------------------------------
4758
test_that("mcmc_pairs returns a bayesplot_grid object", {

vignettes/plotting-mcmc-draws.Rmd

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -250,7 +250,10 @@ The `mcmc_hex` function creates a similar plot but using hexagonal binning,
250250
which can be useful to avoid overplotting.
251251

252252
```{r, mcmc_hex}
253-
mcmc_hex(posterior, pars = c("(Intercept)", "wt"))
253+
# requires hexbin package
254+
if (requireNamespace("hexbin", quietly = TRUE)) {
255+
mcmc_hex(posterior, pars = c("(Intercept)", "wt"))
256+
}
254257
```
255258

256259
#### mcmc_pairs

0 commit comments

Comments
 (0)