From 09c30cdc67b222538f403acba529a60b114b91b6 Mon Sep 17 00:00:00 2001 From: m-dz Date: Thu, 13 Apr 2017 00:07:43 +0100 Subject: [PATCH 1/2] Add formatting to facets' labels --- pkg/caret/R/ggplot.R | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/pkg/caret/R/ggplot.R b/pkg/caret/R/ggplot.R index c2463576b..107a470de 100644 --- a/pkg/caret/R/ggplot.R +++ b/pkg/caret/R/ggplot.R @@ -122,8 +122,8 @@ ggplot.train <- function(data = NULL, mapping = NULL, metric = data$metric[1], p strip_lab <- as.character(subset(data$modelInfo$parameters, parameter %in% strip_vars)$label) for(i in seq_along(strip_vars)) dat[, strip_vars[i]] <- factor( - paste(strip_lab[i], dat[, strip_vars[i]], sep = ": "), - levels = paste(strip_lab[i], sort(unique(dat[, strip_vars[i]])), sep = ": ") + paste(strip_lab[i], format(dat[, strip_vars[i]]), sep = ": "), + levels = paste(strip_lab[i], format(sort(unique(dat[, strip_vars[i]]))), sep = ": ") ) } ## TODO: use factor(format(x)) to make a solid block of colors? From 7967d2051161a916b2a64ecc2ac1e39915921877 Mon Sep 17 00:00:00 2001 From: m-dz Date: Thu, 13 Apr 2017 00:08:41 +0100 Subject: [PATCH 2/2] Add svmPoly test for ordering of facets' labels --- pkg/caret/tests/testthat/test_ggplot.R | 48 +++++++++++++++++++++++--- 1 file changed, 43 insertions(+), 5 deletions(-) diff --git a/pkg/caret/tests/testthat/test_ggplot.R b/pkg/caret/tests/testthat/test_ggplot.R index c243cc4de..7a0dd32db 100644 --- a/pkg/caret/tests/testthat/test_ggplot.R +++ b/pkg/caret/tests/testthat/test_ggplot.R @@ -1,12 +1,16 @@ context("Test ggplot") test_that("ggplot.train correctly orders factors", { + library(caret) + library(kernlab) data(mtcars) - m <- train(mpg ~ cyl + disp, - data = mtcars, - method="svmRadial", - tuneGrid = expand.grid(C=1:2, sigma=c(0.0001, 0.01, 1))) - g <- ggplot(m, plotType="level") + m <- train( + mpg ~ cyl + disp, + data = mtcars, + method = "svmRadial", + tuneGrid = expand.grid(C = 1:2, sigma = c(0.0001, 0.01, 1)) + ) + g <- ggplot(m, plotType = "level") # Test plot data obj_sigma <- as.numeric(levels(g$data$sigma)) @@ -21,3 +25,37 @@ test_that("ggplot.train correctly orders factors", { expect_equal(obj_x, sort(obj_x)) expect_equal(obj_y, sort(obj_y)) }) + +test_that("ggplot.train correctly orders facets' labels", { + library(caret) + library(kernlab) + data(mtcars) + m <- suppressWarnings(train( + mpg ~ cyl + disp, + data = mtcars, + method = "svmPoly", + tuneGrid = expand.grid( + degree = c(0.0001, 0.01, 1), + scale = c(0.0001, 0.01, 1), + C = c(0.0001, 0.01, 1) + ) + )) + g <- ggplot(m, plotType = "level", nameInStrip = TRUE) + + # Test plot data + obj_C <- as.numeric(gsub( + 'Cost: ', + '', + levels(g$data$C) + )) + expect_equal(obj_C, sort(obj_C)) + + # Test axes' labels on a built plot + build <- ggplot2::ggplot_build(g) + obj_labels <- as.numeric(gsub( + 'Cost: ', + '', + levels(build$layout$panel_layout$C) + )) + expect_equal(obj_labels, sort(obj_labels)) +})