Skip to content
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

Add a new continuous colour/fill scale using colorbrewer colours #439

Closed
wants to merge 7 commits into from
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
3 changes: 3 additions & 0 deletions NAMESPACE
Expand Up @@ -112,6 +112,7 @@ export(scale_area)
export(scale_color_brewer) export(scale_color_brewer)
export(scale_color_continuous) export(scale_color_continuous)
export(scale_color_discrete) export(scale_color_discrete)
export(scale_color_distiller)
export(scale_color_gradient) export(scale_color_gradient)
export(scale_color_gradient2) export(scale_color_gradient2)
export(scale_color_gradientn) export(scale_color_gradientn)
Expand All @@ -122,6 +123,7 @@ export(scale_color_manual)
export(scale_colour_brewer) export(scale_colour_brewer)
export(scale_colour_continuous) export(scale_colour_continuous)
export(scale_colour_discrete) export(scale_colour_discrete)
export(scale_colour_distiller)
export(scale_colour_gradient) export(scale_colour_gradient)
export(scale_colour_gradient2) export(scale_colour_gradient2)
export(scale_colour_gradientn) export(scale_colour_gradientn)
Expand All @@ -132,6 +134,7 @@ export(scale_colour_manual)
export(scale_fill_brewer) export(scale_fill_brewer)
export(scale_fill_continuous) export(scale_fill_continuous)
export(scale_fill_discrete) export(scale_fill_discrete)
export(scale_fill_distiller)
export(scale_fill_gradient) export(scale_fill_gradient)
export(scale_fill_gradient2) export(scale_fill_gradient2)
export(scale_fill_gradientn) export(scale_fill_gradientn)
Expand Down
51 changes: 51 additions & 0 deletions R/scale-brewer.r
@@ -1,9 +1,20 @@
#' Sequential, diverging and qualitative colour scales from colorbrewer.org #' Sequential, diverging and qualitative colour scales from colorbrewer.org
#' #'
#' Create colour scales based on ColorBrewer colours.
#'
#' 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 the colours to a continuous scale,
#' although the original colour schemes (particularly the qualitative ones)
#' were not intended for this. The perceptual result is left to the
#' appreciation of the user.
#'
#' See \url{http://colorbrewer2.org} for more information. #' See \url{http://colorbrewer2.org} for more information.
#' #'
#' @inheritParams scales::brewer_pal #' @inheritParams scales::brewer_pal
#' @inheritParams scale_colour_hue #' @inheritParams scale_colour_hue
#' @inheritParams scale_colour_gradient
#' @family colour scales #' @family colour scales
#' @rdname scale_brewer #' @rdname scale_brewer
#' @export #' @export
Expand All @@ -28,6 +39,22 @@
#' ggplot(diamonds, aes(x=price, fill=cut)) + #' ggplot(diamonds, aes(x=price, fill=cut)) +
#' geom_histogram(position="dodge", binwidth=1000) + #' geom_histogram(position="dodge", binwidth=1000) +
#' scale_fill_brewer() #' 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) { scale_colour_brewer <- function(..., type = "seq", palette = 1) {
discrete_scale("colour", "brewer", brewer_pal(type, palette), ...) discrete_scale("colour", "brewer", brewer_pal(type, palette), ...)
} }
Expand All @@ -38,6 +65,30 @@ scale_fill_brewer <- function(..., type = "seq", palette = 1) {
discrete_scale("fill", "brewer", brewer_pal(type, palette), ...) discrete_scale("fill", "brewer", brewer_pal(type, palette), ...)
} }


#' @export
Copy link
Member

Choose a reason for hiding this comment

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

Can you also add a few words to the main doc mentioning that it does both continuous and discrete variables?

Copy link
Contributor Author

Choose a reason for hiding this comment

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

Is commit b5fe2d4 enough?

Copy link
Member

Choose a reason for hiding this comment

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

Maybe mention that continuous colours are smoothly interpolated discrete colours, and have not be tested like the discrete colours have.

Copy link
Contributor Author

Choose a reason for hiding this comment

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

OK, I expanded the description in commit afda6e9. Let me know if the text is OK.

(I also committed the .Rd file this time, while I forgot on the previous commit)

Copy link
Member

Choose a reason for hiding this comment

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

Much better, but can you please break the documentation lines at ~80 characters?

Copy link
Contributor Author

Choose a reason for hiding this comment

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

I think this should be the text editor's job (and TextMate actually does this quite well now) rather than mine (or yours) and I also feel that "justifying" text manually like this complicates diffs when one wants to add a word or two and has to shift every subsequent line... but I did ;)

#' @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")
}
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
}

#' @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")
}
continuous_scale("fill", "distiller",
gradient_n_pal(brewer_pal(type, palette)(6), values, space), na.value = na.value, ...)
}

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


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

#' @export #' @export
#' @rdname scale_gradient #' @rdname scale_gradient
scale_color_continuous <- scale_colour_gradient scale_color_continuous <- scale_colour_gradient
Expand Down
6 changes: 3 additions & 3 deletions man/coord_fixed.Rd
Expand Up @@ -33,9 +33,9 @@
# ensures that the ranges of axes are equal to the specified ratio by # ensures that the ranges of axes are equal to the specified ratio by
# adjusting the plot aspect ratio # adjusting the plot aspect ratio


qplot(mpg, wt, data = mtcars) + coord_equal(ratio = 1) qplot(mpg, wt, data = mtcars) + coord_fixed(ratio = 1)
qplot(mpg, wt, data = mtcars) + coord_equal(ratio = 5) qplot(mpg, wt, data = mtcars) + coord_fixed(ratio = 5)
qplot(mpg, wt, data = mtcars) + coord_equal(ratio = 1/5) qplot(mpg, wt, data = mtcars) + coord_fixed(ratio = 1/5)


# Resize the plot to see that the specified aspect ratio is maintained # Resize the plot to see that the specified aspect ratio is maintained
} }
Expand Down
43 changes: 43 additions & 0 deletions man/scale_brewer.Rd
@@ -1,14 +1,26 @@
\name{scale_colour_brewer} \name{scale_colour_brewer}
\alias{scale_color_brewer} \alias{scale_color_brewer}
\alias{scale_color_distiller}
\alias{scale_colour_brewer} \alias{scale_colour_brewer}
\alias{scale_colour_distiller}
\alias{scale_fill_brewer} \alias{scale_fill_brewer}
\alias{scale_fill_distiller}
\title{Sequential, diverging and qualitative colour scales from colorbrewer.org} \title{Sequential, diverging and qualitative colour scales from colorbrewer.org}
\usage{ \usage{
scale_colour_brewer(..., type = "seq", palette = 1) scale_colour_brewer(..., type = "seq", palette = 1)


scale_fill_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_brewer(..., type = "seq", palette = 1)

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

\item{na.value}{Colour to use for missing values}
} }
\description{ \description{
Create colour scales based on ColorBrewer colours.
}
\details{
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
the colours to a continuous scale, although the original
colour schemes (particularly the qualitative ones) were
not intended for this. The perceptual result is left to
the appreciation of the user.

See \url{http://colorbrewer2.org} for more information. See \url{http://colorbrewer2.org} for more information.
} }
\examples{ \examples{
Expand All @@ -46,6 +73,22 @@ d + scale_colour_brewer(palette="Set1")
ggplot(diamonds, aes(x=price, fill=cut)) + ggplot(diamonds, aes(x=price, fill=cut)) +
geom_histogram(position="dodge", binwidth=1000) + geom_histogram(position="dodge", binwidth=1000) +
scale_fill_brewer() 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{ \seealso{
Other colour scales: Other colour scales:
Expand Down
3 changes: 3 additions & 0 deletions man/scale_gradient.Rd
Expand Up @@ -95,18 +95,21 @@ qplot(mpg, wt, data = mtcars, colour = miss) +


Other colour scales: \code{\link{scale_color_brewer}}, Other colour scales: \code{\link{scale_color_brewer}},
\code{\link{scale_color_discrete}}, \code{\link{scale_color_discrete}},
\code{\link{scale_color_distiller}},
\code{\link{scale_color_gradient2}}, \code{\link{scale_color_gradient2}},
\code{\link{scale_color_gradientn}}, \code{\link{scale_color_gradientn}},
\code{\link{scale_color_grey}}, \code{\link{scale_color_grey}},
\code{\link{scale_color_hue}}, \code{\link{scale_color_hue}},
\code{\link{scale_colour_brewer}}, \code{\link{scale_colour_brewer}},
\code{\link{scale_colour_discrete}}, \code{\link{scale_colour_discrete}},
\code{\link{scale_colour_distiller}},
\code{\link{scale_colour_gradient2}}, \code{\link{scale_colour_gradient2}},
\code{\link{scale_colour_gradientn}}, \code{\link{scale_colour_gradientn}},
\code{\link{scale_colour_grey}}, \code{\link{scale_colour_grey}},
\code{\link{scale_colour_hue}}, \code{\link{scale_colour_hue}},
\code{\link{scale_fill_brewer}}, \code{\link{scale_fill_brewer}},
\code{\link{scale_fill_discrete}}, \code{\link{scale_fill_discrete}},
\code{\link{scale_fill_distiller}},
\code{\link{scale_fill_gradient2}}, \code{\link{scale_fill_gradient2}},
\code{\link{scale_fill_gradientn}}, \code{\link{scale_fill_gradientn}},
\code{\link{scale_fill_grey}}, \code{\link{scale_fill_grey}},
Expand Down
3 changes: 3 additions & 0 deletions man/scale_gradient2.Rd
Expand Up @@ -84,20 +84,23 @@ p + scale_fill_gradient2("fill")
Other colour scales: \code{\link{scale_color_brewer}}, Other colour scales: \code{\link{scale_color_brewer}},
\code{\link{scale_color_continuous}}, \code{\link{scale_color_continuous}},
\code{\link{scale_color_discrete}}, \code{\link{scale_color_discrete}},
\code{\link{scale_color_distiller}},
\code{\link{scale_color_gradient}}, \code{\link{scale_color_gradient}},
\code{\link{scale_color_gradientn}}, \code{\link{scale_color_gradientn}},
\code{\link{scale_color_grey}}, \code{\link{scale_color_grey}},
\code{\link{scale_color_hue}}, \code{\link{scale_color_hue}},
\code{\link{scale_colour_brewer}}, \code{\link{scale_colour_brewer}},
\code{\link{scale_colour_continuous}}, \code{\link{scale_colour_continuous}},
\code{\link{scale_colour_discrete}}, \code{\link{scale_colour_discrete}},
\code{\link{scale_colour_distiller}},
\code{\link{scale_colour_gradient}}, \code{\link{scale_colour_gradient}},
\code{\link{scale_colour_gradientn}}, \code{\link{scale_colour_gradientn}},
\code{\link{scale_colour_grey}}, \code{\link{scale_colour_grey}},
\code{\link{scale_colour_hue}}, \code{\link{scale_colour_hue}},
\code{\link{scale_fill_brewer}}, \code{\link{scale_fill_brewer}},
\code{\link{scale_fill_continuous}}, \code{\link{scale_fill_continuous}},
\code{\link{scale_fill_discrete}}, \code{\link{scale_fill_discrete}},
\code{\link{scale_fill_distiller}},
\code{\link{scale_fill_gradient}}, \code{\link{scale_fill_gradient}},
\code{\link{scale_fill_gradientn}}, \code{\link{scale_fill_gradientn}},
\code{\link{scale_fill_grey}}, \code{\link{scale_fill_grey}},
Expand Down
3 changes: 3 additions & 0 deletions man/scale_gradientn.Rd
Expand Up @@ -62,20 +62,23 @@ d + scale_colour_gradientn(colours = terrain.colors(10),
Other colour scales: \code{\link{scale_color_brewer}}, Other colour scales: \code{\link{scale_color_brewer}},
\code{\link{scale_color_continuous}}, \code{\link{scale_color_continuous}},
\code{\link{scale_color_discrete}}, \code{\link{scale_color_discrete}},
\code{\link{scale_color_distiller}},
\code{\link{scale_color_gradient}}, \code{\link{scale_color_gradient}},
\code{\link{scale_color_gradient2}}, \code{\link{scale_color_gradient2}},
\code{\link{scale_color_grey}}, \code{\link{scale_color_grey}},
\code{\link{scale_color_hue}}, \code{\link{scale_color_hue}},
\code{\link{scale_colour_brewer}}, \code{\link{scale_colour_brewer}},
\code{\link{scale_colour_continuous}}, \code{\link{scale_colour_continuous}},
\code{\link{scale_colour_discrete}}, \code{\link{scale_colour_discrete}},
\code{\link{scale_colour_distiller}},
\code{\link{scale_colour_gradient}}, \code{\link{scale_colour_gradient}},
\code{\link{scale_colour_gradient2}}, \code{\link{scale_colour_gradient2}},
\code{\link{scale_colour_grey}}, \code{\link{scale_colour_grey}},
\code{\link{scale_colour_hue}}, \code{\link{scale_colour_hue}},
\code{\link{scale_fill_brewer}}, \code{\link{scale_fill_brewer}},
\code{\link{scale_fill_continuous}}, \code{\link{scale_fill_continuous}},
\code{\link{scale_fill_discrete}}, \code{\link{scale_fill_discrete}},
\code{\link{scale_fill_distiller}},
\code{\link{scale_fill_gradient}}, \code{\link{scale_fill_gradient}},
\code{\link{scale_fill_gradient2}}, \code{\link{scale_fill_gradient2}},
\code{\link{scale_fill_grey}}, \code{\link{scale_fill_grey}},
Expand Down
3 changes: 3 additions & 0 deletions man/scale_grey.Rd
Expand Up @@ -45,20 +45,23 @@ qplot(mpg, wt, data = mtcars, colour = miss) +
Other colour scales: \code{\link{scale_color_brewer}}, Other colour scales: \code{\link{scale_color_brewer}},
\code{\link{scale_color_continuous}}, \code{\link{scale_color_continuous}},
\code{\link{scale_color_discrete}}, \code{\link{scale_color_discrete}},
\code{\link{scale_color_distiller}},
\code{\link{scale_color_gradient}}, \code{\link{scale_color_gradient}},
\code{\link{scale_color_gradient2}}, \code{\link{scale_color_gradient2}},
\code{\link{scale_color_gradientn}}, \code{\link{scale_color_gradientn}},
\code{\link{scale_color_hue}}, \code{\link{scale_color_hue}},
\code{\link{scale_colour_brewer}}, \code{\link{scale_colour_brewer}},
\code{\link{scale_colour_continuous}}, \code{\link{scale_colour_continuous}},
\code{\link{scale_colour_discrete}}, \code{\link{scale_colour_discrete}},
\code{\link{scale_colour_distiller}},
\code{\link{scale_colour_gradient}}, \code{\link{scale_colour_gradient}},
\code{\link{scale_colour_gradient2}}, \code{\link{scale_colour_gradient2}},
\code{\link{scale_colour_gradientn}}, \code{\link{scale_colour_gradientn}},
\code{\link{scale_colour_hue}}, \code{\link{scale_colour_hue}},
\code{\link{scale_fill_brewer}}, \code{\link{scale_fill_brewer}},
\code{\link{scale_fill_continuous}}, \code{\link{scale_fill_continuous}},
\code{\link{scale_fill_discrete}}, \code{\link{scale_fill_discrete}},
\code{\link{scale_fill_distiller}},
\code{\link{scale_fill_gradient}}, \code{\link{scale_fill_gradient}},
\code{\link{scale_fill_gradient2}}, \code{\link{scale_fill_gradient2}},
\code{\link{scale_fill_gradientn}}, \code{\link{scale_fill_gradientn}},
Expand Down
3 changes: 3 additions & 0 deletions man/scale_hue.Rd
Expand Up @@ -90,18 +90,21 @@ qplot(mpg, wt, data = mtcars, colour = miss) +
\seealso{ \seealso{
Other colour scales: \code{\link{scale_color_brewer}}, Other colour scales: \code{\link{scale_color_brewer}},
\code{\link{scale_color_continuous}}, \code{\link{scale_color_continuous}},
\code{\link{scale_color_distiller}},
\code{\link{scale_color_gradient}}, \code{\link{scale_color_gradient}},
\code{\link{scale_color_gradient2}}, \code{\link{scale_color_gradient2}},
\code{\link{scale_color_gradientn}}, \code{\link{scale_color_gradientn}},
\code{\link{scale_color_grey}}, \code{\link{scale_color_grey}},
\code{\link{scale_colour_brewer}}, \code{\link{scale_colour_brewer}},
\code{\link{scale_colour_continuous}}, \code{\link{scale_colour_continuous}},
\code{\link{scale_colour_distiller}},
\code{\link{scale_colour_gradient}}, \code{\link{scale_colour_gradient}},
\code{\link{scale_colour_gradient2}}, \code{\link{scale_colour_gradient2}},
\code{\link{scale_colour_gradientn}}, \code{\link{scale_colour_gradientn}},
\code{\link{scale_colour_grey}}, \code{\link{scale_colour_grey}},
\code{\link{scale_fill_brewer}}, \code{\link{scale_fill_brewer}},
\code{\link{scale_fill_continuous}}, \code{\link{scale_fill_continuous}},
\code{\link{scale_fill_distiller}},
\code{\link{scale_fill_gradient}}, \code{\link{scale_fill_gradient}},
\code{\link{scale_fill_gradient2}}, \code{\link{scale_fill_gradient2}},
\code{\link{scale_fill_gradientn}}, \code{\link{scale_fill_gradientn}},
Expand Down