From 83503901981ac6f69fc4f373aa1492906edaa92c Mon Sep 17 00:00:00 2001 From: Daniel Sabanes Bove Date: Thu, 17 Nov 2022 16:31:50 +0100 Subject: [PATCH 1/3] start with test --- tests/testthat/test-stat_quadrant_counts.R | 25 ++++++++++++++++++++++ 1 file changed, 25 insertions(+) create mode 100644 tests/testthat/test-stat_quadrant_counts.R diff --git a/tests/testthat/test-stat_quadrant_counts.R b/tests/testthat/test-stat_quadrant_counts.R new file mode 100644 index 0000000..5fdc372 --- /dev/null +++ b/tests/testthat/test-stat_quadrant_counts.R @@ -0,0 +1,25 @@ +context("stat_quadrant_counts") + +library(ggplot2) +library(tibble) + +make_data_tbl <- function(nrow = 100, rfun = rnorm, ...) { + if (nrow %% 2) { + nrow <- nrow + 1 + } + + set.seed(1001) + + tibble::tibble( + x = rfun(nrow, ...), + y = rfun(nrow, ...), + group = rep(c("A", "B"), c(nrow / 2, nrow / 2)) + ) +} + +test_that("stat_quadrant_counts", { + p <- ggplot(make_data_tbl(6), aes(x, y)) + + geom_point() + + stat_quadrant_counts() + result <- layer_data(p, i = 2) # 2nd layer returns the quadrant statistics. +}) From ed8b743f1eb5a8422326d9407e9c965c1d4727f1 Mon Sep 17 00:00:00 2001 From: Daniel Sabanes Bove Date: Fri, 18 Nov 2022 15:48:37 +0100 Subject: [PATCH 2/3] complete test --- tests/testthat/test-stat_quadrant_counts.R | 8 +++++++- 1 file changed, 7 insertions(+), 1 deletion(-) diff --git a/tests/testthat/test-stat_quadrant_counts.R b/tests/testthat/test-stat_quadrant_counts.R index 5fdc372..36e4ae9 100644 --- a/tests/testthat/test-stat_quadrant_counts.R +++ b/tests/testthat/test-stat_quadrant_counts.R @@ -21,5 +21,11 @@ test_that("stat_quadrant_counts", { p <- ggplot(make_data_tbl(6), aes(x, y)) + geom_point() + stat_quadrant_counts() - result <- layer_data(p, i = 2) # 2nd layer returns the quadrant statistics. + result <- layer_data(p, i = 2)[, c("npcx", "npcy", "label")] # 2nd layer returns the quadrant statistics. + expected <- data.frame( + npcx = c(0.95, 0.05, 0.05, 0.95), + npcy = c(0.95, 0.05, 0.95, 0.05), + label = c("n=1", "n=3", "n=2", "n=0") + ) + expect_identical(result, expected) }) From 3fdecfe7b7b6cea701dfed066887bedc23b58175 Mon Sep 17 00:00:00 2001 From: Daniel Sabanes Bove Date: Mon, 21 Nov 2022 13:59:55 +0100 Subject: [PATCH 3/3] simplify test --- tests/testthat/test-stat_quadrant_counts.R | 25 ++++++---------------- 1 file changed, 7 insertions(+), 18 deletions(-) diff --git a/tests/testthat/test-stat_quadrant_counts.R b/tests/testthat/test-stat_quadrant_counts.R index 36e4ae9..babc707 100644 --- a/tests/testthat/test-stat_quadrant_counts.R +++ b/tests/testthat/test-stat_quadrant_counts.R @@ -1,27 +1,16 @@ context("stat_quadrant_counts") library(ggplot2) -library(tibble) - -make_data_tbl <- function(nrow = 100, rfun = rnorm, ...) { - if (nrow %% 2) { - nrow <- nrow + 1 - } - - set.seed(1001) - - tibble::tibble( - x = rfun(nrow, ...), - y = rfun(nrow, ...), - group = rep(c("A", "B"), c(nrow / 2, nrow / 2)) - ) -} test_that("stat_quadrant_counts", { - p <- ggplot(make_data_tbl(6), aes(x, y)) + - geom_point() + + df <- data.frame( + x = c(2.2, -0.2, -0.2, -2.5, -0.6, -0.1), + y = c(1.1, -0.6, -0.9, -1.6, 0.3, 1.6), + group = c("A", "A", "A", "B", "B", "B") + ) + p <- ggplot(df, aes(x, y)) + stat_quadrant_counts() - result <- layer_data(p, i = 2)[, c("npcx", "npcy", "label")] # 2nd layer returns the quadrant statistics. + result <- layer_data(p)[, c("npcx", "npcy", "label")] expected <- data.frame( npcx = c(0.95, 0.05, 0.05, 0.95), npcy = c(0.95, 0.05, 0.95, 0.05),