diff --git a/DESCRIPTION b/DESCRIPTION index f2584c5410..815e8e9967 100644 --- a/DESCRIPTION +++ b/DESCRIPTION @@ -25,7 +25,7 @@ Imports: grid, gtable (>= 0.1.1), reshape2, - scales (>= 0.2.3), + scales (>= 0.2.5), proto, MASS Suggests: diff --git a/NEWS b/NEWS index 16785040a4..40e68584ad 100644 --- a/NEWS +++ b/NEWS @@ -1,6 +1,10 @@ ggplot2 1.0.1.9000 ---------------------------------------------------------------- +* Add access to the new `direction` argument of `scales::brewer_pal` and + simplify changing the order of colours for `scale_*_brewer` and + `scale_*_distiller` (@jiho, #1139) + * `facet_wrap()` more carefully checks its `nrow` and `ncol` arguments to ensure that they're specified correctly (@richierocks, #962) @@ -99,8 +103,8 @@ ggplot2 1.0.1 * Fixes to pass `R CMD check --run-donttest` in R-devel. -* Improvements in to order of colours and legend display of continuous colour - scales extracted from colorbrewer palettes in `scale_*_distiller()` (@jiho, 1076) +* Improvements to order of colours and legend display for continuous colour + scales extracted from colorbrewer palettes by `scale_*_distiller()` (@jiho, 1076) ggplot2 1.0.0 ---------------------------------------------------------------- diff --git a/R/scale-brewer.r b/R/scale-brewer.r index fb9c53a162..08c160b987 100644 --- a/R/scale-brewer.r +++ b/R/scale-brewer.r @@ -32,14 +32,18 @@ #' d + scale_colour_brewer(type = "seq") #' d + scale_colour_brewer(type = "seq", palette = 3) #' -#' d + scale_colour_brewer(palette = "Blues") +#' d + scale_colour_brewer(palette = "Greens") #' d + scale_colour_brewer(palette = "Set1") #' #' # scale_fill_brewer works just the same as #' # scale_colour_brewer but for fill colours -#' ggplot(diamonds, aes(x = price, fill = cut)) + -#' geom_histogram(position = "dodge", binwidth = 1000) + -#' scale_fill_brewer() +#' p <- ggplot(diamonds, aes(x = price, fill = cut)) + +#' geom_histogram(position = "dodge", binwidth = 1000) +#' p + scale_fill_brewer() +#' # the order of colour can be reversed +#' p + scale_fill_brewer(direction = -1) +#' # the brewer scales look better on a darker background +#' p + scale_fill_brewer(direction = -1) + theme_dark() #' #' # Generate map data #' library(reshape2) # for melt @@ -54,41 +58,40 @@ #' v + scale_fill_distiller(palette = 2) #' v + scale_fill_distiller(type = "div") #' v + scale_fill_distiller(palette = "Spectral") -#' v + scale_fill_distiller(palette = "Spectral", trans = "reverse") #' v + scale_fill_distiller(type = "qual") #' # Not appropriate for continuous data, issues a warning -scale_colour_brewer <- function(..., type = "seq", palette = 1) { - discrete_scale("colour", "brewer", brewer_pal(type, palette), ...) +scale_colour_brewer <- function(..., type = "seq", palette = 1, direction = 1) { + discrete_scale("colour", "brewer", brewer_pal(type, palette, direction), ...) } #' @export #' @rdname scale_brewer -scale_fill_brewer <- function(..., type = "seq", palette = 1) { - discrete_scale("fill", "brewer", brewer_pal(type, palette), ...) +scale_fill_brewer <- function(..., type = "seq", palette = 1, direction = 1) { + discrete_scale("fill", "brewer", brewer_pal(type, palette, direction), ...) } #' @export #' @rdname scale_brewer -scale_colour_distiller <- function(..., type = "seq", palette = 1, values = NULL, space = "Lab", na.value = "grey50", guide = "colourbar") { +scale_colour_distiller <- function(..., type = "seq", palette = 1, direction = -1, values = NULL, space = "Lab", na.value = "grey50", guide = "colourbar") { # warn about using a qualitative brewer palette to generate the gradient type <- match.arg(type, c("seq", "div", "qual")) if (type == "qual") { warning("Using a discrete colour palette in a continuous scale.\n Consider using type = \"seq\" or type = \"div\" instead", call. = FALSE) } continuous_scale("colour", "distiller", - gradient_n_pal(rev(brewer_pal(type, palette)(6)), values, space), na.value = na.value, guide = guide, ...) + gradient_n_pal(brewer_pal(type, palette, direction)(6), values, space), na.value = na.value, guide = guide, ...) # NB: 6 colours per palette gives nice gradients; more results in more saturated colours which do not look as good } #' @export #' @rdname scale_brewer -scale_fill_distiller <- function(..., type = "seq", palette = 1, values = NULL, space = "Lab", na.value = "grey50", guide = "colourbar") { +scale_fill_distiller <- function(..., type = "seq", palette = 1, direction = -1, values = NULL, space = "Lab", na.value = "grey50", guide = "colourbar") { type <- match.arg(type, c("seq", "div", "qual")) if (type == "qual") { warning("Using a discrete colour palette in a continuous scale.\n Consider using type = \"seq\" or type = \"div\" instead", call. = FALSE) } continuous_scale("fill", "distiller", - gradient_n_pal(rev(brewer_pal(type, palette)(6)), values, space), na.value = na.value, guide = guide, ...) + gradient_n_pal(brewer_pal(type, palette, direction)(6), values, space), na.value = na.value, guide = guide, ...) } # icon.brewer <- function() { diff --git a/man/scale_brewer.Rd b/man/scale_brewer.Rd index 3bba954800..38ee66543b 100644 --- a/man/scale_brewer.Rd +++ b/man/scale_brewer.Rd @@ -9,20 +9,23 @@ \alias{scale_fill_distiller} \title{Sequential, diverging and qualitative colour scales from colorbrewer.org} \usage{ -scale_colour_brewer(..., type = "seq", palette = 1) +scale_colour_brewer(..., type = "seq", palette = 1, direction = 1) -scale_fill_brewer(..., type = "seq", palette = 1) +scale_fill_brewer(..., type = "seq", palette = 1, direction = 1) -scale_colour_distiller(..., type = "seq", palette = 1, values = NULL, - space = "Lab", na.value = "grey50", guide = "colourbar") +scale_colour_distiller(..., type = "seq", palette = 1, direction = -1, + values = NULL, space = "Lab", na.value = "grey50", + guide = "colourbar") -scale_fill_distiller(..., type = "seq", palette = 1, values = NULL, - space = "Lab", na.value = "grey50", guide = "colourbar") +scale_fill_distiller(..., type = "seq", palette = 1, direction = -1, + values = NULL, space = "Lab", na.value = "grey50", + guide = "colourbar") -scale_color_brewer(..., type = "seq", palette = 1) +scale_color_brewer(..., type = "seq", palette = 1, direction = 1) -scale_color_distiller(..., type = "seq", palette = 1, values = NULL, - space = "Lab", na.value = "grey50", guide = "colourbar") +scale_color_distiller(..., type = "seq", palette = 1, direction = -1, + values = NULL, space = "Lab", na.value = "grey50", + guide = "colourbar") } \arguments{ \item{...}{Other arguments passed on to \code{\link{discrete_scale}} @@ -33,6 +36,10 @@ to control name, limits, breaks, labels and so forth.} \item{palette}{If a string, will use that named palette. If a number, will index into the list of palettes of appropriate \code{type}} +\item{direction}{Sets the order of colors in the scale. If 1, the default, +colors are as output by \code{\link[RColorBrewer]{brewer.pal}}. If -1, the +order of colors is reversed.} + \item{values}{if colours should not be evenly positioned along the gradient this vector gives the position (between 0 and 1) for each colour in the \code{colours} vector. See \code{\link{rescale}} for a convience function @@ -73,14 +80,18 @@ d + scale_colour_brewer(expression(clarity[beta])) d + scale_colour_brewer(type = "seq") d + scale_colour_brewer(type = "seq", palette = 3) -d + scale_colour_brewer(palette = "Blues") +d + scale_colour_brewer(palette = "Greens") d + scale_colour_brewer(palette = "Set1") # scale_fill_brewer works just the same as # scale_colour_brewer but for fill colours -ggplot(diamonds, aes(x = price, fill = cut)) + - geom_histogram(position = "dodge", binwidth = 1000) + - scale_fill_brewer() +p <- ggplot(diamonds, aes(x = price, fill = cut)) + + geom_histogram(position = "dodge", binwidth = 1000) +p + scale_fill_brewer() +# the order of colour can be reversed +p + scale_fill_brewer(direction = -1) +# the brewer scales look better on a darker background +p + scale_fill_brewer(direction = -1) + theme_dark() # Generate map data library(reshape2) # for melt @@ -95,7 +106,6 @@ v + scale_fill_distiller() v + scale_fill_distiller(palette = 2) v + scale_fill_distiller(type = "div") v + scale_fill_distiller(palette = "Spectral") -v + scale_fill_distiller(palette = "Spectral", trans = "reverse") v + scale_fill_distiller(type = "qual") # Not appropriate for continuous data, issues a warning }