diff --git a/.lintr b/.lintr index 8baadeba..e2cbcb0f 100755 --- a/.lintr +++ b/.lintr @@ -1,13 +1,19 @@ linters: all_linters( assignment_linter = NULL, - commas_linter(allow_trailing = TRUE), + commas_linter = NULL, consecutive_assertion_linter = NULL, duplicate_argument_linter(except = c("mutate", "transmute", "c")), + expect_identical_linter = NULL, + expect_null_linter = NULL, + expect_true_false_linter = NULL, + function_argument_linter = NULL, implicit_integer_linter = NULL, infix_spaces_linter(exclude_operators = c("/", "*")), keyword_quote_linter = NULL, line_length_linter(120L), - missing_argument_linter(except = c("alist", "quote", "switch", "pairlist2")), + missing_argument_linter(except = c("alist", "quote", "switch", "pairlist2", "lapply")), + nested_ifelse_linter = NULL, + nonportable_path_linter = NULL, numeric_leading_zero_linter = NULL, object_length_linter = NULL, object_name_linter( @@ -16,14 +22,17 @@ linters: all_linters( misc = "^(F_x|x_1_hat,x_n_hat|R_inv|Amat|Aind)$", CamelCase = "^(RankCorr.*|.*Geom.*|.*Stat.*|Scale.*|Position.*|K|Mode.*|.*Pr.*|.*linearGradient.*|\\.Deprecated.*)$", dot.case = "^(na\\.rm|na\\.translate|na\\.value|lower\\.tail|log\\.p|width\\.cutoff)$", - bandwidth = "^(bandwidth_.*)$" + bandwidth = "^(bandwidth_.*)$", + breaks = "^breaks_.*$" ) ), object_overwrite_linter( allow_names = c("dist", "data", "layout", "pdf", "q") ), + object_usage_linter = NULL, quotes_linter = NULL, scalar_in_linter = NULL, + todo_comment_linter = NULL, undesirable_function_linter(modify_defaults( defaults = all_undesirable_functions, library = NULL, diff --git a/R/geom_dotsinterval.R b/R/geom_dotsinterval.R index 401bd438..fc97c528 100755 --- a/R/geom_dotsinterval.R +++ b/R/geom_dotsinterval.R @@ -375,9 +375,7 @@ GeomDotsinterval = ggproto("GeomDotsinterval", GeomSlabinterval, aes_docs }, - hidden_aes = union(c( - "thickness" - ), GeomSlabinterval$hidden_aes), + hidden_aes = union("thickness", GeomSlabinterval$hidden_aes), default_aes = defaults(aes( family = "", diff --git a/tests/testthat.R b/tests/testthat.R index a6ec897d..0111c2ce 100755 --- a/tests/testthat.R +++ b/tests/testthat.R @@ -3,8 +3,8 @@ # # Where should you do additional test configuration? # Learn more about the roles of various files in: -# * https://r-pkgs.org/tests.html -# * https://testthat.r-lib.org/reference/test_package.html#special-files +# * https://r-pkgs.org/testing-design.html#sec-tests-files-overview +# * https://testthat.r-lib.org/articles/special-files.html library(testthat) library(ggdist) diff --git a/tests/testthat/test.binning_methods.R b/tests/testthat/test.binning_methods.R index f073d94f..163d506d 100755 --- a/tests/testthat/test.binning_methods.R +++ b/tests/testthat/test.binning_methods.R @@ -110,8 +110,8 @@ test_that("bin nudging works", { NULL ) expect_equal( - nudge_bins(c(1), width = 1), - c(1) + nudge_bins(1, width = 1), + 1 ) expect_equal( nudge_bins(c(1,2), width = 1), diff --git a/tests/testthat/test.lkj_marginal.R b/tests/testthat/test.lkj_marginal.R index bc4a180b..cfa4136b 100755 --- a/tests/testthat/test.lkj_marginal.R +++ b/tests/testthat/test.lkj_marginal.R @@ -43,10 +43,10 @@ test_that("lkjcorr_marginal throws an error for invalid K", { test_that("marginalize_lkjcorr works", { ref = as.data.frame(tibble( coef = c("a", "b"), - prior = c("lkjcorr(3)", "lkjcorr(3)" ), + prior = c("lkjcorr(3)", "lkjcorr(3)"), .dist = c("lkjcorr_marginal", "lkjcorr_marginal"), .args = list(list(2, 3), list(4, 3)), - .dist_obj = dist_wrap(dist = "lkjcorr_marginal", c(2, 4), c(3, 3), package = "ggdist"), + .dist_obj = dist_wrap(dist = "lkjcorr_marginal", c(2, 4), c(3, 3), package = "ggdist") )) expect_equal( diff --git a/tests/testthat/test.rd_lineribbon.R b/tests/testthat/test.rd_lineribbon.R index ea68a155..674d1503 100755 --- a/tests/testthat/test.rd_lineribbon.R +++ b/tests/testthat/test.rd_lineribbon.R @@ -23,7 +23,10 @@ test_that("lineribbon aesthetic documention generator works", { test_that("shortcut stat documentation generator works", { - stat_output = paste0(rd_lineribbon_shortcut_stat("lineribbon", chart_type = "multiple-ribbon", from_name = "slabinterval"), collapse = "\n") + stat_output = paste0( + rd_lineribbon_shortcut_stat("lineribbon", chart_type = "multiple-ribbon", from_name = "slabinterval"), + collapse = "\n" + ) expect_match(stat_output, "@title Multiple-ribbon plot (shortcut stat)", fixed = TRUE) expect_match(stat_output, ".width = c(0.5, 0.8, 0.95)", fixed = TRUE) diff --git a/tests/testthat/test.scale_.R b/tests/testthat/test.scale_.R index 99150dfa..15810b03 100755 --- a/tests/testthat/test.scale_.R +++ b/tests/testthat/test.scale_.R @@ -38,7 +38,7 @@ test_that("direct scale setting works", { slab_linetype = "dotted", slab_alpha = .5 ) - ) + ) }) test_that("mapping custom aesthetics works", { diff --git a/tests/testthat/test.smooth.R b/tests/testthat/test.smooth.R index a513992c..f3dc9dda 100755 --- a/tests/testthat/test.smooth.R +++ b/tests/testthat/test.smooth.R @@ -30,11 +30,19 @@ test_that("smooth_discrete works", { x = rep(1:4, times = 4:1) ld = layer_data(ggplot() + geom_dots(aes(x), smooth = "discrete")) - ref_x = c(0.760112695656659, 0.920256356040445, 1.08040001642423, 1.24054367680802, 1.78654853188863, 2.00007341240034, 2.21359829291206, 2.83977362126489, 3.16006094203247, 3.99967181376766) + ref_x = c( + 0.760112695656659, 0.920256356040445, 1.08040001642423, 1.24054367680802, + 1.78654853188863, 2.00007341240034, 2.21359829291206, 2.83977362126489, + 3.16006094203247, 3.99967181376766 + ) expect_equal(ld$x, ref_x, tolerance = 0.001) ld = layer_data(ggplot() + geom_dots(aes(x), smooth = smooth_discrete(kernel = "ep"))) - ref_x = c(0.816364320617873, 0.944625373470495, 1.05539072151476, 1.18366670884155, 1.84144365484769, 2.00000507797274, 2.15857922335003, 2.88568947241406, 3.11432520840337, 3.99999280566278) + ref_x = c( + 0.816364320617873, 0.944625373470495, 1.05539072151476, 1.18366670884155, + 1.84144365484769, 2.00000507797274, 2.15857922335003, 2.88568947241406, + 3.11432520840337, 3.99999280566278 + ) expect_equal(ld$x, ref_x, tolerance = 0.001) }) @@ -42,6 +50,10 @@ test_that("smooth_bounded works", { x = 1:10 ld = layer_data(ggplot() + geom_dots(aes(x), smooth = "bounded")) - ref_x = c(0.997794316080475, 1.99639357461413, 2.99631978598422, 3.99740164666058, 4.99908084276668, 6.00091915723332, 7.00259835333942, 8.00368021401578, 9.00360642538587, 10.0022056839195) + ref_x = c( + 0.997794316080475, 1.99639357461413, 2.99631978598422, 3.99740164666058, + 4.99908084276668, 6.00091915723332, 7.00259835333942, 8.00368021401578, + 9.00360642538587, 10.0022056839195 + ) expect_equal(ld$x, ref_x, tolerance = 0.001) }) diff --git a/tests/testthat/test.stat_cdfinterval.R b/tests/testthat/test.stat_cdfinterval.R index d62dbe77..8b4f5bdf 100755 --- a/tests/testthat/test.stat_cdfinterval.R +++ b/tests/testthat/test.stat_cdfinterval.R @@ -11,7 +11,7 @@ test_that("dodged ccdf barplots work", { skip_if_no_vdiffr() - df = data.frame(y = 1:5, x = "a", g = c("g1")) %>% + df = data.frame(y = 1:5, x = "a", g = "g1") %>% rbind(data.frame(y = rep(1:5, each = 3) + 1:3, x = "b", g = c("g1", "g2", "g3"))) p = ggplot(df, aes(x = x, y = y)) diff --git a/tests/testthat/test.stat_sample_slabinterval.R b/tests/testthat/test.stat_sample_slabinterval.R index 8613cbeb..0515c9fa 100755 --- a/tests/testthat/test.stat_sample_slabinterval.R +++ b/tests/testthat/test.stat_sample_slabinterval.R @@ -137,12 +137,22 @@ test_that("scale transformation works on halfeye", { scale_x_log10(breaks = 10^seq(-1,1)) vdiffr::expect_doppelganger("halfeyeh log scale tri", - p_log + stat_halfeye(point_interval = mode_hdci, n = 20, density = density_unbounded(kernel = "tri"), .width = .5) + + p_log + + stat_halfeye( + point_interval = mode_hdci, n = 20, + density = density_unbounded(kernel = "tri"), + .width = .5 + ) + geom_point(data = data.frame(x = 10^c(-1, 1))) ) vdiffr::expect_doppelganger("halfeyeh log scale tri no trim", - p_log + stat_halfeye(point_interval = mode_hdci, n = 20, density = density_unbounded(kernel = "tri"), trim = FALSE, .width = .5) + + p_log + + stat_halfeye( + point_interval = mode_hdci, n = 20, + density = density_unbounded(kernel = "tri"), trim = FALSE, + .width = .5 + ) + geom_point(data = data.frame(x = 10^c(-1, 1))) ) diff --git a/tests/testthat/test.student_t.R b/tests/testthat/test.student_t.R index 871abe9d..e5043d89 100755 --- a/tests/testthat/test.student_t.R +++ b/tests/testthat/test.student_t.R @@ -28,4 +28,3 @@ test_that("student_t functions work", { set.seed(1234) expect_equal(rstudent_t(10, df, median, scale), r) }) - diff --git a/tests/testthat/test.theme_ggdist.R b/tests/testthat/test.theme_ggdist.R index cf5fa9fd..477e55d9 100755 --- a/tests/testthat/test.theme_ggdist.R +++ b/tests/testthat/test.theme_ggdist.R @@ -16,7 +16,8 @@ test_that("theme helper functions work", { p = data.frame( x = 1:2, y = 0, - g = c("aaa","bbb") + g = c("aaa","bbb"), + stringsAsFactors = FALSE ) %>% ggplot(aes(x, y)) + geom_point() + diff --git a/tests/testthat/test.weighted_ecdf.R b/tests/testthat/test.weighted_ecdf.R index a1e8f9a7..5351b279 100755 --- a/tests/testthat/test.weighted_ecdf.R +++ b/tests/testthat/test.weighted_ecdf.R @@ -12,5 +12,8 @@ test_that("weighted_ecdf works", { }) test_that("quantile() works", { - expect_equal(quantile(weighted_ecdf(c(1, 2, 2, 3), 1:4), ppoints(10)), weighted_quantile(c(1, 2, 2, 3), weights = 1:4, ppoints(10))) + expect_equal( + quantile(weighted_ecdf(c(1, 2, 2, 3), 1:4), ppoints(10)), + weighted_quantile(c(1, 2, 2, 3), weights = 1:4, ppoints(10)) + ) }) diff --git a/tests/testthat/test.weighted_hist.R b/tests/testthat/test.weighted_hist.R index 235d1f3e..ea6545a0 100755 --- a/tests/testthat/test.weighted_hist.R +++ b/tests/testthat/test.weighted_hist.R @@ -75,10 +75,22 @@ test_that("align functions work", { x = c(1,2,3,4,5,6) breaks = c(0.25, 2.25, 4.25, 6.25) - expect_equal(weighted_hist(x, breaks = breaks, align = 0.25), weighted_hist(x, breaks = breaks - 0.25)) - expect_equal(weighted_hist(x, breaks = breaks, align = align_none()), weighted_hist(x, breaks = breaks)) - expect_equal(weighted_hist(x, breaks = breaks, align = align_center(at = 2)), weighted_hist(x, breaks = breaks + 0.75)) - expect_equal(weighted_hist(x, breaks = breaks, align = align_boundary(at = 2)), weighted_hist(x, breaks = breaks - 0.25)) + expect_equal( + weighted_hist(x, breaks = breaks, align = 0.25), + weighted_hist(x, breaks = breaks - 0.25) + ) + expect_equal( + weighted_hist(x, breaks = breaks, align = align_none()), + weighted_hist(x, breaks = breaks) + ) + expect_equal( + weighted_hist(x, breaks = breaks, align = align_center(at = 2)), + weighted_hist(x, breaks = breaks + 0.75) + ) + expect_equal( + weighted_hist(x, breaks = breaks, align = align_boundary(at = 2)), + weighted_hist(x, breaks = breaks - 0.25) + ) }) diff --git a/tests/testthat/test.weighted_quantile.R b/tests/testthat/test.weighted_quantile.R index 0d291b24..667d7751 100755 --- a/tests/testthat/test.weighted_quantile.R +++ b/tests/testthat/test.weighted_quantile.R @@ -15,8 +15,8 @@ test_that("weighted_quantile is equivalent to quantile on non-weighted samples", test_that("weighted_quantile is equivalent to quantile on weighted samples", { x = c(1,1,1,1,2,2,2,3,3,4) - xw = c(1:4) - w = c(4:1) + xw = 1:4 + w = 4:1 p = ppoints(20, a = 1) for (type in 1:9) { @@ -48,4 +48,3 @@ test_that("0- and 1-length vectors work", { expect_equal(weighted_quantile(numeric(), c(0, 0.5, 1, NA), names = FALSE), c(NA_real_, NA_real_, NA_real_, NA_real_)) }) - diff --git a/vignettes/dotsinterval.Rmd b/vignettes/dotsinterval.Rmd index 4cc630a2..5e65dec1 100755 --- a/vignettes/dotsinterval.Rmd +++ b/vignettes/dotsinterval.Rmd @@ -75,7 +75,7 @@ label_ = function(..., hjust = 0, color = red_) { ) } arrow_ = function(..., curvature = 0, x, xend = x, y, yend = y) { - annotate("curve", + annotate("curve", color = red_, arrow = arrow(angle = 45, length = unit(3, "points"), type = "closed"), curvature = curvature, x = x, xend = xend, y = y, yend = yend @@ -90,7 +90,7 @@ tibble(dist = dist_normal(4, 1.2)) %>% stat_dotsinterval( aes(linewidth = NULL), - slab_color = "gray50", + slab_color = "gray50", .width = 1 - 2*pnorm(-1, sd = 1.2), fill = "gray75", point_size = 5, @@ -100,53 +100,63 @@ tibble(dist = dist_normal(4, 1.2)) %>% linewidth = 5, slab_linewidth = 1.5 ) + - + # height refline_(x = 0, xend = 8.4, y = 1) + bracket_(x = 8.4, y = 0, yend = 1) + label_(label = "height", x = 8.6, y = 1) + - + # scale refline_(x = 4, xend = 8.6, y = 0.9) + bracket_(x = 8.6, y = 0, yend = 0.9) + label_(label = "scale = 0.9", x = 8.8, y = 0.9) + - + # slab line properties - label_(x = 2.5, y = 0.7, + label_(x = 2.5, y = 0.7, label = 'slab_color = "gray50"\nslab_linewidth = 1.5', vjust = 1, hjust = 1 ) + arrow_(x = 2.52, xend = 3, y = 0.67, yend = thickness_(3.1) + 0.03, curvature = -0.2) + - + # slab fill label_(x = 5.5, y = 0.7, label = 'slab_fill = fill = "gray75"\nslab_alpha = alpha = 1\nslab_shape = 21', - vjust = 1, + vjust = 1 ) + arrow_(x = 5.48, xend = 4.81, y = 0.67, yend = thickness_(3.1) + 0.01, curvature = 0.2) + - # xmin / x / xmax + # xmin, x, xmax arrow_(x = 2.65, xend = 3, y = -0.1, yend = -0.01, curvature = -0.2) + label_(x = 2.7, y = -0.1, label = "xmin", hjust = 1, vjust = 1) + arrow_(x = 4, y = -0.1, yend = -0.05) + label_(x = 4, y = -0.1, label = "x", hjust = 0.5, vjust = 1) + arrow_(x = 5.35, xend = 5, y = -0.1, yend = -0.01, curvature = 0.2) + label_(x = 5.3, y = -0.1, label = "xmax", hjust = 0, vjust = 1) + - + # interval properties label_(x = 3.5, y = -0.2, - label = 'interval_color = color = "black"\ninterval_alpha = alpha = 1\ninterval_linetype = linetype = "solid"\nlinewidth = size = 5', + label = paste0( + 'interval_color = color = "black"\n', + 'interval_alpha = alpha = 1\n', + 'interval_linetype = linetype = "solid"\n', + 'linewidth = size = 5' + ), vjust = 1, hjust = 1 ) + arrow_(x = 3.3, xend = 3.4, y = -0.18, yend = -0.015, curvature = -0.1) + # point properties label_(x = 4.5, y = -0.2, - label = 'point_fill = fill = "gray75"\npoint_color = color = "black"\npoint_alpha = alpha = 1\npoint_size = size = 5\nshape = 22\nstroke = 1.5', + label = paste0( + 'point_fill = fill = "gray75"\n', + 'point_color = color = "black"\n', + 'point_alpha = alpha = 1\n', + 'point_size = size = 5\nshape = 22\nstroke = 1.5' + ), vjust = 1, hjust = 0 ) + arrow_(x = 4.55, xend = 4.2, y = -0.18, yend = -0.03, curvature = 0.2) + - + coord_cartesian(xlim = c(-1, 10), ylim = c(-0.6, 1)) + labs(subtitle = "Properties of geom_dotsinterval", x = NULL, y = NULL) ``` @@ -171,28 +181,28 @@ tibble(dist = dist_normal(4, 1.2)) %>% linewidth = 1.5, shape = 21 ) + - + # height refline_(x = 0, xend = 8.4, y = 1) + bracket_(x = 8.4, y = 0, yend = 1) + label_(label = "height", x = 8.6, y = 1) + - + # scale refline_(x = 4, xend = 8.6, y = 0.9) + bracket_(x = 8.6, y = 0, yend = 0.9) + label_(label = "scale = 0.9", x = 8.8, y = 0.9) + - + # slab line properties - label_(x = 2.5, y = 0.7, + label_(x = 2.5, y = 0.7, label = 'color = "gray50"\nlinewidth = 1.5', vjust = 1, hjust = 1 ) + arrow_(x = 2.52, xend = 3, y = 0.67, yend = thickness_(3.1) + 0.03, curvature = -0.2) + - + # slab fill label_(x = 5.5, y = 0.7, label = 'fill = "gray75"\nalpha = 1\nshape = 21', - vjust = 1, + vjust = 1 ) + arrow_(x = 5.48, xend = 4.81, y = 0.67, yend = thickness_(3.1) + 0.01, curvature = 0.2) + @@ -218,11 +228,10 @@ Size and layout of dots in the dotplot are controlled by four parameters: ```{r layout_params, echo=FALSE, fig.height=3.7, fig.width=6} data.frame(x = c(.4, .7, .7, 1, 1, 1)) %>% -# data.frame(x = c(.4, rep(.7, 2), rep(1, 3))) %>% ggplot(aes(x = x)) + - + geom_hline(yintercept = 0:1, color = "gray95") + - + # binwidth refline_(x = seq(.25, 1.15, by = .3), y = -0.025, yend = 0.9, color = green_) + bracket_(x = .25, xend = .55, y = -0.025, color = green_) + @@ -232,12 +241,12 @@ data.frame(x = c(.4, .7, .7, 1, 1, 1)) %>% ) + geom_dots(scale = 0.9, dotsize = 1, alpha = 0.5) + - + # height refline_(x = 0, xend = 2, y = 1) + bracket_(x = 2, y = 0, yend = 1) + label_(label = "height", x = 2.05, y = 1) + - + # scale refline_(x = 0.25, xend = 2.1, y = 0.9) + bracket_(x = 2.1, y = 0, yend = 0.9) + @@ -255,7 +264,7 @@ data.frame(x = c(.4, .7, .7, 1, 1, 1)) %>% label = "dotsize = 1\n(relative to binwidth)", x = 0.85, y = -0.08, vjust = 1, hjust = 0, color = blue_ ) + - + scale_x_continuous(limits = c(-0.1, 2.35)) + scale_y_continuous(limits = c(-0.35, 1)) + coord_fixed() + @@ -836,7 +845,8 @@ data.frame( stat_slab(aes(thickness = after_stat(pdf*n)), scale = 0.7) + stat_dotsinterval(side = "bottom", scale = 0.7, slab_linewidth = NA) + scale_fill_brewer(palette = "Set2") + - ggtitle(paste0( + ggtitle( + paste0( 'stat_slab(aes(thickness = after_stat(pdf*n)), scale = 0.7) +\n', 'stat_dotsinterval(side = "bottom", scale = 0.7, slab_linewidth = NA)' ), @@ -865,7 +875,7 @@ gentoo %>% ggtitle( "geom_dots(scale = 0.5)", 'aes(side = sex) + scale_side_mirrored()' - ) + ) ``` This can also be accomplished by setting side directly and omitting @@ -883,7 +893,7 @@ on top of the mirrored dotplots to create a *logit dotplot*: ```{r logit_dotplot, fig.width = med_width, fig.height = med_height/1.5} # construct a prediction grid for the fit line -prediction_grid = with(gentoo, +prediction_grid = with(gentoo, data.frame(body_mass_g = seq(min(body_mass_g), max(body_mass_g), length.out = 100)) ) @@ -913,7 +923,6 @@ prediction_grid %>% x = "Body mass (g) of Gentoo penguins", y = "Pr(sex = male)" ) - ``` diff --git a/vignettes/freq-uncertainty-vis.Rmd b/vignettes/freq-uncertainty-vis.Rmd index 9e88cdbb..2ca690af 100644 --- a/vignettes/freq-uncertainty-vis.Rmd +++ b/vignettes/freq-uncertainty-vis.Rmd @@ -134,7 +134,7 @@ ABC %>% augment(m_ABC, newdata = ., se_fit = TRUE) %>% ggplot(aes(y = condition)) + stat_halfeye( - aes(xdist = dist_student_t(df = df.residual(m_ABC), mu = .fitted, sigma = .se.fit)), + aes(xdist = dist_student_t(df = df.residual(m_ABC), mu = .fitted, sigma = .se.fit)), scale = .5 ) + # we'll add the data back in too (scale = .5 above adjusts the halfeye height so @@ -150,7 +150,7 @@ ABC %>% augment(m_ABC, newdata = ., se_fit = TRUE) %>% ggplot(aes(y = condition)) + stat_gradientinterval( - aes(xdist = dist_student_t(df = df.residual(m_ABC), mu = .fitted, sigma = .se.fit)), + aes(xdist = dist_student_t(df = df.residual(m_ABC), mu = .fitted, sigma = .se.fit)), scale = .5, fill_type = "gradient" ) ``` @@ -207,7 +207,6 @@ mtcars %>% alpha = 1/4 ) + geom_point(aes(y = mpg), data = mtcars) + - scale_fill_brewer(palette = "Set2") + scale_color_brewer(palette = "Dark2") + labs( @@ -236,7 +235,6 @@ mtcars %>% fill_ramp = after_stat(level) )) + geom_point(aes(y = mpg), data = mtcars) + - scale_fill_brewer(palette = "Set2") + scale_color_brewer(palette = "Dark2") + labs( diff --git a/vignettes/lineribbon.Rmd b/vignettes/lineribbon.Rmd index 13f5201b..361d2c23 100644 --- a/vignettes/lineribbon.Rmd +++ b/vignettes/lineribbon.Rmd @@ -105,7 +105,7 @@ df %>% group_by(x) %>% median_qi(y) %>% ggplot(aes(x = x, y = y, ymin = .lower, ymax = .upper)) + - geom_lineribbon(fill = "gray65") + geom_lineribbon(fill = "gray65") ``` `geom_lineribbon()` automatically pulls in the `.width` column and maps it onto the @@ -125,7 +125,7 @@ df %>% median_qi(y, .width = c(.50, .80, .95)) %>% ggplot(aes(x = x, y = y, ymin = .lower, ymax = .upper)) + geom_lineribbon() + - scale_fill_brewer() + scale_fill_brewer() ``` ## Lineribbons on sample data @@ -331,7 +331,7 @@ variable `y` that is normally distributed conditional on `x` with mean `y_mean` analytical_df = tibble( x = -4:5, y_mean = 3 + x, - y_sd = sqrt(x^2/10 + 1), + y_sd = sqrt(x^2/10 + 1) ) analytical_df ``` @@ -364,15 +364,15 @@ k = 11 # number of curves n = 501 df = tibble( .draw = 1:k, - mean = seq(-5,5, length.out = k), - x = list(seq(-15,15,length.out = n)), + mean = seq(-5, 5, length.out = k), + x = list(seq(-15, 15, length.out = n)) ) %>% unnest(x) %>% mutate(y = dnorm(x, mean, 3)/max(dnorm(x, mean, 3))) df %>% ggplot(aes(x = x, y = y)) + - geom_line(aes(group = .draw), alpha=0.2) + geom_line(aes(group = .draw), alpha = 0.2) ``` If one used one of the `point_interval()` functions to summarize this curve (such as `median_qi()`, `mean_qi()`, etc), it would calculate *pointwise* intervals: @@ -383,7 +383,7 @@ df %>% median_qi(y, .width = .5) %>% ggplot(aes(x = x, y = y)) + geom_lineribbon(aes(ymin = .lower, ymax = .upper)) + - geom_line(aes(group = .draw), alpha=0.15, data = df) + + geom_line(aes(group = .draw), alpha = 0.15, data = df) + scale_fill_brewer() + ggtitle("50% pointwise intervals with point_interval()") ``` @@ -400,7 +400,7 @@ df %>% curve_interval(y, .width = .5) %>% ggplot(aes(x = x, y = y)) + geom_lineribbon(aes(ymin = .lower, ymax = .upper)) + - geom_line(aes(group = .draw), alpha=0.15, data = df) + + geom_line(aes(group = .draw), alpha = 0.15, data = df) + scale_fill_brewer() + ggtitle("50% curvewise intervals with curve_interval()") ``` @@ -416,7 +416,7 @@ k = 1000 # number of curves large_df = tibble( .draw = 1:k, mean = seq(-5,5, length.out = k), - x = list(seq(-15,15,length.out = n)), + x = list(seq(-15,15,length.out = n)) ) %>% unnest(x) %>% mutate(y = dnorm(x, mean, 3)/max(dnorm(x, mean, 3))) @@ -460,10 +460,10 @@ mpg = seq(min(mtcars$mpg), max(mtcars$mpg), length.out = 100) mtcars_boot = tibble( .draw = 1:n, m = map(.draw, ~ loess( - hp ~ mpg, + hp ~ mpg, span = 0.9, # this lets us predict outside the range of the data - control = loess.control(surface = "direct"), + control = loess.control(surface = "direct"), data = slice_sample(mtcars, prop = 1, replace = TRUE) )), hp = map(m, predict, newdata = tibble(mpg)), diff --git a/vignettes/slabinterval.Rmd b/vignettes/slabinterval.Rmd index 95f4c940..25c3730a 100644 --- a/vignettes/slabinterval.Rmd +++ b/vignettes/slabinterval.Rmd @@ -46,10 +46,10 @@ theme_set(theme_ggdist()) dists_df = tibble( # enforce order geom = rev(c( - "halfeye", + "halfeye", "eye", - "gradientinterval", - "ccdfinterval", + "gradientinterval", + "ccdfinterval", "cdfinterval", "interval", "pointinterval", @@ -127,8 +127,10 @@ dists_plot = dists_df %>% stat_gradientinterval(data = dists_df_("gradientinterval"), scale = .5, fill_type = "gradient") + stat_ccdfinterval(data = dists_df_("ccdfinterval"), scale = .5) + stat_cdfinterval(data = dists_df_("cdfinterval"), scale = .5) + - stat_interval(data = dists_df_("interval"), color = "gray65", alpha = 1/3, linewidth = 10, - position = position_nudge(y = -.1)) + + stat_interval( + data = dists_df_("interval"), color = "gray65", alpha = 1/3, linewidth = 10, + position = position_nudge(y = -.1) + ) + stat_pointinterval(data = dists_df_("pointinterval")) + stat_slab(data = dists_df_("slab"), position = position_nudge(y = - 0.2)) + stat_histinterval(aes(x = x, xdist = NULL), data = hist_df, position = position_nudge(y = - 0.25)) + diff --git a/vignettes/thickness.Rmd b/vignettes/thickness.Rmd index 4e36575d..5a7745aa 100755 --- a/vignettes/thickness.Rmd +++ b/vignettes/thickness.Rmd @@ -75,7 +75,7 @@ df %>% facet_grid(cols = vars(panel), labeller = "label_both") + labs("geom_slab() with default thickness scaling") + theme( - legend.position = "bottom", + legend.position = "bottom", panel.grid.major.y = element_line(color = "gray85"), panel.background = element_rect(color = "gray70", fill = NA) ) @@ -110,8 +110,8 @@ of the default normalization settings: ```{r normalize_all} subguide_orangered = subguide_outside( - title = "thickness", - position = "right", + title = "thickness", + position = "right", theme = theme_ggdist() + theme( text = element_text(color = "orangered"), axis.line.y = element_line(color = "orangered"), @@ -122,32 +122,32 @@ subguide_orangered = subguide_outside( plot_slabs_with_scales = function(..., subguide = subguide_orangered) { df %>% - ggplot(aes(x = x, y = y, fill = group)) + - geom_hline(yintercept = c(1,1.9, 2,2.9), color = "orangered", linetype = "11", linewidth = 0.5) + - geom_slab( - aes(thickness = h), - subguide = subguide, - alpha = 0.75, - color = "gray25", - ... - ) + - geom_label( - aes(label = name), - data = df_group, - color = "gray25", - alpha = 0.75, - vjust = 0, - show.legend = FALSE - ) + - scale_y_discrete(expand = expansion(add = 0.1)) + - scale_fill_brewer(palette = "Set2") + - facet_grid(cols = vars(panel), labeller = "label_both") + - theme( - plot.margin = margin(5.5, 50, 5.5, 5.5), - panel.spacing.x = unit(40, "pt"), - legend.position = "bottom", - panel.background = element_rect(color = "gray70", fill = NA) - ) + ggplot(aes(x = x, y = y, fill = group)) + + geom_hline(yintercept = c(1,1.9, 2,2.9), color = "orangered", linetype = "11", linewidth = 0.5) + + geom_slab( + aes(thickness = h), + subguide = subguide, + alpha = 0.75, + color = "gray25", + ... + ) + + geom_label( + aes(label = name), + data = df_group, + color = "gray25", + alpha = 0.75, + vjust = 0, + show.legend = FALSE + ) + + scale_y_discrete(expand = expansion(add = 0.1)) + + scale_fill_brewer(palette = "Set2") + + facet_grid(cols = vars(panel), labeller = "label_both") + + theme( + plot.margin = margin(5.5, 50, 5.5, 5.5), + panel.spacing.x = unit(40, "pt"), + legend.position = "bottom", + panel.background = element_rect(color = "gray70", fill = NA) + ) } plot_slabs_with_scales(normalize = "all") + @@ -247,20 +247,20 @@ thickness scales annotated: ```{r prior_post} df_prior_post = data.frame( - prior = dist_normal(0, 1), - posterior = dist_normal(0.1, 0.3) + prior = dist_normal(0, 1), + posterior = dist_normal(0.1, 0.3) ) prior_post_plot = df_prior_post %>% ggplot() + stat_slab( - aes(xdist = posterior), + aes(xdist = posterior), subguide = subguide_inside(title = "posterior thickness") ) + stat_slab( - aes(xdist = prior), - color = "orangered", - fill = NA, + aes(xdist = prior), + color = "orangered", + fill = NA, subguide = subguide_orangered(title = "prior thickness", just = 0, label_side = "inside") ) + scale_y_continuous(breaks = NULL) + @@ -304,20 +304,20 @@ scale_plot = function(scale = 0.9) { data.frame(d = dist_normal(c(4,5)), y = c("a","b")) %>% ggplot(aes(xdist = d, y = y)) + geom_hline( - yintercept = c(1, 1 + scale, 2, 2 + scale), + yintercept = c(1, 1 + scale, 2, 2 + scale), color = "orangered", linetype = "11", linewidth = 0.5 ) + stat_slab(scale = scale, subguide = subguide_orangered) + - annotate("segment", x = c(-5,-4), xend = c(-5,-4), y = c(1,2), yend = c(2,3), + annotate("segment", x = c(-5,-4), xend = c(-5,-4), y = c(1,2), yend = c(2,3), arrow = cap, linewidth = 0.75, color = "gray25" ) + - annotate("label", x = c(-5,-4), y = c(1.5, 2.5), + annotate("label", x = c(-5,-4), y = c(1.5, 2.5), label = "height = 1", hjust = -0.05, color = "gray25" ) + - annotate("segment", x = c(-2,-1), xend = c(-2,-1), y = c(1,2), yend = c(1,2) + scale, + annotate("segment", x = c(-2,-1), xend = c(-2,-1), y = c(1,2), yend = c(1,2) + scale, arrow = cap, linewidth = 0.75, color = "gray25" ) + - annotate("label", x = c(-2,-1), y = c(1,2) + scale/2, + annotate("label", x = c(-2,-1), y = c(1,2) + scale/2, label = paste("scale =", scale), hjust = -0.05, color = "gray25" ) + scale_y_discrete(expand = expansion(add = 0)) + @@ -333,7 +333,7 @@ scale_plot(scale = 0.9) + labs( title = "geom_slab()", subtitle = "using default height = 1 and scale = 0.9" - ) + ) ``` This allows us to adjust the spacing between slabs using `scale`: @@ -356,38 +356,38 @@ and modifying `scale` allows you to change the spacing **within** sets: dodged_scale_plot = function(height = 1, scale = 0.9, add = 0) { baselines = c(1 - height/2, 1, 2 - height/2, 2) data.frame( - d = dist_normal(c(4,5,4,5)), - y = c("a","a","b","b"), + d = dist_normal(c(4,5,4,5)), + y = c("a","a","b","b"), group = c("d","e","d","e") ) %>% ggplot(aes(xdist = d, y = y, fill = group)) + geom_hline(yintercept = c(1,2) + height/2, color = "gray85") + geom_hline( - yintercept = c(baselines, baselines + height/2*scale), - color = "orangered", - linetype = "11", + yintercept = c(baselines, baselines + height/2*scale), + color = "orangered", + linetype = "11", linewidth = 0.5 ) + stat_slab( - height = height, scale = scale, + height = height, scale = scale, subguide = subguide_orangered, position = "dodgejust", alpha = 0.75 ) + - annotate("segment", - x = c(-5,-4), xend = c(-5,-4), - y = c(1,2) - height/2, yend = c(1,2) + height/2, + annotate("segment", + x = c(-5,-4), xend = c(-5,-4), + y = c(1,2) - height/2, yend = c(1,2) + height/2, arrow = cap, color = "gray25", linewidth = 0.75 ) + - annotate("label", x = c(-5,-4), y = c(1, 2), + annotate("label", x = c(-5,-4), y = c(1, 2), label = paste("height =", height), hjust = -0.05, color = "gray25" ) + - annotate("segment", x = c(-2,-2,-1,-1), xend = c(-2,-2,-1,-1), - y = baselines, yend = baselines + scale * height/2, + annotate("segment", x = c(-2,-2,-1,-1), xend = c(-2,-2,-1,-1), + y = baselines, yend = baselines + scale * height/2, arrow = cap, color = "gray25", linewidth = 0.75 ) + - annotate("label", x = c(-2,-2,-1,-1), - y = baselines + scale*height/4, label = paste("scale =", scale), + annotate("label", x = c(-2,-2,-1,-1), + y = baselines + scale*height/4, label = paste("scale =", scale), hjust = -0.05, color = "gray25" ) + scale_y_discrete(expand = expansion(add = add)) +