From 80724933663c0269e5a9c2e4788ca1fdbfc1ab6a Mon Sep 17 00:00:00 2001 From: Joseph Date: Mon, 25 Oct 2021 17:01:01 +0200 Subject: [PATCH] Fix facets ordering in `ggcoef_compare()` (#427) Co-authored-by: Joseph --- NEWS.md | 1 + R/ggcoef_model.R | 3 ++- tests/testthat/test-ggcoef_model.R | 12 ++++++++++++ 3 files changed, 15 insertions(+), 1 deletion(-) diff --git a/NEWS.md b/NEWS.md index ca77a9996..3d71ddf61 100644 --- a/NEWS.md +++ b/NEWS.md @@ -3,6 +3,7 @@ ### Bug fixes * Reverse ordering of y-axis in `ggally_count()` (#420) +* Facets ordering in `ggcoef_compare()` (#426) # GGally 2.1.2 diff --git a/R/ggcoef_model.R b/R/ggcoef_model.R index d370020a8..2156e21fc 100644 --- a/R/ggcoef_model.R +++ b/R/ggcoef_model.R @@ -306,7 +306,7 @@ ggcoef_compare <- function ( tidyr::complete( .data$model, tidyr::nesting( - !!sym("variable"), !!sym("var_label"), !!sym("var_class"), + !!sym("var_label"), !!sym("variable"), !!sym("var_class"), !!sym("var_type"), !!sym("contrasts"), !!sym("reference_row"), !!sym("label"), !!sym("label_light") ) @@ -520,6 +520,7 @@ ggcoef_data <- function ( data <- data[!is.na(data$estimate), ] data$var_label <- forcats::fct_inorder(data$var_label) + data$variable <- forcats::fct_inorder(data$variable) data$label <- forcats::fct_inorder(data$label) data$label_light <- dplyr::if_else( diff --git a/tests/testthat/test-ggcoef_model.R b/tests/testthat/test-ggcoef_model.R index 0096bc858..07817f0c7 100644 --- a/tests/testthat/test-ggcoef_model.R +++ b/tests/testthat/test-ggcoef_model.R @@ -152,3 +152,15 @@ test_that("ggcoef_model works with tieders not returning p-values", { ) }) + +test_that("ggcoef_compare complete missing data by respecting the order if variables", { + m1 <- lm(Fertility ~ Education + Catholic, data = swiss) + m2 <- lm(Fertility ~ Education + Catholic + Agriculture, data = swiss) + m3 <- lm(Fertility ~ Education + Catholic + Agriculture + Infant.Mortality, data = swiss) + res <- ggcoef_compare(models = list(m1, m2, m3), return_data = TRUE) + expect_equal( + res$variable[1:4], + structure(1:4, .Label = c("Education", "Catholic", "Agriculture", + "Infant.Mortality"), class = "factor") + ) +})