Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

[R] allow xgb.plot.importance() calls to fill a grid (fixes #4863) #6294

Merged
merged 1 commit into from Oct 28, 2020
Merged

[R] allow xgb.plot.importance() calls to fill a grid (fixes #4863) #6294

merged 1 commit into from Oct 28, 2020

Conversation

jameslamb
Copy link
Contributor

@jameslamb jameslamb commented Oct 27, 2020

Closes #4863

I was looking for issues tagged r-package tonight and found #4863 . I think this PR fixes it.

Currently, xgb.plot.importance() overwrites all of the graphical parameters when it exits, which means that consecutive calls don't respect par(mfcol), and don't fill a grid. This PR changes that, and just resets the specific value mar in the parameters.

Here are the results on 4 consecutive calls of xgb.plot.importance():

before

image

after

image

Reproducible example

code (click me)
library(data.table)

data(agaricus.train, package = "xgboost")

bst <- xgboost::xgboost(
    data = agaricus.train$data
    , label = agaricus.train$label
    , max_depth = 3
    , eta = 1
    , nthread = 2
    , nrounds = 2
    , objective = "binary:logistic"
)

importance_matrix <- xgboost::xgb.importance(
    colnames(agaricus.train$data)
    , model = bst
)

par(mfrow = c(2,2))
for (i in seq_len(4)) {
    xgboost::xgb.plot.importance(
        importance_matrix
        , rel_to_first = TRUE
        , xlab = "Relative importance"
        , left_margin = 10
    )
}

How this improves {xgboost}

This allows the results of this function to be arranged in a grid, which could be useful for comparing feature importances across different models.

Thanks for your time and consideration

@hcho3 hcho3 merged commit 6383757 into dmlc:master Oct 28, 2020
@hcho3
Copy link
Collaborator

hcho3 commented Oct 28, 2020

Thanks!

@jameslamb jameslamb deleted the fix/r-plots branch June 1, 2021 04:14
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.

xgb.plot.importance is not compatible with "layout" nor with "par"
2 participants