From 6e08e8ec52f8399ef1b14d0396b2e5543cdba866 Mon Sep 17 00:00:00 2001 From: Joran Elias Date: Tue, 6 Feb 2024 15:09:01 -0700 Subject: [PATCH] add two tests, one verifying the unweighted density and one verifying the weighted density --- tests/testthat/test_stat_density_ridges.R | 20 ++++++++++++++++++++ 1 file changed, 20 insertions(+) diff --git a/tests/testthat/test_stat_density_ridges.R b/tests/testthat/test_stat_density_ridges.R index c3dd623..f514096 100644 --- a/tests/testthat/test_stat_density_ridges.R +++ b/tests/testthat/test_stat_density_ridges.R @@ -110,3 +110,23 @@ test_that("alternative quantile function can be provided", { expect_setequal(out$datatype, c("ridgeline", "vline")) expect_equal(out$x[out$datatype=="vline"], mean(df$x)) }) + +test_that("unweighted densities are calculated correctly", { + df <- data.frame(x = rnorm(100),wts = runif(100)) + df$wts <- df$wts / sum(df$wts) + + gg_no_wts <- layer_data(ggplot(df, aes(x = x, y = 0)) + stat_density_ridges()) + d_no_wts <- stats::density(df$x) + + expect_equal(gg_no_wts$density,d_no_wts$y) +}) + +test_that("weighted densities are calculated correctly", { + df <- data.frame(x = rnorm(100),wts = runif(100)) + df$wts <- df$wts / sum(df$wts) + + gg_wts <- layer_data(ggplot(df, aes(x = x, y = 0, weight = wts)) + stat_density_ridges()) + d_wts <- stats::density(df$x,weights = df$wts) + + expect_equal(gg_wts$density,d_wts$y) +})