Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
6 changes: 3 additions & 3 deletions DESCRIPTION
Original file line number Diff line number Diff line change
Expand Up @@ -22,7 +22,8 @@ Imports:
scales (>= 0.4.1.9002),
stats,
tibble,
lazyeval
lazyeval,
viridisLite
Suggests:
covr,
ggplot2movies,
Expand All @@ -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
Expand Down
5 changes: 5 additions & 0 deletions NAMESPACE
Original file line number Diff line number Diff line change
Expand Up @@ -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)
Expand All @@ -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)
Expand All @@ -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)
Expand All @@ -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)
Expand All @@ -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)
Expand Down
6 changes: 6 additions & 0 deletions NEWS.md
Original file line number Diff line number Diff line change
@@ -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)

Expand Down
17 changes: 14 additions & 3 deletions R/scale-alpha.r
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
8 changes: 8 additions & 0 deletions R/scale-shape.r
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
22 changes: 17 additions & 5 deletions R/scale-size.r
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
10 changes: 10 additions & 0 deletions R/zxx.r
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down Expand Up @@ -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
Expand Down
5 changes: 4 additions & 1 deletion man/scale_alpha.Rd

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

1 change: 1 addition & 0 deletions man/scale_shape.Rd

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

1 change: 1 addition & 0 deletions man/scale_size.Rd

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

2 changes: 2 additions & 0 deletions man/scale_viridis.Rd

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

42 changes: 42 additions & 0 deletions tests/testthat/test-scales.r
Original file line number Diff line number Diff line change
Expand Up @@ -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"
)
})
8 changes: 7 additions & 1 deletion tests/testthat/test-viridis.R
Original file line number Diff line number Diff line change
@@ -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)) +
Expand All @@ -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")
})