Skip to contents

Turns a gg_partial object into a ggplot2 figure. Each curve is a partial dependence trace, the forest's average prediction as one predictor is swept across its range while the rest are marginalized over the training data. Continuous predictors appear as line plots. Categorical predictors appear as box plots, one box per level, drawn from the per-observation predictions that plot.variable() returns for them, so the box shows how the prediction varies across the training data at that level. Both panels are faceted by variable name so you can compare the shape and scale of each variable's effect at a glance.

Usage

# S3 method for class 'gg_partial'
plot(x, labels = NULL, ...)

Arguments

x

A gg_partial object (output of gg_partial).

labels

Optional variable labels for the facet strips. One of: a named character vector (c(bpd_last = "BP Diastole")); a labelled data frame, whose attr(col, "label") values are read; or a two-column key/label data frame. Variables with no label keep their raw name. Defaults to NULL (raw names).

...

Not currently used; reserved for future arguments.

Value

A ggplot (or patchwork) object. When only one variable type is present a single ggplot is returned. When both continuous and categorical variables are present the two panels are combined vertically via patchwork::wrap_plots(), which also satisfies inherits(p, "ggplot").

Details

When a model label was attached in gg_partial(), lines are colored by model, which is handy for overlaying results from two forests (e.g., one tuned, one default) in the same figure.

Examples

set.seed(42)
airq <- na.omit(airquality)
rf <- randomForestSRC::rfsrc(Ozone ~ ., data = airq, ntree = 50)
pv <- randomForestSRC::plot.variable(rf, partial = TRUE, show.plots = FALSE)
pd <- gg_partial(pv)
plot(pd)