Skip to content

Fix aes_string() deprecation warning in diagnostics plots (#582) - #584

Draft
kalra-mohit wants to merge 1 commit into
ModelOriented:masterfrom
kalra-mohit:fix/582-aes-string-deprecation
Draft

Fix aes_string() deprecation warning in diagnostics plots (#582)#584
kalra-mohit wants to merge 1 commit into
ModelOriented:masterfrom
kalra-mohit:fix/582-aes-string-deprecation

Conversation

@kalra-mohit

Copy link
Copy Markdown

Summary

ggplot2 >= 3.0.0 hard-deprecates aes_string(). Both plot.model_diagnostics() and plot.predict_diagnostics() called it directly, so every diagnostics plot emitted a lifecycle deprecation warning:

Warning message:
`aes_string()` was deprecated in ggplot2 3.0.0.
ℹ Please use tidy evaluation idioms with `aes()`.

Reproduced exactly using the reprex from #582:

library(DALEX)
library(randomForest)
set.seed(72)

model_apart_rf <- randomForest(m2.price ~ ., data = apartments)
explain_apart_rf <- DALEX::explain(
  model = model_apart_rf, verbose = FALSE,
  data = apartments_test[, -1], y = apartments_test$m2.price,
  label = "Random Forest"
)

diag_rf <- model_diagnostics(explain_apart_rf)
plot(diag_rf)  # <- warning fired here, pre-fix

Root cause / fix: both functions built their aes() mapping with variable-name strings (column names passed in as function arguments, e.g. variable = "y_hat"), which is exactly what aes_string() was for before tidy evaluation existed. The fix swaps in aes() + .data[[...]], which is the direct replacement ggplot2's own deprecation message points to, and keeps the "map by column-name string" behavior these functions depend on (variable/yvariable are user-facing string parameters).

While tracing this, I found plot.predict_diagnostics()'s histogram branch (used when variables = NULL) has the exact same latent bug — not mentioned in the issue title, but same root cause and same file area, so fixed it too rather than leaving a near-identical bug in place.

flowchart LR
    A["model_diagnostics(explainer)"] --> B["plot.model_diagnostics()"]
    B --> C["ggplot(all_models, aes_string(...))\n⚠ deprecated in ggplot2 >= 3.0.0"]
    C -->|"this PR"| D["ggplot(all_models, aes(x = .data[[variable]], ...))"]

    E["predict_diagnostics(explainer, ..., variables = NULL)"] --> F["plot.predict_diagnostics()\n(histogram branch)"]
    F --> G["ggplot(df, aes_string(...))\n⚠ same deprecation"]
    G -->|"this PR"| H["ggplot(df, aes(x = .data[[\"Var1\"]], ...))"]
Loading

Why it matters

model_diagnostics()/plot() and predict_diagnostics()/plot() are core, commonly-used entry points (residual diagnostics, instance-level diagnostics). The warning fires on every single call, which is noisy for users and — since lifecycle warnings can be promoted to errors by options(lifecycle_verbosity = "error") or under strict CRAN checks on newer ggplot2 — a forward-compatibility risk, not just cosmetic noise.

Test plan

  • Reproduced the exact warning from plot.model_diagnostics() gives a warning for using deprecated aes_string() with ggplot2 >=3.0.0 #582's reprex against current master.
  • Added a regression test to tests/testthat/test_model_diagnostics.R and tests/testthat/test_predict_diagnostics.R that asserts the plotting functions' source no longer references aes_string().
    • Checked this structurally (source inspection) rather than via expect_warning/expect_no_warning, because lifecycle deprecation warnings are only emitted once per R session — whichever test calls plot.model_diagnostics() first "uses up" the warning, so a warning-capturing test would silently pass on later calls in the same test run regardless of whether the underlying bug is fixed. Confirmed this empirically before choosing the structural check.
  • Verified both new tests fail against the pre-fix source (failed: 1) and pass after the fix (failed: 0), by checking out the pre-fix version of the two R/ files, rebuilding, and rerunning testthat::test_file() on just the two affected test files, then restoring the fix and rerunning.
  • Ran the full existing test_model_diagnostics.R and test_predict_diagnostics.R suites (not just the new tests) after the fix — all pass, and the pre-existing "Plot" tests no longer emit the aes_string() warning either.
  • Verified the fixed plots produce identical data mappings (gg object, correct columns bound to x/y/color/fill) — this is a call-site swap, not a behavior change.

Ran only the two affected test files locally (per this session's scoped-testing convention); full R CMD check / CI will run the complete suite.

Closes #582

…ted#582)

ggplot2 >= 3.0.0 hard-deprecates aes_string(), which plot.model_diagnostics()
and plot.predict_diagnostics() both called directly, causing every diagnostics
plot to emit a lifecycle deprecation warning. Replaced with tidy evaluation
(aes() + .data[[...]]), the idiom ggplot2's own deprecation message recommends.

plot.predict_diagnostics() had the same latent issue in its histogram branch
(variables = NULL), even though it wasn't called out in the issue title.

Added regression tests to test_model_diagnostics.R and
test_predict_diagnostics.R that assert the plotting functions' source no
longer references aes_string() (checked structurally rather than by
capturing the warning at call time, since lifecycle warnings are only
emitted once per R session and a warning-capturing test would silently
pass on subsequent calls regardless of whether the bug is fixed).

Verified: both new tests fail against the pre-fix source and pass after
the fix; existing test_model_diagnostics.R / test_predict_diagnostics.R
suites still pass in full with no aes_string warnings.

Closes ModelOriented#582

Co-Authored-By: Claude Sonnet 5 <noreply@anthropic.com>
nlabels <- length(unique(all_models$label))

pl <- ggplot(all_models, aes_string(x = variable, y = yvariable, color = "label", group = "label")) +
pl <- ggplot(all_models, aes(x = .data[[variable]], y = .data[[yvariable]], color = .data[["label"]], group = .data[["label"]])) +

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

color = label etc will do

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

plot.model_diagnostics() gives a warning for using deprecated aes_string() with ggplot2 >=3.0.0

2 participants