Skip to content

Commit 74c4609

Browse files
committed
Add a new continuous colour/fill scale using colorbrewer colours
It fetches 6 colours from a colorbrewer palette and feeds them to gradientn. It gives very nice results even if the palettes were not intended for that use originally.
1 parent ae175ae commit 74c4609

File tree

8 files changed

+93
-0
lines changed

8 files changed

+93
-0
lines changed

NAMESPACE

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -309,6 +309,7 @@ export(scale_color_hue)
309309
export(scale_color_identity)
310310
export(scale_color_manual)
311311
export(scale_colour_brewer)
312+
export(scale_colour_brewerc)
312313
export(scale_colour_continuous)
313314
export(scale_colour_discrete)
314315
export(scale_colour_gradient)
@@ -319,6 +320,7 @@ export(scale_colour_hue)
319320
export(scale_colour_identity)
320321
export(scale_colour_manual)
321322
export(scale_fill_brewer)
323+
export(scale_fill_brewerc)
322324
export(scale_fill_continuous)
323325
export(scale_fill_discrete)
324326
export(scale_fill_gradient)

R/scale-brewer.r

Lines changed: 30 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -37,3 +37,33 @@ scale_colour_brewer <- function(..., type = "seq", palette = 1) {
3737
scale_fill_brewer <- function(..., type = "seq", palette = 1) {
3838
discrete_scale("fill", "brewer", brewer_pal(type, palette), ...)
3939
}
40+
41+
#' @export
42+
#' @rdname scale_brewer
43+
scale_colour_brewerc <- function(..., type = "seq", palette = 1, values = NULL, space = "Lab", na.value = "grey50") {
44+
# warn about using a qualitative brewer palette to generate the gradient
45+
type <- match.arg(type, c("seq", "div", "qual"))
46+
if (type == "qual") {
47+
warning("Using a discrete colour palette in a continuous scale.\n Consider using type=\"seq\" or type=\"div\" instead")
48+
}
49+
continuous_scale("colour", "brewerc",
50+
gradient_n_pal(brewer_pal(type, palette)(6), values, space), na.value = na.value, ...)
51+
# NB: 6 colours per palette gives nice gradients; more results in more saturated colours which do not look as good
52+
}
53+
54+
#' @export
55+
#' @rdname scale_brewer
56+
scale_fill_brewerc <- function(..., type = "seq", palette = 1, values = NULL, space = "Lab", na.value = "grey50") {
57+
type <- match.arg(type, c("seq", "div", "qual"))
58+
if (type == "qual") {
59+
warning("Using a discrete colour palette in a continuous scale.\n Consider using type=\"seq\" or type=\"div\" instead")
60+
}
61+
continuous_scale("fill", "brewerc",
62+
gradient_n_pal(brewer_pal(type, palette)(6), values, space), na.value = na.value, ...)
63+
}
64+
65+
# icon.brewer <- function() {
66+
# rectGrob(c(0.1, 0.3, 0.5, 0.7, 0.9), width=0.21,
67+
# gp=gpar(fill=RColorBrewer::brewer.pal(5, "PuOr"), col=NA)
68+
# )
69+
# }

man/scale_brewer.Rd

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2,13 +2,21 @@
22
\name{scale_colour_brewer}
33
\alias{scale_color_brewer}
44
\alias{scale_colour_brewer}
5+
\alias{scale_colour_brewerc}
56
\alias{scale_fill_brewer}
7+
\alias{scale_fill_brewerc}
68
\title{Sequential, diverging and qualitative colour scales from colorbrewer.org}
79
\usage{
810
scale_colour_brewer(..., type = "seq", palette = 1)
911

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

14+
scale_colour_brewerc(..., type = "seq", palette = 1,
15+
values = NULL, space = "Lab", na.value = "grey50")
16+
17+
scale_fill_brewerc(..., type = "seq", palette = 1,
18+
values = NULL, space = "Lab", na.value = "grey50")
19+
1220
scale_color_brewer(..., type = "seq", palette = 1)
1321
}
1422
\arguments{
@@ -22,6 +30,8 @@ scale_color_brewer(..., type = "seq", palette = 1)
2230
\item{...}{Other arguments passed on to
2331
\code{\link{discrete_scale}} to control name, limits,
2432
breaks, labels and so forth.}
33+
34+
\item{na.value}{Colour to use for missing values}
2535
}
2636
\description{
2737
See \url{http://colorbrewer2.org} for more information.

man/scale_gradient.Rd

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -103,6 +103,8 @@ Other colour scales: \code{\link{scale_color_brewer}},
103103
\code{\link{scale_fill_brewer}};
104104
\code{\link{scale_color_discrete}},
105105
\code{\link{scale_color_hue}},
106+
\code{\link{scale_colour_brewer}},
107+
\code{\link{scale_colour_brewerc}},
106108
\code{\link{scale_colour_discrete}},
107109
\code{\link{scale_colour_hue}},
108110
\code{\link{scale_fill_discrete}},
@@ -116,5 +118,13 @@ Other colour scales: \code{\link{scale_color_brewer}},
116118
\code{\link{scale_color_grey}},
117119
\code{\link{scale_colour_grey}},
118120
\code{\link{scale_fill_grey}}
121+
\code{\link{scale_colour_hue}},
122+
\code{\link{scale_fill_brewer}},
123+
\code{\link{scale_fill_brewerc}},
124+
\code{\link{scale_fill_discrete}},
125+
\code{\link{scale_fill_gradient2}},
126+
\code{\link{scale_fill_gradientn}},
127+
\code{\link{scale_fill_grey}},
128+
\code{\link{scale_fill_hue}}
119129
}
120130

man/scale_gradient2.Rd

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -91,8 +91,18 @@ Other colour scales: \code{\link{scale_color_brewer}},
9191
\code{\link{scale_fill_brewer}};
9292
\code{\link{scale_color_continuous}},
9393
\code{\link{scale_color_gradient}},
94+
\code{\link{scale_color_gradientn}},
95+
\code{\link{scale_color_grey}},
96+
\code{\link{scale_color_hue}},
97+
\code{\link{scale_colour_brewer}},
98+
\code{\link{scale_colour_brewerc}},
9499
\code{\link{scale_colour_continuous}},
95100
\code{\link{scale_colour_gradient}},
101+
\code{\link{scale_colour_gradientn}},
102+
\code{\link{scale_colour_grey}},
103+
\code{\link{scale_colour_hue}},
104+
\code{\link{scale_fill_brewer}},
105+
\code{\link{scale_fill_brewerc}},
96106
\code{\link{scale_fill_continuous}},
97107
\code{\link{scale_fill_gradient}};
98108
\code{\link{scale_color_discrete}},

man/scale_gradientn.Rd

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -72,8 +72,18 @@ Other colour scales: \code{\link{scale_color_brewer}},
7272
\code{\link{scale_fill_brewer}};
7373
\code{\link{scale_color_continuous}},
7474
\code{\link{scale_color_gradient}},
75+
\code{\link{scale_color_gradient2}},
76+
\code{\link{scale_color_grey}},
77+
\code{\link{scale_color_hue}},
78+
\code{\link{scale_colour_brewer}},
79+
\code{\link{scale_colour_brewerc}},
7580
\code{\link{scale_colour_continuous}},
7681
\code{\link{scale_colour_gradient}},
82+
\code{\link{scale_colour_gradient2}},
83+
\code{\link{scale_colour_grey}},
84+
\code{\link{scale_colour_hue}},
85+
\code{\link{scale_fill_brewer}},
86+
\code{\link{scale_fill_brewerc}},
7787
\code{\link{scale_fill_continuous}},
7888
\code{\link{scale_fill_gradient}};
7989
\code{\link{scale_color_discrete}},

man/scale_grey.Rd

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -45,8 +45,18 @@ Other colour scales: \code{\link{scale_color_brewer}},
4545
\code{\link{scale_fill_brewer}};
4646
\code{\link{scale_color_continuous}},
4747
\code{\link{scale_color_gradient}},
48+
\code{\link{scale_color_gradient2}},
49+
\code{\link{scale_color_gradientn}},
50+
\code{\link{scale_color_hue}},
51+
\code{\link{scale_colour_brewer}},
52+
\code{\link{scale_colour_brewerc}},
4853
\code{\link{scale_colour_continuous}},
4954
\code{\link{scale_colour_gradient}},
55+
\code{\link{scale_colour_gradient2}},
56+
\code{\link{scale_colour_gradientn}},
57+
\code{\link{scale_colour_hue}},
58+
\code{\link{scale_fill_brewer}},
59+
\code{\link{scale_fill_brewerc}},
5060
\code{\link{scale_fill_continuous}},
5161
\code{\link{scale_fill_gradient}};
5262
\code{\link{scale_color_discrete}},

man/scale_hue.Rd

Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -90,6 +90,11 @@ Other colour scales: \code{\link{scale_color_brewer}},
9090
\code{\link{scale_fill_brewer}};
9191
\code{\link{scale_color_continuous}},
9292
\code{\link{scale_color_gradient}},
93+
\code{\link{scale_color_gradient2}},
94+
\code{\link{scale_color_gradientn}},
95+
\code{\link{scale_color_grey}},
96+
\code{\link{scale_colour_brewer}},
97+
\code{\link{scale_colour_brewerc}},
9398
\code{\link{scale_colour_continuous}},
9499
\code{\link{scale_colour_gradient}},
95100
\code{\link{scale_fill_continuous}},
@@ -102,6 +107,12 @@ Other colour scales: \code{\link{scale_color_brewer}},
102107
\code{\link{scale_fill_gradientn}};
103108
\code{\link{scale_color_grey}},
104109
\code{\link{scale_colour_grey}},
110+
\code{\link{scale_fill_brewer}},
111+
\code{\link{scale_fill_brewerc}},
112+
\code{\link{scale_fill_continuous}},
113+
\code{\link{scale_fill_gradient}},
114+
\code{\link{scale_fill_gradient2}},
115+
\code{\link{scale_fill_gradientn}},
105116
\code{\link{scale_fill_grey}}
106117
}
107118

0 commit comments

Comments
 (0)