1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162
|
library(bayesplot)
library(ggplot2)
context("Convenience functions (for ggplot objects)")
# abline_01, vline_ and hline_ ------------------------------------------
test_that("vline_* and hline_* return correct objects", {
a <- vline_0(color = "red")
})
test_that("vline_at with 'fun' works", {
x <- example_mcmc_draws(chains = 1)
a <- vline_at(x, colMeans)
b <- geom_vline(xintercept = colMeans(x), na.rm = TRUE)
})
test_that("calc_v (internal function) works", {
a <- 1:4
expect_identical(calc_v(a, "mean"), 2.5)
expect_identical(calc_v(a, median), 2.5)
expect_equal(calc_v(c(a, NA), mean), NA_real_)
expect_identical(calc_v(c(a, NA), min, list(na.rm = TRUE)), 1L)
expect_error(calc_v(fun = "mean"), "'v' can't be missing")
})
# lbub --------------------------------------------------------------------
test_that("lbub works", {
f1 <- lbub(p = 0.5)
f2 <- lbub(p = 0.5, med = FALSE)
expect_type(f1, "closure")
expect_type(f2, "closure")
expect_identical(
f1(1:50),
setNames(c(13.25, 25.5, 37.75), c("25%", "50%", "75%"))
)
expect_identical(
f2(1:50),
setNames(c(13.25, 37.75), c("25%", "75%"))
)
})
# plot and facet backgrounds ----------------------------------------------
test_that("grid_lines returns correct theme object", {
thm <- theme_default() + grid_lines(size = 1.5, color = "purple")
expect_equal(thm$panel.grid.major, element_line(linewidth = 1.5, color = "purple"))
expect_equal(thm$panel.grid.minor, element_line(linewidth = 0.75, color = "purple"))
})
test_that("panel_bg returns correct theme object", {
bg1 <- panel_bg()
bg2 <- panel_bg(fill = "blue", linetype = 2)
expect_identical(bg1, theme(panel.background = element_rect()))
expect_identical(bg2, theme(panel.background = element_rect(fill = "blue", linetype = 2)))
expect_identical(panel_bg(on = FALSE), theme(panel.background = element_blank()))
})
test_that("plot_bg returns correct theme object", {
bg1 <- plot_bg()
bg2 <- plot_bg(fill = "blue", linetype = 2)
expect_identical(bg1, theme(plot.background = element_rect()))
expect_identical(bg2, theme(plot.background = element_rect(fill = "blue", linetype = 2)))
expect_identical(plot_bg(on = FALSE), theme(plot.background = element_blank()))
})
test_that("facet_bg returns correct theme object", {
bg1 <- facet_bg()
bg2 <- facet_bg(fill = "blue", linetype = 2)
expect_identical(bg1, theme(strip.background = element_rect()))
expect_identical(bg2, theme(strip.background = element_rect(fill = "blue", linetype = 2)))
expect_identical(facet_bg(on = FALSE), theme(strip.background = element_blank()))
})
# legend position and text ------------------------------------------------
test_that("legend_none returns correct theme object", {
none <- legend_none()
expect_s3_class(none, "theme")
expect_equivalent(none, list(legend.position = "none"))
expect_false(attr(none, "complete"))
})
test_that("legend_move returns correct theme object", {
left <- legend_move("left")
expect_s3_class(left, "theme")
expect_equivalent(left, list(legend.position = "left"))
expect_false(attr(left, "complete"))
pos <- legend_move(c(0.25, 0.5))
expect_s3_class(pos, "theme")
expect_equivalent(
pos$legend.position.inside %||% pos$legend.position,
c(0.25, 0.5)
)
expect_false(attr(pos, "complete"))
})
test_that("legend_text returns correct theme object", {
expect_equal(
legend_text(size = 16, color = "purple"),
theme(legend.text = element_text(color = "purple", size = 16))
)
})
# axis and facet text --------------------------------------------------
test_that("xaxis_text returns correct theme object", {
expect_identical(xaxis_text(FALSE), theme(axis.text.x = element_blank()))
expect_equal(
xaxis_text(face = "bold", angle = 30),
theme(axis.text.x = element_text(face = "bold", angle = 30))
)
})
test_that("yaxis_text returns correct theme object", {
expect_identical(yaxis_text(FALSE), theme(axis.text.y = element_blank()))
expect_equivalent(
yaxis_text(face = "bold", angle = 30),
theme(axis.text.y = element_text(face = "bold", angle = 30))
)
})
test_that("facet_text returns correct theme object", {
expect_identical(facet_text(FALSE), theme(strip.text = element_blank()))
expect_equal(
facet_text(size = 12, color = "blue"),
theme(strip.text = element_text(color = "blue", size = 12))
)
})
# axis titles -------------------------------------------------------------
test_that("xaxis_title returns correct theme object", {
expect_identical(xaxis_title(FALSE), xlab(NULL))
expect_equal(
xaxis_title(face = "bold", angle = 30),
theme(axis.title.x = element_text(face = "bold", angle = 30))
)
})
test_that("yaxis_title returns correct theme object", {
expect_identical(yaxis_title(FALSE), ylab(NULL))
expect_equal(
yaxis_title(face = "bold", angle = 30),
theme(axis.title.y = element_text(face = "bold", angle = 30))
)
})
# tick marks --------------------------------------------------
test_that("xaxis_ticks returns correct theme object", {
expect_identical(xaxis_ticks(FALSE), theme(axis.ticks.x = element_blank()))
expect_equal(
xaxis_ticks(linewidth = 0.5, color = "red"),
theme(axis.ticks.x = element_line(linewidth = 0.5, color = "red"))
)
})
test_that("yaxis_ticks returns correct theme object", {
expect_identical(yaxis_ticks(FALSE), theme(axis.ticks.y = element_blank()))
expect_equal(
yaxis_ticks(linewidth = 0.5, color = "red"),
theme(axis.ticks.y = element_line(linewidth = 0.5, color = "red"))
)
})
# overlay functions -------------------------------------------------------
test_that("overlay_function returns the correct object", {
expect_error(overlay_function(), 'argument "fun" is missing')
a <- overlay_function(fun = "dnorm")
b <- stat_function(fun = "dnorm", inherit.aes = FALSE)
a$constructor <- b$constructor <- NULL
})
|