diff --git a/DESCRIPTION b/DESCRIPTION index 3c8ed117ea..31a3dc2f85 100644 --- a/DESCRIPTION +++ b/DESCRIPTION @@ -22,7 +22,8 @@ Imports: scales (>= 0.4.1.9002), stats, tibble, - lazyeval + lazyeval, + viridisLite Suggests: covr, ggplot2movies, @@ -43,8 +44,7 @@ Suggests: rpart, rmarkdown, sf (>= 0.3-4), - svglite (>= 1.2.0.9001), - viridisLite + svglite (>= 1.2.0.9001) Remotes: hadley/scales, hadley/svglite diff --git a/NAMESPACE b/NAMESPACE index 908d67d1dd..14848027f4 100644 --- a/NAMESPACE +++ b/NAMESPACE @@ -374,6 +374,7 @@ export(scale_alpha_datetime) export(scale_alpha_discrete) export(scale_alpha_identity) export(scale_alpha_manual) +export(scale_alpha_ordinal) export(scale_color_brewer) export(scale_color_continuous) export(scale_color_discrete) @@ -400,6 +401,7 @@ export(scale_colour_grey) export(scale_colour_hue) export(scale_colour_identity) export(scale_colour_manual) +export(scale_colour_ordinal) export(scale_colour_viridis_c) export(scale_colour_viridis_d) export(scale_fill_brewer) @@ -415,6 +417,7 @@ export(scale_fill_grey) export(scale_fill_hue) export(scale_fill_identity) export(scale_fill_manual) +export(scale_fill_ordinal) export(scale_fill_viridis_c) export(scale_fill_viridis_d) export(scale_linetype) @@ -428,6 +431,7 @@ export(scale_shape_continuous) export(scale_shape_discrete) export(scale_shape_identity) export(scale_shape_manual) +export(scale_shape_ordinal) export(scale_size) export(scale_size_area) export(scale_size_continuous) @@ -436,6 +440,7 @@ export(scale_size_datetime) export(scale_size_discrete) export(scale_size_identity) export(scale_size_manual) +export(scale_size_ordinal) export(scale_type) export(scale_x_continuous) export(scale_x_date) diff --git a/NEWS.md b/NEWS.md index a1c06a8cd2..faf068d0ac 100644 --- a/NEWS.md +++ b/NEWS.md @@ -1,5 +1,11 @@ # ggplot2 2.2.1.9000 +* Ordered factors now behave differently from unordered factors in some cases. + Ordered factors throw a warning when mapped to shape (unordered factors do + not). Ordered factors do not throw warnings when mapped to size or alpha + (unordered factors do). Viridis is the default colour and fill scale for + ordered factors (@karawoo, #1526). + * The `show.legend` parameter now accepts a named logical vector to hide/show only some aesthetics in the legend (@tutuchan, #1798) diff --git a/R/scale-alpha.r b/R/scale-alpha.r index 0d602a1133..967ae92d9a 100644 --- a/R/scale-alpha.r +++ b/R/scale-alpha.r @@ -28,9 +28,20 @@ scale_alpha_continuous <- scale_alpha #' @rdname scale_alpha #' @export -scale_alpha_discrete <- function(..., range = c(0.1, 1)) { - discrete_scale("alpha", "alpha_d", - function(n) seq(range[1], range[2], length.out = n), ...) +scale_alpha_discrete <- function(...) { + warning("Using alpha for a discrete variable is not advised.", call. = FALSE) + scale_alpha_ordinal(...) +} + +#' @rdname scale_alpha +#' @export +scale_alpha_ordinal <- function(..., range = c(0.1, 1)) { + discrete_scale( + "alpha", + "alpha_d", + function(n) seq(range[1], range[2], length.out = n), + ... + ) } #' @rdname scale_alpha diff --git a/R/scale-shape.r b/R/scale-shape.r index fd064088e7..2a7c8cabac 100644 --- a/R/scale-shape.r +++ b/R/scale-shape.r @@ -43,6 +43,14 @@ scale_shape <- function(..., solid = TRUE) { #' @usage NULL scale_shape_discrete <- scale_shape +#' @rdname scale_shape +#' @export +#' @usage NULL +scale_shape_ordinal <- function(...) { + warning("Using shapes for an ordinal variable is not advised", call. = FALSE) + scale_shape(...) +} + #' @rdname scale_shape #' @export #' @usage NULL diff --git a/R/scale-size.r b/R/scale-size.r index d337fc79a8..240f9f70e8 100644 --- a/R/scale-size.r +++ b/R/scale-size.r @@ -59,12 +59,24 @@ scale_size <- scale_size_continuous #' @rdname scale_size #' @export #' @usage NULL -scale_size_discrete <- function(..., range = c(2, 6)) { +scale_size_discrete <- function(...) { warning("Using size for a discrete variable is not advised.", call. = FALSE) - discrete_scale("size", "size_d", function(n) { - area <- seq(range[1] ^ 2, range[2] ^ 2, length.out = n) - sqrt(area) - }, ...) + scale_size_ordinal(...) +} + +#' @rdname scale_size +#' @export +#' @usage NULL +scale_size_ordinal <- function(..., range = c(2, 6)) { + discrete_scale( + "size", + "size_d", + function(n) { + area <- seq(range[1] ^ 2, range[2] ^ 2, length.out = n) + sqrt(area) + }, + ... + ) } #' @inheritDotParams continuous_scale -aesthetics -scale_name -palette -rescaler diff --git a/R/zxx.r b/R/zxx.r index 1728dbd0d9..9624526b90 100644 --- a/R/zxx.r +++ b/R/zxx.r @@ -5,6 +5,11 @@ #' @usage NULL scale_colour_discrete <- scale_colour_hue +#' @export +#' @rdname scale_viridis +#' @usage NULL +scale_colour_ordinal <- scale_colour_viridis_d + #' @export #' @rdname scale_gradient #' @usage NULL @@ -48,6 +53,11 @@ scale_colour_date <- function(..., #' @usage NULL scale_fill_discrete <- scale_fill_hue +#' @export +#' @rdname scale_viridis +#' @usage NULL +scale_fill_ordinal <- scale_fill_viridis_d + #' @export #' @rdname scale_gradient #' @usage NULL diff --git a/man/scale_alpha.Rd b/man/scale_alpha.Rd index ae94242ce5..a6c3388396 100644 --- a/man/scale_alpha.Rd +++ b/man/scale_alpha.Rd @@ -4,6 +4,7 @@ \alias{scale_alpha} \alias{scale_alpha_continuous} \alias{scale_alpha_discrete} +\alias{scale_alpha_ordinal} \alias{scale_alpha_datetime} \alias{scale_alpha_date} \title{Alpha transparency scales} @@ -12,7 +13,9 @@ scale_alpha(..., range = c(0.1, 1)) scale_alpha_continuous(..., range = c(0.1, 1)) -scale_alpha_discrete(..., range = c(0.1, 1)) +scale_alpha_discrete(...) + +scale_alpha_ordinal(..., range = c(0.1, 1)) } \arguments{ \item{...}{Other arguments passed on to \code{\link[=continuous_scale]{continuous_scale()}} diff --git a/man/scale_shape.Rd b/man/scale_shape.Rd index f25f2afa11..cb0692bcaf 100644 --- a/man/scale_shape.Rd +++ b/man/scale_shape.Rd @@ -3,6 +3,7 @@ \name{scale_shape} \alias{scale_shape} \alias{scale_shape_discrete} +\alias{scale_shape_ordinal} \alias{scale_shape_continuous} \title{Scales for shapes, aka glyphs} \usage{ diff --git a/man/scale_size.Rd b/man/scale_size.Rd index 675ea61f16..1ae79036be 100644 --- a/man/scale_size.Rd +++ b/man/scale_size.Rd @@ -6,6 +6,7 @@ \alias{scale_radius} \alias{scale_size} \alias{scale_size_discrete} +\alias{scale_size_ordinal} \alias{scale_size_area} \alias{scale_size_datetime} \alias{scale_size_date} diff --git a/man/scale_viridis.Rd b/man/scale_viridis.Rd index 4907154963..3ece88770f 100644 --- a/man/scale_viridis.Rd +++ b/man/scale_viridis.Rd @@ -5,6 +5,8 @@ \alias{scale_fill_viridis_d} \alias{scale_colour_viridis_c} \alias{scale_fill_viridis_c} +\alias{scale_colour_ordinal} +\alias{scale_fill_ordinal} \alias{scale_color_viridis_d} \alias{scale_color_viridis_c} \title{Viridis colour scales from viridisLite} diff --git a/tests/testthat/test-scales.r b/tests/testthat/test-scales.r index db52d3aee7..947a6f1f66 100644 --- a/tests/testthat/test-scales.r +++ b/tests/testthat/test-scales.r @@ -188,3 +188,45 @@ test_that("Scales get their correct titles through layout", { expect_identical(p$layout$xlabel(p$plot$labels)$primary, "x") expect_identical(p$layout$ylabel(p$plot$labels)$primary, "y") }) + +test_that("Size and alpha scales throw appropriate warnings for factors", { + df <- data.frame( + x = 1:3, + y = 1:3, + d = LETTERS[1:3], + o = factor(LETTERS[1:3], ordered = TRUE) + ) + p <- ggplot(df, aes(x, y)) + + # There should be warnings when unordered factors are mapped to size/alpha + expect_warning( + ggplot_build(p + geom_point(aes(size = d))), + "Using size for a discrete variable is not advised." + ) + expect_warning( + ggplot_build(p + geom_point(aes(alpha = d))), + "Using alpha for a discrete variable is not advised." + ) + # There should be no warnings for ordered factors + expect_warning(ggplot_build(p + geom_point(aes(size = o))), NA) + expect_warning(ggplot_build(p + geom_point(aes(alpha = o))), NA) +}) + +test_that("Shape scale throws appropriate warnings for factors", { + df <- data.frame( + x = 1:3, + y = 1:3, + d = LETTERS[1:3], + o = factor(LETTERS[1:3], ordered = TRUE) + ) + p <- ggplot(df, aes(x, y)) + + # There should be no warnings when unordered factors are mapped to shape + expect_warning(ggplot_build(p + geom_point(aes(shape = d))), NA) + + # There should be warnings for ordered factors + expect_warning( + ggplot_build(p + geom_point(aes(shape = o))), + "Using shapes for an ordinal variable is not advised" + ) +}) diff --git a/tests/testthat/test-viridis.R b/tests/testthat/test-viridis.R index d1531c1e40..242a99dfc3 100644 --- a/tests/testthat/test-viridis.R +++ b/tests/testthat/test-viridis.R @@ -1,6 +1,6 @@ context("Viridis") -df <- data.frame(x = 1, y = 1, z = "a") +df <- data.frame(x = 1, y = 1, z = "a", tier = factor("low", ordered = TRUE)) test_that("Viridis scale changes point color", { p1 <- ggplot(df, aes(x, y, colour = z)) + @@ -10,3 +10,9 @@ test_that("Viridis scale changes point color", { expect_false(layer_data(p1)$colour == layer_data(p2)$colour) expect_equal(layer_data(p2)$colour, "#440154FF") }) + +test_that("Viridis scale is used by default for ordered factors", { + p <- ggplot(df, aes(x, y, colour = tier)) + geom_point() + + expect_equal(layer_data(p)$colour, "#440154FF") +})