Skip to content

Rebased version of continuous brewer color scales #925

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 13 commits into from
Mar 21, 2014
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
3 changes: 3 additions & 0 deletions NAMESPACE
Original file line number Diff line number Diff line change
Expand Up @@ -301,6 +301,7 @@ export(scale_area)
export(scale_color_brewer)
export(scale_color_continuous)
export(scale_color_discrete)
export(scale_color_distiller)
export(scale_color_gradient)
export(scale_color_gradient2)
export(scale_color_gradientn)
Expand All @@ -311,6 +312,7 @@ export(scale_color_manual)
export(scale_colour_brewer)
export(scale_colour_continuous)
export(scale_colour_discrete)
export(scale_colour_distiller)
export(scale_colour_gradient)
export(scale_colour_gradient2)
export(scale_colour_gradientn)
Expand All @@ -321,6 +323,7 @@ export(scale_colour_manual)
export(scale_fill_brewer)
export(scale_fill_continuous)
export(scale_fill_discrete)
export(scale_fill_distiller)
export(scale_fill_gradient)
export(scale_fill_gradient2)
export(scale_fill_gradientn)
Expand Down
3 changes: 3 additions & 0 deletions NEWS
Original file line number Diff line number Diff line change
@@ -1,6 +1,9 @@
ggplot2 0.9.3.1.99
----------------------------------------------------------------

* Allow to use brewer palettes for continuous scales, through the new
`scale_fill/colour_distiller()` functions (@jiho, #925).

* Allow specifying only one of the limits in a scale and use the automatic
calculation of the other limit by passing NA to to the limit function,
`xlim()` or `ylim()` (@jimhester, #557).
Expand Down
70 changes: 63 additions & 7 deletions R/scale-brewer.r
Original file line number Diff line number Diff line change
@@ -1,33 +1,59 @@
#' Sequential, diverging and qualitative colour scales from colorbrewer.org
#'
#' ColorBrewer provides sequential, diverging and qualitative colour schemes
#' which are particularly suited and tested to display discrete values (levels
#' of a factor) on a map. ggplot2 can use those colours in discrete scales. It
#' also allows to smoothly interpolate 6 colours from any palette to a
#' continuous scale (6 colours per palette gives nice gradients; more results in
#' more saturated colours which do not look as good). However, the original
#' colour schemes (particularly the qualitative ones) were not intended for this
#' and the perceptual result is left to the appreciation of the user.
#'
#' See \url{http://colorbrewer2.org} for more information.
#'
#' @inheritParams scales::brewer_pal
#' @inheritParams scale_colour_hue
#' @inheritParams scale_colour_gradient
#' @family colour scales
#' @rdname scale_brewer
#' @export
#' @examples
#' dsamp <- diamonds[sample(nrow(diamonds), 1000), ]
#' (d <- qplot(carat, price, data=dsamp, colour=clarity))
#' (d <- qplot(carat, price, data = dsamp, colour = clarity))
#'
#' # Change scale label
#' d + scale_colour_brewer()
#' d + scale_colour_brewer("clarity")
#' d + scale_colour_brewer(expression(clarity[beta]))
#'
#' # Select brewer palette to use, see ?scales::brewer_pal for more details
#' d + scale_colour_brewer(type="seq")
#' d + scale_colour_brewer(type="seq", palette=3)
#' 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="Set1")
#' d + scale_colour_brewer(palette = "Blues")
#' 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) +
#' ggplot(diamonds, aes(x = price, fill = cut)) +
#' geom_histogram(position = "dodge", binwidth = 1000) +
#' scale_fill_brewer()
#'
#' # Generate map data
#' library(reshape2) # for melt
#' volcano3d <- melt(volcano)
#' names(volcano3d) <- c("x", "y", "z")
#'
#' # Basic plot
#' v <- ggplot() + geom_tile(aes(x = x, y = y, fill = z), data = volcano3d)
#' v
#' 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
scale_colour_brewer <- function(..., type = "seq", palette = 1) {
discrete_scale("colour", "brewer", brewer_pal(type, palette), ...)
}
Expand All @@ -37,3 +63,33 @@ scale_colour_brewer <- function(..., type = "seq", palette = 1) {
scale_fill_brewer <- function(..., type = "seq", palette = 1) {
discrete_scale("fill", "brewer", brewer_pal(type, palette), ...)
}

#' @export
#' @rdname scale_brewer
scale_colour_distiller <- function(..., type = "seq", palette = 1, values = NULL, space = "Lab", na.value = "grey50") {
# 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(brewer_pal(type, palette)(6), values, space), na.value = na.value, ...)
# NB: 6 colours per palette gives nice gradients; more results in more saturated colours which do not look as good
Copy link
Member

Choose a reason for hiding this comment

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

Maybe move this note into docs?

}

#' @export
#' @rdname scale_brewer
scale_fill_distiller <- function(..., type = "seq", palette = 1, values = NULL, space = "Lab", na.value = "grey50") {
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(brewer_pal(type, palette)(6), values, space), na.value = na.value, ...)
}

# icon.brewer <- function() {
# rectGrob(c(0.1, 0.3, 0.5, 0.7, 0.9), width = 0.21,
# gp = gpar(fill = RColorBrewer::brewer.pal(5, "PuOr"), col = NA)
# )
# }
4 changes: 4 additions & 0 deletions R/zxx.r
Original file line number Diff line number Diff line change
Expand Up @@ -22,6 +22,10 @@ scale_fill_continuous <- scale_fill_gradient
#' @rdname scale_brewer
scale_color_brewer <- scale_colour_brewer

#' @export
#' @rdname scale_brewer
scale_color_distiller <- scale_colour_distiller

#' @export
#' @rdname scale_gradient
scale_color_continuous <- scale_colour_gradient
Expand Down
54 changes: 47 additions & 7 deletions man/scale_brewer.Rd
Original file line number Diff line number Diff line change
@@ -1,15 +1,27 @@
% Generated by roxygen2 (4.0.0): do not edit by hand
\name{scale_colour_brewer}
\alias{scale_color_brewer}
\alias{scale_color_distiller}
\alias{scale_colour_brewer}
\alias{scale_colour_distiller}
\alias{scale_fill_brewer}
\alias{scale_fill_distiller}
\title{Sequential, diverging and qualitative colour scales from colorbrewer.org}
\usage{
scale_colour_brewer(..., type = "seq", palette = 1)

scale_fill_brewer(..., type = "seq", palette = 1)

scale_colour_distiller(..., type = "seq", palette = 1, values = NULL,
space = "Lab", na.value = "grey50")

scale_fill_distiller(..., type = "seq", palette = 1, values = NULL,
space = "Lab", na.value = "grey50")

scale_color_brewer(..., type = "seq", palette = 1)

scale_color_distiller(..., type = "seq", palette = 1, values = NULL,
space = "Lab", na.value = "grey50")
}
\arguments{
\item{type}{One of seq (sequential), div (diverging) or
Expand All @@ -22,31 +34,59 @@ scale_color_brewer(..., type = "seq", palette = 1)
\item{...}{Other arguments passed on to
\code{\link{discrete_scale}} to control name, limits,
breaks, labels and so forth.}

\item{na.value}{Colour to use for missing values}
}
\description{
ColorBrewer provides sequential, diverging and qualitative colour schemes
which are particularly suited and tested to display discrete values (levels
of a factor) on a map. ggplot2 can use those colours in discrete scales. It
also allows to smoothly interpolate 6 colours from any palette to a
continuous scale (6 colours per palette gives nice gradients; more results in
more saturated colours which do not look as good). However, the original
colour schemes (particularly the qualitative ones) were not intended for this
and the perceptual result is left to the appreciation of the user.
}
\details{
See \url{http://colorbrewer2.org} for more information.
}
\examples{
dsamp <- diamonds[sample(nrow(diamonds), 1000), ]
(d <- qplot(carat, price, data=dsamp, colour=clarity))
(d <- qplot(carat, price, data = dsamp, colour = clarity))

# Change scale label
d + scale_colour_brewer()
d + scale_colour_brewer("clarity")
d + scale_colour_brewer(expression(clarity[beta]))

# Select brewer palette to use, see ?scales::brewer_pal for more details
d + scale_colour_brewer(type="seq")
d + scale_colour_brewer(type="seq", palette=3)
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="Set1")
d + scale_colour_brewer(palette = "Blues")
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) +
ggplot(diamonds, aes(x = price, fill = cut)) +
geom_histogram(position = "dodge", binwidth = 1000) +
scale_fill_brewer()

# Generate map data
library(reshape2) # for melt
volcano3d <- melt(volcano)
names(volcano3d) <- c("x", "y", "z")

# Basic plot
v <- ggplot() + geom_tile(aes(x = x, y = y, fill = z), data = volcano3d)
v
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
}
\seealso{
Other colour scales: \code{\link{scale_color_continuous}},
Expand Down
5 changes: 4 additions & 1 deletion man/scale_gradient.Rd
Original file line number Diff line number Diff line change
Expand Up @@ -99,8 +99,11 @@ qplot(mpg, wt, data = mtcars, colour = miss) +
palette

Other colour scales: \code{\link{scale_color_brewer}},
\code{\link{scale_color_distiller}},
\code{\link{scale_colour_brewer}},
\code{\link{scale_fill_brewer}};
\code{\link{scale_colour_distiller}},
\code{\link{scale_fill_brewer}},
\code{\link{scale_fill_distiller}};
\code{\link{scale_color_discrete}},
\code{\link{scale_color_hue}},
\code{\link{scale_colour_discrete}},
Expand Down
5 changes: 4 additions & 1 deletion man/scale_gradient2.Rd
Original file line number Diff line number Diff line change
Expand Up @@ -87,8 +87,11 @@ p + scale_fill_gradient2("fill")
}
\seealso{
Other colour scales: \code{\link{scale_color_brewer}},
\code{\link{scale_color_distiller}},
\code{\link{scale_colour_brewer}},
\code{\link{scale_fill_brewer}};
\code{\link{scale_colour_distiller}},
\code{\link{scale_fill_brewer}},
\code{\link{scale_fill_distiller}};
\code{\link{scale_color_continuous}},
\code{\link{scale_color_gradient}},
\code{\link{scale_colour_continuous}},
Expand Down
5 changes: 4 additions & 1 deletion man/scale_gradientn.Rd
Original file line number Diff line number Diff line change
Expand Up @@ -68,8 +68,11 @@ d + scale_colour_gradientn(colours = terrain.colors(10),
}
\seealso{
Other colour scales: \code{\link{scale_color_brewer}},
\code{\link{scale_color_distiller}},
\code{\link{scale_colour_brewer}},
\code{\link{scale_fill_brewer}};
\code{\link{scale_colour_distiller}},
\code{\link{scale_fill_brewer}},
\code{\link{scale_fill_distiller}};
\code{\link{scale_color_continuous}},
\code{\link{scale_color_gradient}},
\code{\link{scale_colour_continuous}},
Expand Down
5 changes: 4 additions & 1 deletion man/scale_grey.Rd
Original file line number Diff line number Diff line change
Expand Up @@ -41,8 +41,11 @@ qplot(mpg, wt, data = mtcars, colour = miss) +
}
\seealso{
Other colour scales: \code{\link{scale_color_brewer}},
\code{\link{scale_color_distiller}},
\code{\link{scale_colour_brewer}},
\code{\link{scale_fill_brewer}};
\code{\link{scale_colour_distiller}},
\code{\link{scale_fill_brewer}},
\code{\link{scale_fill_distiller}};
\code{\link{scale_color_continuous}},
\code{\link{scale_color_gradient}},
\code{\link{scale_colour_continuous}},
Expand Down
5 changes: 4 additions & 1 deletion man/scale_hue.Rd
Original file line number Diff line number Diff line change
Expand Up @@ -86,8 +86,11 @@ qplot(mpg, wt, data = mtcars, colour = miss) +
}
\seealso{
Other colour scales: \code{\link{scale_color_brewer}},
\code{\link{scale_color_distiller}},
\code{\link{scale_colour_brewer}},
\code{\link{scale_fill_brewer}};
\code{\link{scale_colour_distiller}},
\code{\link{scale_fill_brewer}},
\code{\link{scale_fill_distiller}};
\code{\link{scale_color_continuous}},
\code{\link{scale_color_gradient}},
\code{\link{scale_colour_continuous}},
Expand Down