Skip to contents

Plot a gg_variable object,

Usage

# S3 method for class 'gg_variable'
plot(
  x,
  xvar,
  panel = FALSE,
  oob = TRUE,
  points = TRUE,
  smooth = TRUE,
  labels = NULL,
  ...,
  time_units = NULL
)

Arguments

x

gg_variable object created from a rfsrc object

xvar

variable (or list of variables) of interest.

panel

Should plots be faceted along multiple xvar?

oob

oob estimates (boolean)

points

plot the raw data points (boolean)

smooth

include a smooth curve (boolean)

labels

Optional variable labels. One of: a named character vector (c(wt = "Weight")); 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. Applied to the facet strips in the panel plot and to the x axis title in the individual plot. Defaults to NULL (raw names).

...

arguments passed to the ggplot2 functions.

time_units

Optional name of the time unit the forest was fit in, used only in the survival y axis title. The horizon is chosen at gg_variable(rf, time = 1191); plot(gg_dta) then titles the axis "Survival at 1191", and time_units = "days" makes that "Survival at 1191 days". Nothing on an rfsrc object records the unit, so the package cannot infer it. Defaults to NULL (no unit printed). Not printed when multiple times are faceted.

Value

A single ggplot object when length(xvar) == 1 or panel = TRUE; otherwise a patchwork composite that stacks one panel per variable in xvar. Either way the result is one plottable object, never a bare list, so it composes with patchwork and dispatches through ggplot2::autoplot(). For the patchwork case, to inspect one panel with ggplot2::layer_data() pull that panel out first (e.g. ggplot2::layer_data(p[[1]])).

References

Breiman L. (2001). Random forests, Machine Learning, 45:5-32.

Ishwaran H. and Kogalur U.B. (2007). Random survival forests for R, Rnews, 7(2):25-31.

Ishwaran H, Kogalur U (2026). Fast Unified Random Forests for Survival, Regression, and Classification (RF-SRC). R package version 3.6.2. https://cran.r-project.org/package=randomForestSRC

Examples

## ------------------------------------------------------------
## classification
## ------------------------------------------------------------
## -------- iris data
set.seed(42)
rfsrc_iris <- randomForestSRC::rfsrc(Species ~ ., data = iris, ntree = 50)

gg_dta <- gg_variable(rfsrc_iris)
plot(gg_dta, xvar = "Sepal.Width")
#> `geom_smooth()` using method = 'loess' and formula = 'y ~ x'

plot(gg_dta, xvar = "Sepal.Length")
#> `geom_smooth()` using method = 'loess' and formula = 'y ~ x'


## Panel plot across all predictors
plot(gg_dta,
  xvar = rfsrc_iris$xvar.names,
  panel = TRUE, se = FALSE
)
#> Warning: Ignoring unknown parameters: `se`


## ------------------------------------------------------------
## regression
## ------------------------------------------------------------
## -------- air quality data
# na.action = "na.impute" handles missing Ozone / Solar.R values
set.seed(42)
rfsrc_airq <- randomForestSRC::rfsrc(Ozone ~ ., data = airquality,
                    na.action = "na.impute", ntree = 50)
gg_dta <- gg_variable(rfsrc_airq)

# Treat Month as an ordinal factor for better visualization
gg_dta[, "Month"] <- factor(gg_dta[, "Month"])

plot(gg_dta, xvar = "Wind")
#> `geom_smooth()` using method = 'loess' and formula = 'y ~ x'

plot(gg_dta, xvar = "Temp")
#> `geom_smooth()` using method = 'loess' and formula = 'y ~ x'

plot(gg_dta, xvar = "Solar.R")
#> `geom_smooth()` using method = 'loess' and formula = 'y ~ x'
#> Warning: Removed 7 rows containing non-finite outside the scale range (`stat_smooth()`).
#> Warning: Removed 7 rows containing missing values or values outside the scale range
#> (`geom_point()`).


# Factor variable uses notched boxplots
plot(gg_dta, xvar = "Month", notch = TRUE)
#> Warning: Ignoring unknown parameters: `notch`
#> Notch went outside hinges
#>  Do you want `notch = FALSE`?


# \donttest{
# Panel plot across continuous predictors (loess smooths; slower)
plot(gg_dta, xvar = c("Solar.R", "Wind", "Temp", "Day"), panel = TRUE)
#> `geom_smooth()` using method = 'loess' and formula = 'y ~ x'
#> Warning: Removed 7 rows containing non-finite outside the scale range (`stat_smooth()`).
#> Warning: Removed 7 rows containing missing values or values outside the scale range
#> (`geom_point()`).
#> Warning: Removed 7 rows containing missing values or values outside the scale range
#> (`geom_point()`).

# }

## ------------------------------------------------------------
## survival examples
## ------------------------------------------------------------
## -------- veteran data
# \donttest{
data(veteran, package = "randomForestSRC")
set.seed(42)
rfsrc_veteran <- randomForestSRC::rfsrc(Surv(time, status) ~ ., veteran,
  nsplit = 10,
  ntree = 50
)

# Marginal survival at 90 days
gg_dta <- gg_variable(rfsrc_veteran, time = 90)

# Single-variable dependence plots
plot(gg_dta, xvar = "age")
#> `geom_smooth()` using method = 'loess' and formula = 'y ~ x'

plot(gg_dta, xvar = "diagtime")
#> `geom_smooth()` using method = 'loess' and formula = 'y ~ x'


# Panel coplot for two predictors at a single time
plot(gg_dta, xvar = c("age", "diagtime"), panel = TRUE)
#> `geom_smooth()` using method = 'loess' and formula = 'y ~ x'


# Compare survival at 30, 90, and 365 days simultaneously
gg_dta <- gg_variable(rfsrc_veteran, time = c(30, 90, 365))

# Single-variable plot (one facet per time point)
plot(gg_dta, xvar = "age")
#> `geom_smooth()` using method = 'loess' and formula = 'y ~ x'


# Panel coplot across two predictors and three time points
plot(gg_dta, xvar = c("age", "diagtime"), panel = TRUE)
#> `geom_smooth()` using method = 'loess' and formula = 'y ~ x'

# }