Skip to content

Commit 1da66d3

Browse files
committed
add visual tests for color_scheme_view()
1 parent acfd44a commit 1da66d3

7 files changed

+175
-29
lines changed

R/bayesplot-colors.R

Lines changed: 19 additions & 28 deletions
Original file line numberDiff line numberDiff line change
@@ -106,10 +106,11 @@
106106
#' ppc_stat(y, yrep, stat = "sd") + legend_none()
107107
#' mcmc_areas(x, regex_pars = "beta")
108108
#'
109-
#' ###########################
110-
#' ### ColorBrewer schemes ###
111-
#' ###########################
109+
#' ##########################
110+
#' ### ColorBrewer scheme ###
111+
#' ##########################
112112
#' color_scheme_set("brewer-Spectral")
113+
#' color_scheme_view()
113114
#' mcmc_trace(x, pars = "sigma")
114115
#'
115116
#' ###########################
@@ -119,6 +120,7 @@
119120
#' "#ffad33", "#e68a00",
120121
#' "#995c00", "#663d00")
121122
#' color_scheme_set(orange_scheme)
123+
#' color_scheme_view()
122124
#' mcmc_areas(x, regex_pars = "alpha")
123125
#' mcmc_dens_overlay(x)
124126
#' ppc_stat(y, yrep, stat = "var") + legend_none()
@@ -171,20 +173,6 @@ color_scheme_get <- function(scheme, i) {
171173
scheme[i]
172174
}
173175

174-
#' @export
175-
print.bayesplot_scheme <- function(x, ...) {
176-
tab <- data.frame(unlist(x, use.names = FALSE),
177-
stringsAsFactors = FALSE)
178-
colnames(tab) <- attr(x, "scheme_name") %||% "hex_color"
179-
print(tab, ...)
180-
}
181-
#' @export
182-
plot.bayesplot_scheme <- function(x, ...) {
183-
scheme <- attr(x, "scheme_name") %||% stop("Scheme name not found.")
184-
plot_scheme(scheme)
185-
}
186-
187-
188176
#' @rdname bayesplot-colors
189177
#' @export
190178
color_scheme_view <- function(scheme = NULL) {
@@ -198,6 +186,17 @@ color_scheme_view <- function(scheme = NULL) {
198186
)
199187
}
200188

189+
#' @export
190+
print.bayesplot_scheme <- function(x, ...) {
191+
tab <- data.frame(unlist(x, use.names = FALSE), stringsAsFactors = FALSE)
192+
colnames(tab) <- attr(x, "scheme_name") %||% "hex_color"
193+
print(tab, ...)
194+
}
195+
#' @export
196+
plot.bayesplot_scheme <- function(x, ...) {
197+
scheme <- attr(x, "scheme_name") %||% stop("Scheme name not found.")
198+
plot_scheme(scheme)
199+
}
201200

202201

203202
# internal -----------------------------------------------------------------
@@ -207,25 +206,17 @@ color_scheme_view <- function(scheme = NULL) {
207206
plot_scheme <- function(scheme = NULL) {
208207
if (is.null(scheme)) {
209208
x <- color_scheme_get()
210-
x_name <- ""
211209
} else {
212210
x <- color_scheme_get(scheme)
213-
x_name <- factor(scheme)
214211
}
215212

216213
color_data <- data.frame(
217-
name = x_name,
214+
name = factor(attr(x, "scheme_name")),
218215
group = factor(names(x), levels = rev(names(x))),
219216
value = rep(1, length(x))
220217
)
221-
ggplot(
222-
color_data,
223-
aes_(
224-
x = ~ name,
225-
y = ~ value,
226-
fill = ~ group
227-
)
228-
) +
218+
219+
ggplot(color_data, aes_(x = ~ name, y = ~ value, fill = ~ group)) +
229220
geom_bar(
230221
width = .5,
231222
stat = "identity",

man/bayesplot-colors.Rd

Lines changed: 1 addition & 1 deletion
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.
Lines changed: 33 additions & 0 deletions
Loading
Lines changed: 33 additions & 0 deletions
Loading
Lines changed: 33 additions & 0 deletions
Loading
Lines changed: 33 additions & 0 deletions
Loading

tests/testthat/test-aesthetics.R

Lines changed: 23 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -200,3 +200,26 @@ test_that("ggplot2::theme_set overrides bayesplot theme", {
200200

201201
bayesplot_theme_set(bayesplot::theme_default())
202202
color_scheme_set()
203+
204+
205+
206+
# Visual tests ------------------------------------------------------------
207+
208+
test_that("color_scheme_view renders correctly", {
209+
testthat::skip_on_cran()
210+
211+
color_scheme_set()
212+
p_default <- color_scheme_view()
213+
vdiffr::expect_doppelganger("color_scheme_view (default)", p_default)
214+
215+
p_red <- color_scheme_view("red")
216+
vdiffr::expect_doppelganger("color_scheme_view (scheme specified)", p_red)
217+
218+
p_mix <- color_scheme_view("mix-red-blue")
219+
vdiffr::expect_doppelganger("color_scheme_view (mixed scheme)", p_mix)
220+
221+
p_brewer <- color_scheme_view("brewer-Spectral")
222+
vdiffr::expect_doppelganger("color_scheme_view (brewer palette)", p_brewer)
223+
224+
color_scheme_set()
225+
})

0 commit comments

Comments
 (0)