Skip to content

Commit c46beb6

Browse files
committed
Merge pull request #925 from jiho/feature/brewer-continuous
Rebased version of continuous brewer color scales
2 parents 188c8e0 + d57c73f commit c46beb6

File tree

10 files changed

+140
-19
lines changed

10 files changed

+140
-19
lines changed

NAMESPACE

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -301,6 +301,7 @@ export(scale_area)
301301
export(scale_color_brewer)
302302
export(scale_color_continuous)
303303
export(scale_color_discrete)
304+
export(scale_color_distiller)
304305
export(scale_color_gradient)
305306
export(scale_color_gradient2)
306307
export(scale_color_gradientn)
@@ -311,6 +312,7 @@ export(scale_color_manual)
311312
export(scale_colour_brewer)
312313
export(scale_colour_continuous)
313314
export(scale_colour_discrete)
315+
export(scale_colour_distiller)
314316
export(scale_colour_gradient)
315317
export(scale_colour_gradient2)
316318
export(scale_colour_gradientn)
@@ -321,6 +323,7 @@ export(scale_colour_manual)
321323
export(scale_fill_brewer)
322324
export(scale_fill_continuous)
323325
export(scale_fill_discrete)
326+
export(scale_fill_distiller)
324327
export(scale_fill_gradient)
325328
export(scale_fill_gradient2)
326329
export(scale_fill_gradientn)

NEWS

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -8,6 +8,9 @@ ggplot2 0.9.3.1.99
88
to `theme_gray`. `theme_light` is similar but with light gray box and axes
99
around the plot, to emphasise content more (@jiho, #923)
1010

11+
* Allow to use brewer palettes for continuous scales, through the new
12+
`scale_fill/colour_distiller()` functions (@jiho, #925).
13+
1114
* Allow specifying only one of the limits in a scale and use the automatic
1215
calculation of the other limit by passing NA to to the limit function,
1316
`xlim()` or `ylim()` (@jimhester, #557).

R/scale-brewer.r

Lines changed: 63 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -1,33 +1,59 @@
11
#' Sequential, diverging and qualitative colour scales from colorbrewer.org
22
#'
3+
#' ColorBrewer provides sequential, diverging and qualitative colour schemes
4+
#' which are particularly suited and tested to display discrete values (levels
5+
#' of a factor) on a map. ggplot2 can use those colours in discrete scales. It
6+
#' also allows to smoothly interpolate 6 colours from any palette to a
7+
#' continuous scale (6 colours per palette gives nice gradients; more results in
8+
#' more saturated colours which do not look as good). However, the original
9+
#' colour schemes (particularly the qualitative ones) were not intended for this
10+
#' and the perceptual result is left to the appreciation of the user.
11+
#'
312
#' See \url{http://colorbrewer2.org} for more information.
413
#'
514
#' @inheritParams scales::brewer_pal
615
#' @inheritParams scale_colour_hue
16+
#' @inheritParams scale_colour_gradient
717
#' @family colour scales
818
#' @rdname scale_brewer
919
#' @export
1020
#' @examples
1121
#' dsamp <- diamonds[sample(nrow(diamonds), 1000), ]
12-
#' (d <- qplot(carat, price, data=dsamp, colour=clarity))
22+
#' (d <- qplot(carat, price, data = dsamp, colour = clarity))
1323
#'
1424
#' # Change scale label
1525
#' d + scale_colour_brewer()
1626
#' d + scale_colour_brewer("clarity")
1727
#' d + scale_colour_brewer(expression(clarity[beta]))
1828
#'
1929
#' # Select brewer palette to use, see ?scales::brewer_pal for more details
20-
#' d + scale_colour_brewer(type="seq")
21-
#' d + scale_colour_brewer(type="seq", palette=3)
30+
#' d + scale_colour_brewer(type = "seq")
31+
#' d + scale_colour_brewer(type = "seq", palette = 3)
2232
#'
23-
#' d + scale_colour_brewer(palette="Blues")
24-
#' d + scale_colour_brewer(palette="Set1")
33+
#' d + scale_colour_brewer(palette = "Blues")
34+
#' d + scale_colour_brewer(palette = "Set1")
2535
#'
2636
#' # scale_fill_brewer works just the same as
2737
#' # scale_colour_brewer but for fill colours
28-
#' ggplot(diamonds, aes(x=price, fill=cut)) +
29-
#' geom_histogram(position="dodge", binwidth=1000) +
38+
#' ggplot(diamonds, aes(x = price, fill = cut)) +
39+
#' geom_histogram(position = "dodge", binwidth = 1000) +
3040
#' scale_fill_brewer()
41+
#'
42+
#' # Generate map data
43+
#' library(reshape2) # for melt
44+
#' volcano3d <- melt(volcano)
45+
#' names(volcano3d) <- c("x", "y", "z")
46+
#'
47+
#' # Basic plot
48+
#' v <- ggplot() + geom_tile(aes(x = x, y = y, fill = z), data = volcano3d)
49+
#' v
50+
#' v + scale_fill_distiller()
51+
#' v + scale_fill_distiller(palette = 2)
52+
#' v + scale_fill_distiller(type = "div")
53+
#' v + scale_fill_distiller(palette = "Spectral")
54+
#' v + scale_fill_distiller(palette = "Spectral", trans = "reverse")
55+
#' v + scale_fill_distiller(type = "qual")
56+
#' # Not appropriate for continuous data, issues a warning
3157
scale_colour_brewer <- function(..., type = "seq", palette = 1) {
3258
discrete_scale("colour", "brewer", brewer_pal(type, palette), ...)
3359
}
@@ -37,3 +63,33 @@ scale_colour_brewer <- function(..., type = "seq", palette = 1) {
3763
scale_fill_brewer <- function(..., type = "seq", palette = 1) {
3864
discrete_scale("fill", "brewer", brewer_pal(type, palette), ...)
3965
}
66+
67+
#' @export
68+
#' @rdname scale_brewer
69+
scale_colour_distiller <- function(..., type = "seq", palette = 1, values = NULL, space = "Lab", na.value = "grey50") {
70+
# warn about using a qualitative brewer palette to generate the gradient
71+
type <- match.arg(type, c("seq", "div", "qual"))
72+
if (type == "qual") {
73+
warning("Using a discrete colour palette in a continuous scale.\n Consider using type = \"seq\" or type = \"div\" instead", call. = FALSE)
74+
}
75+
continuous_scale("colour", "distiller",
76+
gradient_n_pal(brewer_pal(type, palette)(6), values, space), na.value = na.value, ...)
77+
# NB: 6 colours per palette gives nice gradients; more results in more saturated colours which do not look as good
78+
}
79+
80+
#' @export
81+
#' @rdname scale_brewer
82+
scale_fill_distiller <- function(..., type = "seq", palette = 1, values = NULL, space = "Lab", na.value = "grey50") {
83+
type <- match.arg(type, c("seq", "div", "qual"))
84+
if (type == "qual") {
85+
warning("Using a discrete colour palette in a continuous scale.\n Consider using type = \"seq\" or type = \"div\" instead", call. = FALSE)
86+
}
87+
continuous_scale("fill", "distiller",
88+
gradient_n_pal(brewer_pal(type, palette)(6), values, space), na.value = na.value, ...)
89+
}
90+
91+
# icon.brewer <- function() {
92+
# rectGrob(c(0.1, 0.3, 0.5, 0.7, 0.9), width = 0.21,
93+
# gp = gpar(fill = RColorBrewer::brewer.pal(5, "PuOr"), col = NA)
94+
# )
95+
# }

R/zxx.r

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -22,6 +22,10 @@ scale_fill_continuous <- scale_fill_gradient
2222
#' @rdname scale_brewer
2323
scale_color_brewer <- scale_colour_brewer
2424

25+
#' @export
26+
#' @rdname scale_brewer
27+
scale_color_distiller <- scale_colour_distiller
28+
2529
#' @export
2630
#' @rdname scale_gradient
2731
scale_color_continuous <- scale_colour_gradient

man/scale_brewer.Rd

Lines changed: 47 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -1,15 +1,27 @@
11
% Generated by roxygen2 (4.0.0): do not edit by hand
22
\name{scale_colour_brewer}
33
\alias{scale_color_brewer}
4+
\alias{scale_color_distiller}
45
\alias{scale_colour_brewer}
6+
\alias{scale_colour_distiller}
57
\alias{scale_fill_brewer}
8+
\alias{scale_fill_distiller}
69
\title{Sequential, diverging and qualitative colour scales from colorbrewer.org}
710
\usage{
811
scale_colour_brewer(..., type = "seq", palette = 1)
912

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

15+
scale_colour_distiller(..., type = "seq", palette = 1, values = NULL,
16+
space = "Lab", na.value = "grey50")
17+
18+
scale_fill_distiller(..., type = "seq", palette = 1, values = NULL,
19+
space = "Lab", na.value = "grey50")
20+
1221
scale_color_brewer(..., type = "seq", palette = 1)
22+
23+
scale_color_distiller(..., type = "seq", palette = 1, values = NULL,
24+
space = "Lab", na.value = "grey50")
1325
}
1426
\arguments{
1527
\item{type}{One of seq (sequential), div (diverging) or
@@ -22,31 +34,59 @@ scale_color_brewer(..., type = "seq", palette = 1)
2234
\item{...}{Other arguments passed on to
2335
\code{\link{discrete_scale}} to control name, limits,
2436
breaks, labels and so forth.}
37+
38+
\item{na.value}{Colour to use for missing values}
2539
}
2640
\description{
41+
ColorBrewer provides sequential, diverging and qualitative colour schemes
42+
which are particularly suited and tested to display discrete values (levels
43+
of a factor) on a map. ggplot2 can use those colours in discrete scales. It
44+
also allows to smoothly interpolate 6 colours from any palette to a
45+
continuous scale (6 colours per palette gives nice gradients; more results in
46+
more saturated colours which do not look as good). However, the original
47+
colour schemes (particularly the qualitative ones) were not intended for this
48+
and the perceptual result is left to the appreciation of the user.
49+
}
50+
\details{
2751
See \url{http://colorbrewer2.org} for more information.
2852
}
2953
\examples{
3054
dsamp <- diamonds[sample(nrow(diamonds), 1000), ]
31-
(d <- qplot(carat, price, data=dsamp, colour=clarity))
55+
(d <- qplot(carat, price, data = dsamp, colour = clarity))
3256

3357
# Change scale label
3458
d + scale_colour_brewer()
3559
d + scale_colour_brewer("clarity")
3660
d + scale_colour_brewer(expression(clarity[beta]))
3761

3862
# Select brewer palette to use, see ?scales::brewer_pal for more details
39-
d + scale_colour_brewer(type="seq")
40-
d + scale_colour_brewer(type="seq", palette=3)
63+
d + scale_colour_brewer(type = "seq")
64+
d + scale_colour_brewer(type = "seq", palette = 3)
4165

42-
d + scale_colour_brewer(palette="Blues")
43-
d + scale_colour_brewer(palette="Set1")
66+
d + scale_colour_brewer(palette = "Blues")
67+
d + scale_colour_brewer(palette = "Set1")
4468

4569
# scale_fill_brewer works just the same as
4670
# scale_colour_brewer but for fill colours
47-
ggplot(diamonds, aes(x=price, fill=cut)) +
48-
geom_histogram(position="dodge", binwidth=1000) +
71+
ggplot(diamonds, aes(x = price, fill = cut)) +
72+
geom_histogram(position = "dodge", binwidth = 1000) +
4973
scale_fill_brewer()
74+
75+
# Generate map data
76+
library(reshape2) # for melt
77+
volcano3d <- melt(volcano)
78+
names(volcano3d) <- c("x", "y", "z")
79+
80+
# Basic plot
81+
v <- ggplot() + geom_tile(aes(x = x, y = y, fill = z), data = volcano3d)
82+
v
83+
v + scale_fill_distiller()
84+
v + scale_fill_distiller(palette = 2)
85+
v + scale_fill_distiller(type = "div")
86+
v + scale_fill_distiller(palette = "Spectral")
87+
v + scale_fill_distiller(palette = "Spectral", trans = "reverse")
88+
v + scale_fill_distiller(type = "qual")
89+
# Not appropriate for continuous data, issues a warning
5090
}
5191
\seealso{
5292
Other colour scales: \code{\link{scale_color_continuous}},

man/scale_gradient.Rd

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -99,8 +99,11 @@ qplot(mpg, wt, data = mtcars, colour = miss) +
9999
palette
100100

101101
Other colour scales: \code{\link{scale_color_brewer}},
102+
\code{\link{scale_color_distiller}},
102103
\code{\link{scale_colour_brewer}},
103-
\code{\link{scale_fill_brewer}};
104+
\code{\link{scale_colour_distiller}},
105+
\code{\link{scale_fill_brewer}},
106+
\code{\link{scale_fill_distiller}};
104107
\code{\link{scale_color_discrete}},
105108
\code{\link{scale_color_hue}},
106109
\code{\link{scale_colour_discrete}},

man/scale_gradient2.Rd

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -87,8 +87,11 @@ p + scale_fill_gradient2("fill")
8787
}
8888
\seealso{
8989
Other colour scales: \code{\link{scale_color_brewer}},
90+
\code{\link{scale_color_distiller}},
9091
\code{\link{scale_colour_brewer}},
91-
\code{\link{scale_fill_brewer}};
92+
\code{\link{scale_colour_distiller}},
93+
\code{\link{scale_fill_brewer}},
94+
\code{\link{scale_fill_distiller}};
9295
\code{\link{scale_color_continuous}},
9396
\code{\link{scale_color_gradient}},
9497
\code{\link{scale_colour_continuous}},

man/scale_gradientn.Rd

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -68,8 +68,11 @@ d + scale_colour_gradientn(colours = terrain.colors(10),
6868
}
6969
\seealso{
7070
Other colour scales: \code{\link{scale_color_brewer}},
71+
\code{\link{scale_color_distiller}},
7172
\code{\link{scale_colour_brewer}},
72-
\code{\link{scale_fill_brewer}};
73+
\code{\link{scale_colour_distiller}},
74+
\code{\link{scale_fill_brewer}},
75+
\code{\link{scale_fill_distiller}};
7376
\code{\link{scale_color_continuous}},
7477
\code{\link{scale_color_gradient}},
7578
\code{\link{scale_colour_continuous}},

man/scale_grey.Rd

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -41,8 +41,11 @@ qplot(mpg, wt, data = mtcars, colour = miss) +
4141
}
4242
\seealso{
4343
Other colour scales: \code{\link{scale_color_brewer}},
44+
\code{\link{scale_color_distiller}},
4445
\code{\link{scale_colour_brewer}},
45-
\code{\link{scale_fill_brewer}};
46+
\code{\link{scale_colour_distiller}},
47+
\code{\link{scale_fill_brewer}},
48+
\code{\link{scale_fill_distiller}};
4649
\code{\link{scale_color_continuous}},
4750
\code{\link{scale_color_gradient}},
4851
\code{\link{scale_colour_continuous}},

man/scale_hue.Rd

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -86,8 +86,11 @@ qplot(mpg, wt, data = mtcars, colour = miss) +
8686
}
8787
\seealso{
8888
Other colour scales: \code{\link{scale_color_brewer}},
89+
\code{\link{scale_color_distiller}},
8990
\code{\link{scale_colour_brewer}},
90-
\code{\link{scale_fill_brewer}};
91+
\code{\link{scale_colour_distiller}},
92+
\code{\link{scale_fill_brewer}},
93+
\code{\link{scale_fill_distiller}};
9194
\code{\link{scale_color_continuous}},
9295
\code{\link{scale_color_gradient}},
9396
\code{\link{scale_colour_continuous}},

0 commit comments

Comments
 (0)