Visualization
Generation and plotting functions
mlr's visualization capabilities rely on generation functions which generate data for plots, and plotting functions which plot this output using either ggplot2 or ggvis (the latter being currently experimental).
This separation allows users to easily make custom visualizations by taking advantage of the generation functions. The only data transformation that is handled inside plotting functions is reshaping. The reshaped data is also accessible by calling the plotting functions and then extracting the data from the ggplot2::ggplot object.
The functions are named accordingly.
- Names of generation functions start with
generate
and are followed by a title-case description of theirFunctionPurpose
, followed byData
, i.e.,generateFunctionPurposeData
. These functions output objects of classFunctionPurposeData
. - Plotting functions are prefixed by
plot
followed by their purpose, i.e.,plotFunctionPurpose
. - ggvis plotting functions have an additional suffix
GGVIS
, i.e.,plotFunctionPurposeGGVIS
.
In the example below we create a plot of classifier performance as function of the decision
threshold for the binary classification problem sonar.task.
The generation function generateThreshVsPerfData creates an object of class
ThreshVsPerfData which contains the data for the plot in slot
$data
.
lrn = makeLearner("classif.lda", predict.type = "prob")
n = getTaskSize(sonar.task)
mod = train(lrn, task = sonar.task, subset = seq(1, n, by = 2))
pred = predict(mod, task = sonar.task, subset = seq(2, n, by = 2))
d = generateThreshVsPerfData(pred, measures = list(fpr, fnr, mmce))
class(d)
#> [1] "ThreshVsPerfData"
head(d$data)
#> threshold learner fpr fnr mmce
#> 1 0.00000000 prediction 1.0000000 0.0000000 0.4615385
#> 2 0.01010101 prediction 0.3541667 0.1964286 0.2692308
#> 3 0.02020202 prediction 0.3333333 0.2321429 0.2788462
#> 4 0.03030303 prediction 0.3333333 0.2321429 0.2788462
#> 5 0.04040404 prediction 0.3333333 0.2321429 0.2788462
#> 6 0.05050505 prediction 0.3125000 0.2321429 0.2692308
For plotting we can use the built-in mlr function plotThreshVsPerf.
plotThreshVsPerf(d)
Note that by default the Measure name
s are used to annotate the plot.
fpr$name
#> [1] "False positive rate"
fpr$id
#> [1] "fpr"
This does not only apply to plotThreshVsPerf, but to most other plot functions that
show performance measures.
You can use the id
s instead of the names by setting pretty.names = FALSE
.
Instead of using the built-in function plotThreshVsPerf we could also manually create the plot based on the output of generateThreshVsPerfData: in this case to plot only one measure.
ggplot(d$data, aes(threshold, fpr)) + geom_line()
The decoupling of generation and plotting functions is especially practical for all users who prefer traditional graphics or lattice. Here is a lattice plot which gives a result similar to that of plotThreshVsPerf.
lattice::xyplot(fpr + fnr + mmce ~ threshold, data = d$data, type = "l", ylab = "performance",
outer = TRUE, scales = list(relation = "free"),
strip = strip.custom(factor.levels = sapply(d$measures, function(x) x$name)))
Let's conclude with a brief look on a second example. Here we use plotPartialPrediction but extract the data from the plot object and use it to create a traditional graphics::plot, additional to the ggplot2 plot.
sonar = getTaskData(sonar.task)
pd = generatePartialPredictionData(mod, sonar, "V11")
plt = plotPartialPrediction(pd)
head(plt$data)
#> Class Probability V11
#> 1 M 0.9295997 0.7342000
#> 2 M 0.9084961 0.6558333
#> 3 M 0.8792694 0.5774667
#> 4 M 0.8232852 0.4991000
#> 5 M 0.7387962 0.4207333
#> 6 M 0.6557857 0.3423667
plt
plot(Probability ~ V11, data = plt$data, type = "b")
List of available functions
The table shows the currently available generation and plotting functions. It also references tutorial pages that provide in depth descriptions of the listed functions.
Note that some plots, e.g., plotTuneMultiCritResult are not described here since they lack a generation function.
The ggvis functions are experimental and are subject to change, though they should work. Most generate interactive shiny applications, that automatically start and run locally.