diff --git a/R/position-nudge-stack.R b/R/position-nudge-stack.R index c0bff20..ee0447a 100644 --- a/R/position-nudge-stack.R +++ b/R/position-nudge-stack.R @@ -124,9 +124,9 @@ #' #' ggplot(birch_dw.df, #' aes(y = dry.weight * 1e-3, x = Density, fill = Part)) + -#' stat_summary(geom = "col", fun = mean, na.rm = TRUE, +#' stat_summary(geom = "col", fun = mean, #' position = "stack", alpha = 0.7, width = 0.67) + -#' stat_summary(geom = "linerange", fun.data = mean_cl_normal, na.rm = TRUE, +#' stat_summary(geom = "linerange", fun.data = mean_cl_normal, #' position = position_stack_minmax()) + #' labs(y = "Seedling dry mass (g)") + #' scale_fill_grey(start = 0.7, end = 0.3) + @@ -134,10 +134,15 @@ #' #' ggplot(birch_dw.df, #' aes(y = dry.weight * 1e-3, x = Density, fill = Part)) + -#' stat_summary(geom = "col", fun = mean, na.rm = TRUE, -#' position = "stack", alpha = 0.7, width = 0.67) + -#' stat_summary(geom = "linerange", fun.data = mean_cl_normal, na.rm = TRUE, -#' position = position_stack_minmax(x = -0.1)) + +#' stat_summary(geom = "col", fun = mean, +#' position = "stack", alpha = 0.7, width = 0.67) + +#' # error bars for each stack bar +#' stat_summary(geom = "linerange", fun.data = mean_cl_normal, +#' position = position_stack_minmax(x = -0.1)) + +#' # error bar for the total +#' stat_summary(data = birch.df, aes(y = (dwstem + dwroot) * 1e-3, fill = NULL), +#' geom = "linerange", linewidth = 0.75, +#' position = position_nudge(x = 0.1), fun.data = mean_cl_normal) + #' labs(y = "Seedling dry mass (g)") + #' scale_fill_grey(start = 0.7, end = 0.3) + #' facet_wrap(facets = vars(Container)) @@ -197,16 +202,12 @@ position_fillnudge <- none = function(x) {1}, split = sign, split.y = function(x) {1}, - split.x = sign, - center = sign, - function(x) {1}), + split.x = sign), .fun_y = switch(direction, none = function(x) {1}, split = sign, split.x = function(x) {1}, - split.y = sign, - center = sign, - function(x) {1}), + split.y = sign), kept.origin = kept.origin, vjust = vjust, reverse = reverse @@ -448,16 +449,12 @@ position_stack_minmax <- none = function(x) {1}, split = sign, split.y = function(x) {1}, - split.x = sign, - center = sign, - function(x) {1}), + split.x = sign), .fun_y = switch(direction, none = function(x) {1}, split = sign, split.x = function(x) {1}, - split.y = sign, - center = sign, - function(x) {1}), + split.y = sign), kept.origin = kept.origin, vjust = vjust, reverse = reverse diff --git a/tests/testthat/test-position_fillnudge.R b/tests/testthat/test-position_fillnudge.R index abe9a0d..ad75366 100644 --- a/tests/testthat/test-position_fillnudge.R +++ b/tests/testthat/test-position_fillnudge.R @@ -2,14 +2,47 @@ context("position_fillnudge") test_that("incorrect kept.origin used", { expect_error( - position_fillnudge(kept.origin = "", vjust = 0.5, x = -0.3) #, + position_fillnudge(kept.origin = "") #, # "`kept.origin` must be one of \"stacked\", \"original\", or \"none\", not \"\"." # rlang arg_match() adds to the error message characters that break expect_error() ) }) test_that("correct kept.origin used", { - position <- position_fillnudge(kept.origin = "none", vjust = 0.5, x = -0.3) + position <- position_fillnudge(kept.origin = "none") + expect_no_error(position) +}) + +test_that("correct kept.origin used", { + position <- position_fillnudge(kept.origin = "original") + expect_no_error(position) +}) + +test_that("incorrect direction used", { + expect_error( + position_stack_minmax(direction = "") #, + # "`kept.origin` must be one of \"stacked\", \"original\", or \"none\", not \"\"." + # rlang arg_match() adds to the error message characters that break expect_error() + ) +}) + +test_that("correct direction used", { + position <- position_stack_minmax(direction = "none") + expect_no_error(position) +}) + +test_that("correct direction used", { + position <- position_stack_minmax(direction = "split") + expect_no_error(position) +}) + +test_that("correct direction used", { + position <- position_stack_minmax(direction = "split.x") + expect_no_error(position) +}) + +test_that("correct direction used", { + position <- position_stack_minmax(direction = "split.y") expect_no_error(position) }) @@ -17,6 +50,7 @@ test_that("correct reverse, vjust and x assigned", { position <- position_fillnudge(kept.origin = "none", vjust = 0.5, x = -0.3) expect_false(position$reverse) expect_identical(position$vjust, 0.5) + expect_identical(position$y, 0) expect_identical(position$x, -0.3) expect_identical(position$fill, TRUE) }) diff --git a/tests/testthat/test-position_stack_minmax.R b/tests/testthat/test-position_stack_minmax.R index 5298586..37fcd71 100644 --- a/tests/testthat/test-position_stack_minmax.R +++ b/tests/testthat/test-position_stack_minmax.R @@ -1,9 +1,57 @@ context("position_stack_minmax") +test_that("incorrect kept.origin used", { + expect_error( + position_stack_minmax(kept.origin = "") #, + # "`kept.origin` must be one of \"stacked\", \"original\", or \"none\", not \"\"." + # rlang arg_match() adds to the error message characters that break expect_error() + ) +}) + +test_that("correct kept.origin used", { + position <- position_stack_minmax(kept.origin = "none") + expect_no_error(position) +}) + +test_that("correct kept.origin used", { + position <- position_stack_minmax(kept.origin = "original") + expect_no_error(position) +}) + +test_that("incorrect direction used", { + expect_error( + position_stack_minmax(direction = "") #, + # "`kept.origin` must be one of \"stacked\", \"original\", or \"none\", not \"\"." + # rlang arg_match() adds to the error message characters that break expect_error() + ) +}) + +test_that("correct direction used", { + position <- position_stack_minmax(direction = "none") + expect_no_error(position) +}) + +test_that("correct direction used", { + position <- position_stack_minmax(direction = "split") + expect_no_error(position) +}) + +test_that("correct direction used", { + position <- position_stack_minmax(direction = "split.x") + expect_no_error(position) +}) + +test_that("correct direction used", { + position <- position_stack_minmax(direction = "split.y") + expect_no_error(position) +}) + test_that("test vjust, reverse, x, y and kept.origin arguments", { position <- position_stack_minmax() expect_identical(position$vjust, 1) expect_identical(position$reverse, FALSE) +expect_identical(position$x, 0) +expect_identical(position$y, 0) expect_identical(position$kept.origin, "stacked") expect_identical(position$var, "y") expect_identical(position$fill, FALSE)