Skip to content

Add access to direction argument of scales::brewer_pal #1139

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 2 commits into from
Jul 27, 2015
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
2 changes: 1 addition & 1 deletion DESCRIPTION
Original file line number Diff line number Diff line change
Expand Up @@ -25,7 +25,7 @@ Imports:
grid,
gtable (>= 0.1.1),
reshape2,
scales (>= 0.2.3),
scales (>= 0.2.5),
proto,
MASS
Suggests:
Expand Down
8 changes: 6 additions & 2 deletions NEWS
Original file line number Diff line number Diff line change
@@ -1,6 +1,10 @@
ggplot2 1.0.1.9000
----------------------------------------------------------------

* Add access to the new `direction` argument of `scales::brewer_pal` and
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Sorry I think I was unclear bfore, this should be the first bullet under the version heading

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Oh, OK. I thought this is what I always did so I misunderstood your comment regarding this. I'll correct it.

BTW, do you prefer 1-commit pull requests (in which case I overwrite the changes each time and force push, so I guess line comments like this disappear) or many small commits are OK (in which case the "history" of the PR itself is kept but it gets messier when merged).

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Small commits is fine - if it gets too messy I'll ask you to flatten.

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)

Expand Down Expand Up @@ -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
----------------------------------------------------------------
Expand Down
29 changes: 16 additions & 13 deletions R/scale-brewer.r
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand All @@ -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() {
Expand Down
38 changes: 24 additions & 14 deletions man/scale_brewer.Rd
Original file line number Diff line number Diff line change
Expand Up @@ -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}}
Expand All @@ -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
Expand Down Expand Up @@ -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
Expand All @@ -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
}
Expand Down