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::ggplot2() or ggvis::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 their FunctionPurpose, followed by Data, i.e., generateFunctionPurposeData. These functions output objects of class FunctionPurposeData.
• Plotting functions are prefixed by plot followed by their purpose, i.e., plotFunctionPurpose.
• ggvis::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. For plotting we can use the built-in mlr function plotThreshVsPerf(). Note that by default the Measure names are used to annotate the panels. This does not only apply to plotThreshVsPerf(), but to other plot functions that show performance measures as well, for example plotLearningCurve(). You can use the ids instead of the names by setting pretty.names = FALSE. Customizing plots As mentioned above it is easily possible to customize the built-in plots or making your own visualizations from scratch based on the generated data. What will probably come up most often is changing labels and annotations. Generally, this can be done by manipulating the ggplot2::ggplot() object, in this example the object returned by plotThreshVsPerf(), using the usual ggplot2::ggplot2() functions like ggplot2::labs() or ggplot2::labeller(). Moreover, you can change the underlying data, either d$data (resulting from generateThreshVsPerfData() or the possibly reshaped data contained in the ggplot2::ggplot() object (resulting from plotThreshVsPerf(), most often by renaming columns or factor levels.

Below are two examples of how to alter the axis and panel labels of the above plot.

Imagine you want to change the order of the panels and also are not satisfied with the panel names, for example you find that “Mean misclassification error” is too long and you prefer “Error rate” instead. Moreover, you want the error rate to be displayed first.

Using the ggplot2::labeller() function requires calling ggplot2::facet_wrap() (or ggplot2::facet_grid()), which can be useful if you want to change how the panels are positioned (number of rows and columns) or influence the axis limits.

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.

The decoupling of generation and plotting functions is especially practical if you prefer traditional graphics::graphics() or lattice::lattice(). Here is a lattice::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), par.strip.text = list(cex = 0.8))) Let’s conclude with a brief look on a second example. Here we use plotPartialDependence() but extract the data from the ggplot2::ggplot() object plt and use it to create a traditional graphics::plot(), additional to the ggplot2::ggplot() plot. plot(M ~ Value, data = plt$data, type = "b", xlab = plt$data$Feature[1])

Available generation and plotting functions

Below the currently available generation and plotting functions are listed and tutorial pages that provide in depth descriptions of the listed functions are referenced.

Note that some plots, e.g., plotTuneMultiCritResult() are not mentioned here since they lack a generation function. Both plotThreshVsPerf() and plotROCCurves() operate on the result of generateThreshVsPerfData(). Functions plotPartialDependence() and plotPartialDependenceGGVIS() can be applied to the results of both generatePartialDependenceData() and generateFunctionalANOVAData().

The ggvis::ggvis() functions are experimental and are subject to change, though they should work. Most generate interactive shiny::shiny() applications, that automatically start and run locally.

generation function ggplot2 plotting function ggvis plotting function tutorial pages
generateThreshVsPerfData() plotThresVsPerf() plotThreshVsPerfGGVIS() performance
plotROCCurves() ROC analysis
generateCritDifferencesData() plotCritDifferences() benchmark experiments
generateHyperParsEffectData() plotHyperParsEffect() tuning, hyperparameter tuning effects
generateFilterValuesData() plotFilterValues() plotFilterValuesGGVIS() feature selection
generateLearningCurveData() plotLearningCurve() plotLearningCurveGGVIS() learning curves
generatePartialDependenceData() plotPartialDependence() plotPartialDependenceGGVIS() partial dependence