mlr-org / mlr3viz

Visualizations for mlr3
https://mlr3viz.mlr-org.com
GNU Lesser General Public License v3.0
42 stars 8 forks source link

Autplot for Benchmarking (Change order of boxplots/tasks) #88

Open FionaKunz opened 2 years ago

FionaKunz commented 2 years ago

Hi!

Plotting my benchmark results, I would like to change the order of the boxplots from alphabetical order (default in autoplot function?) to another order. So far, I was able to change the respective task's boxplots the following way (see code). However, since recently, changing the levels of the task_id-factor variable, only changes the order of the title, but not the respective boxplots themselves.

How can I change the order of the boxplots? Any help highly appreciated! Thanks a lot


library(mlr3)
library(mlr3viz)

tasks = tsks(c("pima", "sonar"))
learner = lrns(c("classif.featureless", "classif.rpart"),
               predict_type = "prob")
resampling = rsmps("cv")
bmr = benchmark(benchmark_grid(tasks, learner, resampling))

# AUC
bmr_boxplots_auc <- autoplot(bmr, measure = msr("classif.auc")) + 
  ggplot2::theme(axis.text.x = ggplot2::element_text(hjust = .5)) + 
  ggplot2::scale_x_discrete(labels =c("FL", "RF", "LR"))+
  ggplot2::ylab("AUC")+
  ggplot2::xlab("Learner")+
  ggplot2::theme_bw()+
  ggplot2::theme(panel.grid = element_blank(),
                 strip.background = element_blank()
  )
bmr_boxplots_auc
bmr_boxplots_auc$data$task_id <- as.factor(bmr_boxplots_auc$data$task_id) 
levels(bmr_boxplots_auc$data$task_id) = c("sonar", "pima")
bmr_boxplots_auc

Bildschirmfoto 2021-11-24 um 18 00 22

pat-s commented 2 years ago

@FionaKunz This is a bit tricky as one needs to modify the order of a factor.

I've added support for this in #102, you can try installing the branch to use it.

library(mlr3)
library(mlr3viz)

tasks <- tsks(c("pima", "sonar"))
learner <- lrns(c("classif.featureless", "classif.rpart"),
  predict_type = "prob"
)
resampling <- rsmps("cv")
lgr::get_logger("mlr3")$set_threshold("error")
bmr <- benchmark(benchmark_grid(tasks, learner, resampling))

autoplot(bmr, measure = msr("classif.auc"), custom_facet_order = c("pima", "sonar"))

autoplot(bmr, measure = msr("classif.auc"), custom_facet_order = c("sonar", "pima"))

Created on 2022-05-30 by the reprex package (v2.0.1)

Session info ``` r sessioninfo::session_info() #> ─ Session info ─────────────────────────────────────────────────────────────── #> setting value #> version R version 4.2.0 (2022-04-22) #> os macOS Monterey 12.4 #> system aarch64, darwin20 #> ui X11 #> language (EN) #> collate en_US.UTF-8 #> ctype en_US.UTF-8 #> tz Europe/Zurich #> date 2022-05-30 #> pandoc 2.18 @ /Applications/RStudio.app/Contents/Resources/app/quarto/bin/tools/ (via rmarkdown) #> #> ─ Packages ─────────────────────────────────────────────────────────────────── #> package * version date (UTC) lib source #> assertthat 0.2.1 2019-03-21 [1] CRAN (R 4.2.0) #> backports 1.4.1 2021-12-13 [1] CRAN (R 4.2.0) #> checkmate 2.1.0 2022-04-21 [1] CRAN (R 4.2.0) #> cli 3.3.0 2022-04-25 [1] CRAN (R 4.2.0) #> codetools 0.2-18 2020-11-04 [3] CRAN (R 4.2.0) #> colorspace 2.0-3 2022-02-21 [1] CRAN (R 4.2.0) #> crayon 1.5.1 2022-03-26 [1] CRAN (R 4.2.0) #> curl 4.3.2 2021-06-23 [1] CRAN (R 4.2.0) #> data.table 1.14.2 2021-09-27 [1] CRAN (R 4.2.0) #> DBI 1.1.2 2021-12-20 [1] CRAN (R 4.2.0) #> digest 0.6.29 2021-12-01 [1] CRAN (R 4.2.0) #> dplyr 1.0.9 2022-04-28 [1] CRAN (R 4.2.0) #> ellipsis 0.3.2 2021-04-29 [1] CRAN (R 4.2.0) #> evaluate 0.15 2022-02-18 [1] CRAN (R 4.2.0) #> fansi 1.0.3 2022-03-24 [1] CRAN (R 4.2.0) #> farver 2.1.0 2021-02-28 [1] CRAN (R 4.2.0) #> fastmap 1.1.0 2021-01-25 [1] CRAN (R 4.2.0) #> fs 1.5.2 2021-12-08 [1] CRAN (R 4.2.0) #> future 1.26.1 2022-05-27 [1] CRAN (R 4.2.0) #> future.apply 1.9.0 2022-04-25 [1] CRAN (R 4.2.0) #> generics 0.1.2 2022-01-31 [1] CRAN (R 4.2.0) #> ggplot2 3.3.6 2022-05-03 [1] CRAN (R 4.2.0) #> globals 0.15.0 2022-05-09 [1] CRAN (R 4.2.0) #> glue 1.6.2 2022-02-24 [1] CRAN (R 4.2.0) #> gtable 0.3.0 2019-03-25 [1] CRAN (R 4.2.0) #> highr 0.9 2021-04-16 [1] CRAN (R 4.2.0) #> htmltools 0.5.2 2021-08-25 [1] CRAN (R 4.2.0) #> httr 1.4.3 2022-05-04 [1] CRAN (R 4.2.0) #> knitr 1.39 2022-04-26 [1] CRAN (R 4.2.0) #> labeling 0.4.2 2020-10-20 [1] CRAN (R 4.2.0) #> lgr 0.4.3 2021-09-16 [1] CRAN (R 4.2.0) #> lifecycle 1.0.1 2021-09-24 [1] CRAN (R 4.2.0) #> listenv 0.8.0 2019-12-05 [1] CRAN (R 4.2.0) #> magrittr 2.0.3 2022-03-30 [1] CRAN (R 4.2.0) #> mime 0.12 2021-09-28 [1] CRAN (R 4.2.0) #> mlr3 * 0.13.3-9000 2022-05-28 [1] Github (mlr-org/mlr3@7313ae8) #> mlr3measures 0.4.1 2022-01-13 [1] CRAN (R 4.2.0) #> mlr3misc 0.10.0 2022-01-11 [1] CRAN (R 4.2.0) #> mlr3viz * 0.5.9 2022-05-30 [1] local #> munsell 0.5.0 2018-06-12 [1] CRAN (R 4.2.0) #> palmerpenguins 0.1.0 2020-07-23 [1] CRAN (R 4.2.0) #> paradox 0.9.0.9000 2022-05-09 [1] Github (mlr-org/paradox@d41cc29) #> parallelly 1.31.1 2022-04-22 [1] CRAN (R 4.2.0) #> pillar 1.7.0 2022-02-01 [1] CRAN (R 4.2.0) #> pkgconfig 2.0.3 2019-09-22 [1] CRAN (R 4.2.0) #> purrr 0.3.4 2020-04-17 [1] CRAN (R 4.2.0) #> R.cache 0.15.0 2021-04-30 [1] CRAN (R 4.2.0) #> R.methodsS3 1.8.1 2020-08-26 [1] CRAN (R 4.2.0) #> R.oo 1.24.0 2020-08-26 [1] CRAN (R 4.2.0) #> R.utils 2.11.0 2021-09-26 [1] CRAN (R 4.2.0) #> R6 2.5.1 2021-08-19 [1] CRAN (R 4.2.0) #> reprex 2.0.1 2021-08-05 [1] CRAN (R 4.2.0) #> rlang 1.0.2 2022-03-04 [1] CRAN (R 4.2.0) #> rmarkdown 2.14 2022-04-25 [1] CRAN (R 4.2.0) #> rpart 4.1.16 2022-01-24 [3] CRAN (R 4.2.0) #> rstudioapi 0.13 2020-11-12 [1] CRAN (R 4.2.0) #> scales 1.2.0 2022-04-13 [1] CRAN (R 4.2.0) #> sessioninfo 1.2.2 2021-12-06 [1] CRAN (R 4.2.0) #> stringi 1.7.6 2021-11-29 [1] CRAN (R 4.2.0) #> stringr 1.4.0 2019-02-10 [1] CRAN (R 4.2.0) #> styler 1.7.0 2022-03-13 [1] CRAN (R 4.2.0) #> tibble 3.1.7 2022-05-03 [1] CRAN (R 4.2.0) #> tidyselect 1.1.2 2022-02-21 [1] CRAN (R 4.2.0) #> utf8 1.2.2 2021-07-24 [1] CRAN (R 4.2.0) #> uuid 1.1-0 2022-04-19 [1] CRAN (R 4.2.0) #> vctrs 0.4.1 2022-04-13 [1] CRAN (R 4.2.0) #> withr 2.5.0 2022-03-03 [1] CRAN (R 4.2.0) #> xfun 0.31 2022-05-10 [1] CRAN (R 4.2.0) #> xml2 1.3.3 2021-11-30 [1] CRAN (R 4.2.0) #> yaml 2.3.5 2022-02-21 [1] CRAN (R 4.2.0) #> #> [1] /Users/pjs/Library/R/arm64/4.2/library #> [2] /opt/R/4.2.0-arm64/Resources/site-library #> [3] /opt/R/4.2.0-arm64/Resources/library #> #> ────────────────────────────────────────────────────────────────────────────── ```