Skip to content

Commit 407ec4f

Browse files
committed
v2.0
1 parent ec50075 commit 407ec4f

Some content is hidden

Large Commits have some content hidden by default. Use the searchbox below for content that may be hidden.

45 files changed

+1060
-155
lines changed

DESCRIPTION

+6-3
Original file line numberDiff line numberDiff line change
@@ -1,10 +1,12 @@
11
Package: ggsci
22
Type: Package
33
Title: Scientific Journal and Sci-Fi Themed Color Palettes for 'ggplot2'
4-
Version: 1.5
4+
Version: 2.0
55
Author: Nan Xiao <[email protected]>, Miaozhu Li <[email protected]>
66
Maintainer: Nan Xiao <[email protected]>
7-
Description: A collection of 'ggplot2' color palettes inspired by scientific journals and science fiction TV shows.
7+
Description: A collection of 'ggplot2' color palettes inspired by
8+
scientific journals, data visualization libraries, and
9+
science fiction TV shows.
810
License: GPL
911
LazyData: TRUE
1012
VignetteBuilder: knitr
@@ -19,5 +21,6 @@ Imports:
1921
Suggests:
2022
knitr,
2123
rmarkdown,
22-
gridExtra
24+
gridExtra,
25+
reshape2
2326
RoxygenNote: 5.0.1

NAMESPACE

+16
Original file line numberDiff line numberDiff line change
@@ -1,14 +1,21 @@
11
# Generated by roxygen2: do not edit by hand
22

33
export(pal_aaas)
4+
export(pal_d3)
5+
export(pal_futurama)
6+
export(pal_gsea)
47
export(pal_jco)
58
export(pal_lancet)
69
export(pal_npg)
710
export(pal_rickandmorty)
811
export(pal_simpsons)
912
export(pal_uchicago)
1013
export(pal_ucscgb)
14+
export(rgb_gsea)
1115
export(scale_color_aaas)
16+
export(scale_color_d3)
17+
export(scale_color_futurama)
18+
export(scale_color_gsea)
1219
export(scale_color_jco)
1320
export(scale_color_lancet)
1421
export(scale_color_npg)
@@ -17,6 +24,9 @@ export(scale_color_simpsons)
1724
export(scale_color_uchicago)
1825
export(scale_color_ucscgb)
1926
export(scale_colour_aaas)
27+
export(scale_colour_d3)
28+
export(scale_colour_futurama)
29+
export(scale_colour_gsea)
2030
export(scale_colour_jco)
2131
export(scale_colour_lancet)
2232
export(scale_colour_npg)
@@ -25,6 +35,9 @@ export(scale_colour_simpsons)
2535
export(scale_colour_uchicago)
2636
export(scale_colour_ucscgb)
2737
export(scale_fill_aaas)
38+
export(scale_fill_d3)
39+
export(scale_fill_futurama)
40+
export(scale_fill_gsea)
2841
export(scale_fill_jco)
2942
export(scale_fill_lancet)
3043
export(scale_fill_npg)
@@ -33,6 +46,9 @@ export(scale_fill_simpsons)
3346
export(scale_fill_uchicago)
3447
export(scale_fill_ucscgb)
3548
importFrom(ggplot2,discrete_scale)
49+
importFrom(ggplot2,scale_color_gradientn)
50+
importFrom(ggplot2,scale_fill_gradientn)
3651
importFrom(grDevices,col2rgb)
52+
importFrom(grDevices,colorRamp)
3753
importFrom(grDevices,rgb)
3854
importFrom(scales,manual_pal)

NEWS.md

+10
Original file line numberDiff line numberDiff line change
@@ -12,3 +12,13 @@
1212
* UChicago
1313
* Simpsons
1414
* Rick and Morty
15+
16+
# CHANGES IN ggsci VERSION 2.0 (2016-11-20)
17+
18+
## NEW FEATURES
19+
20+
* Two new discrete color palettes:
21+
* D3 color palette
22+
* Futurama color palette
23+
* The first continuous color palette:
24+
* GSEA GenePattern color palette

R/continuous.01.gsea.R

+127
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,127 @@
1+
#' The GSEA GenePattern Color Palettes
2+
#'
3+
#' Color palette inspired by the colors used in the
4+
#' heatmaps plotted by GSEA GenePattern.
5+
#'
6+
#' @param palette Palette type.
7+
#' Currently there is one available option: \code{"default"}
8+
#' (continuous palette with 12 base colors).
9+
#' @param n Number of individual colors to be generated.
10+
#' @param alpha Transparency level, a real number in (0, 1].
11+
#' See \code{alpha} in \code{\link[grDevices]{rgb}} for details.
12+
#' @param reverse Logical. Should the order of the colors be reversed?
13+
#'
14+
#' @export rgb_gsea
15+
#'
16+
#' @importFrom grDevices colorRamp rgb
17+
#' @importFrom scales manual_pal
18+
#'
19+
#' @author Nan Xiao <\email{me@@nanx.me}> |
20+
#' <\href{http://nanx.me}{http://nanx.me}>
21+
#'
22+
#' @note The 12 base colors used in this palette are derived from
23+
#' \href{ftp://ftp.broad.mit.edu/pub/genepattern/modules/HeatMapImage/broad.mit.edu:cancer.software.genepattern.module.analysis/00032/6/HeatMapImage.pdf}{this document}.
24+
#'
25+
#' @examples
26+
#' library("scales")
27+
#' show_col(pal_gsea("default")(12))
28+
#' show_col(pal_gsea("default", n = 30, alpha = 0.6, reverse = TRUE)(30))
29+
rgb_gsea = function (palette = c('default'), n = 12,
30+
alpha = 1, reverse = FALSE) {
31+
32+
palette = match.arg(palette)
33+
34+
if (alpha > 1L | alpha <= 0L) stop('alpha must be in (0, 1]')
35+
36+
raw_cols = ggsci_db$'gsea'[[palette]]
37+
func_cols = colorRamp(raw_cols, space = 'Lab', interpolate = 'spline')
38+
mat_cols = func_cols(seq(0L, 1L, length.out = n))
39+
alpha_cols = rgb(mat_cols[, 1L], mat_cols[, 2L], mat_cols[, 3L],
40+
alpha = alpha * 255L, maxColorValue = 255L)
41+
42+
if (reverse) alpha_cols = rev(alpha_cols)
43+
44+
alpha_cols
45+
46+
}
47+
48+
#' The GSEA GenePattern Color Palettes
49+
#'
50+
#' Color palette inspired by the colors used in the
51+
#' heatmaps plotted by GSEA GenePattern.
52+
#'
53+
#' @inheritParams rgb_gsea
54+
#'
55+
#' @export pal_gsea
56+
#'
57+
#' @importFrom scales manual_pal
58+
#'
59+
#' @author Nan Xiao <\email{me@@nanx.me}> |
60+
#' <\href{http://nanx.me}{http://nanx.me}>
61+
#'
62+
#' @examples
63+
#' library("scales")
64+
#' show_col(pal_gsea("default")(12))
65+
#' show_col(pal_gsea("default", n = 30, alpha = 0.6, reverse = TRUE)(30))
66+
pal_gsea = function (palette = c('default'), n = 12,
67+
alpha = 1, reverse = FALSE) {
68+
69+
palette = match.arg(palette)
70+
71+
alpha_cols = rgb_gsea(palette, n, alpha, reverse)
72+
manual_pal(unname(alpha_cols))
73+
74+
}
75+
76+
#' The GSEA GenePattern Color Scales
77+
#'
78+
#' See \code{\link{pal_gsea}} for details.
79+
#'
80+
#' @inheritParams pal_gsea
81+
#' @param ... additional parameters for \code{\link[ggplot2]{discrete_scale}}
82+
#'
83+
#' @export scale_color_gsea
84+
#'
85+
#' @importFrom ggplot2 scale_color_gradientn
86+
#'
87+
#' @author Nan Xiao <\email{me@@nanx.me}> |
88+
#' <\href{http://nanx.me}{http://nanx.me}>
89+
#'
90+
#' @rdname scale_gsea
91+
#'
92+
#' @examples
93+
#' library("ggplot2")
94+
#' library("reshape2")
95+
#' data("mtcars")
96+
#'
97+
#' cor = cor(mtcars)
98+
#' cor_melt = melt(cor)
99+
#'
100+
#' ggplot(cor_melt,
101+
#' aes(x = Var1, y = Var2, fill = value)) +
102+
#' geom_tile(colour = "black", size = 0.3) +
103+
#' theme_bw() + scale_fill_gsea()
104+
scale_color_gsea = function (palette = c('default'),
105+
alpha = 1, reverse = FALSE, ...) {
106+
107+
palette = match.arg(palette)
108+
scale_color_gradientn(colours = rgb_gsea(palette, n = 512, alpha = alpha,
109+
reverse = reverse), ...)
110+
111+
}
112+
113+
#' @export scale_colour_gsea
114+
#' @rdname scale_gsea
115+
scale_colour_gsea = scale_color_gsea
116+
117+
#' @export scale_fill_gsea
118+
#' @importFrom ggplot2 scale_fill_gradientn
119+
#' @rdname scale_gsea
120+
scale_fill_gsea = function (palette = c('default'), alpha = 1,
121+
reverse = FALSE, ...) {
122+
123+
palette = match.arg(palette)
124+
scale_fill_gradientn(colours = rgb_gsea(palette, n = 512, alpha = alpha,
125+
reverse = reverse), ...)
126+
127+
}

R/01.npg.R R/discrete.01.npg.R

+1-1
Original file line numberDiff line numberDiff line change
@@ -60,7 +60,7 @@ pal_npg = function (palette = c('nrc'), alpha = 1) {
6060
#' ggplot(subset(diamonds, carat >= 2.2),
6161
#' aes(x = table, y = price, colour = cut)) +
6262
#' geom_point(alpha = 0.7) +
63-
#' geom_smooth(alpha = 0.1, size = 1, span = 1) +
63+
#' geom_smooth(method = "loess", alpha = 0.1, size = 1, span = 1) +
6464
#' theme_bw() + scale_color_npg()
6565
#'
6666
#' ggplot(subset(diamonds, carat > 2.2 & depth > 55 & depth < 70),

R/02.aaas.R R/discrete.02.aaas.R

+1-1
Original file line numberDiff line numberDiff line change
@@ -61,7 +61,7 @@ pal_aaas = function (palette = c('default'), alpha = 1) {
6161
#' ggplot(subset(diamonds, carat >= 2.2),
6262
#' aes(x = table, y = price, colour = cut)) +
6363
#' geom_point(alpha = 0.7) +
64-
#' geom_smooth(alpha = 0.1, size = 1, span = 1) +
64+
#' geom_smooth(method = "loess", alpha = 0.1, size = 1, span = 1) +
6565
#' theme_bw() + scale_color_aaas()
6666
#'
6767
#' ggplot(subset(diamonds, carat > 2.2 & depth > 55 & depth < 70),

R/03.lancet.R R/discrete.03.lancet.R

+1-1
Original file line numberDiff line numberDiff line change
@@ -60,7 +60,7 @@ pal_lancet = function (palette = c('lanonc'), alpha = 1) {
6060
#' ggplot(subset(diamonds, carat >= 2.2),
6161
#' aes(x = table, y = price, colour = cut)) +
6262
#' geom_point(alpha = 0.7) +
63-
#' geom_smooth(alpha = 0.1, size = 1, span = 1) +
63+
#' geom_smooth(method = "loess", alpha = 0.1, size = 1, span = 1) +
6464
#' theme_bw() + scale_color_lancet()
6565
#'
6666
#' ggplot(subset(diamonds, carat > 2.2 & depth > 55 & depth < 70),

R/04.jco.R R/discrete.04.jco.R

+1-1
Original file line numberDiff line numberDiff line change
@@ -59,7 +59,7 @@ pal_jco = function (palette = c('default'), alpha = 1) {
5959
#' ggplot(subset(diamonds, carat >= 2.2),
6060
#' aes(x = table, y = price, colour = cut)) +
6161
#' geom_point(alpha = 0.7) +
62-
#' geom_smooth(alpha = 0.1, size = 1, span = 1) +
62+
#' geom_smooth(method = "loess", alpha = 0.1, size = 1, span = 1) +
6363
#' theme_bw() + scale_color_jco()
6464
#'
6565
#' ggplot(subset(diamonds, carat > 2.2 & depth > 55 & depth < 70),

R/05.ucscgb.R R/discrete.05.ucscgb.R

+1-1
Original file line numberDiff line numberDiff line change
@@ -59,7 +59,7 @@ pal_ucscgb = function (palette = c('default'), alpha = 1) {
5959
#' ggplot(subset(diamonds, carat >= 2.2),
6060
#' aes(x = table, y = price, colour = cut)) +
6161
#' geom_point(alpha = 0.7) +
62-
#' geom_smooth(alpha = 0.1, size = 1, span = 1) +
62+
#' geom_smooth(method = "loess", alpha = 0.1, size = 1, span = 1) +
6363
#' theme_bw() + scale_color_ucscgb()
6464
#'
6565
#' ggplot(subset(diamonds, carat > 2.2 & depth > 55 & depth < 70),

R/discrete.06.d3.R

+114
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,114 @@
1+
#' D3.js Color Palettes
2+
#'
3+
#' Color palettes based on the colors used by D3.js.
4+
#'
5+
#' @param palette Palette type.
6+
#' There are 4 available options:
7+
#' \code{"category10"} (10-color palette);
8+
#' \code{"category20"} (20-color palette);
9+
#' \code{"category20b"} (20-color palette);
10+
#' \code{"category20c"} (20-color palette).
11+
#' @param alpha Transparency level, a real number in (0, 1].
12+
#' See \code{alpha} in \code{\link[grDevices]{rgb}} for details.
13+
#'
14+
#' @export pal_d3
15+
#'
16+
#' @importFrom grDevices col2rgb rgb
17+
#' @importFrom scales manual_pal
18+
#'
19+
#' @author Nan Xiao <\email{me@@nanx.me}> |
20+
#' <\href{http://nanx.me}{http://nanx.me}>
21+
#'
22+
#' @references \url{https://github.com/d3/d3-3.x-api-reference/blob/master/Ordinal-Scales.md}
23+
#'
24+
#' @examples
25+
#' library("scales")
26+
#' show_col(pal_d3("category10")(10))
27+
#' show_col(pal_d3("category20")(20))
28+
#' show_col(pal_d3("category20b")(20))
29+
#' show_col(pal_d3("category20c")(20))
30+
pal_d3 = function (palette = c('category10', 'category20',
31+
'category20b', 'category20c'), alpha = 1) {
32+
33+
palette = match.arg(palette)
34+
35+
if (alpha > 1L | alpha <= 0L) stop('alpha must be in (0, 1]')
36+
37+
raw_cols = ggsci_db$'d3'[[palette]]
38+
raw_cols_rgb = col2rgb(raw_cols)
39+
alpha_cols = rgb(raw_cols_rgb[1L, ], raw_cols_rgb[2L, ], raw_cols_rgb[3L, ],
40+
alpha = alpha * 255L, names = names(raw_cols),
41+
maxColorValue = 255L)
42+
43+
manual_pal(unname(alpha_cols))
44+
45+
}
46+
47+
#' D3.js Color Scales
48+
#'
49+
#' See \code{\link{pal_d3}} for details.
50+
#'
51+
#' @inheritParams pal_d3
52+
#' @param ... additional parameters for \code{\link[ggplot2]{discrete_scale}}
53+
#'
54+
#' @export scale_color_d3
55+
#'
56+
#' @importFrom ggplot2 discrete_scale
57+
#'
58+
#' @author Nan Xiao <\email{me@@nanx.me}> |
59+
#' <\href{http://nanx.me}{http://nanx.me}>
60+
#'
61+
#' @references \url{https://github.com/d3/d3-3.x-api-reference/blob/master/Ordinal-Scales.md}
62+
#'
63+
#' @rdname scale_d3
64+
#'
65+
#' @examples
66+
#' library("ggplot2")
67+
#' data("diamonds")
68+
#'
69+
#' p1 = ggplot(subset(diamonds, carat >= 2.2),
70+
#' aes(x = table, y = price, colour = cut)) +
71+
#' geom_point(alpha = 0.7) +
72+
#' geom_smooth(method = "loess", alpha = 0.1, size = 1, span = 1) +
73+
#' theme_bw()
74+
#'
75+
#' p2 = ggplot(subset(diamonds, carat > 2.2 & depth > 55 & depth < 70),
76+
#' aes(x = depth, fill = cut)) +
77+
#' geom_histogram(colour = "black", binwidth = 1, position = "dodge") +
78+
#' theme_bw()
79+
#'
80+
#' p1 + scale_color_d3()
81+
#' p2 + scale_fill_d3()
82+
#'
83+
#' p1 + scale_color_d3(palette = "category20")
84+
#' p2 + scale_fill_d3(palette = "category20")
85+
#'
86+
#' p1 + scale_color_d3(palette = "category20b")
87+
#' p2 + scale_fill_d3(palette = "category20b")
88+
#'
89+
#' p1 + scale_color_d3(palette = "category20c")
90+
#' p2 + scale_fill_d3(palette = "category20c")
91+
scale_color_d3 = function (palette = c('category10', 'category20',
92+
'category20b', 'category20c'),
93+
alpha = 1, ...) {
94+
95+
palette = match.arg(palette)
96+
discrete_scale('colour', 'd3', pal_d3(palette, alpha), ...)
97+
98+
}
99+
100+
#' @export scale_colour_d3
101+
#' @rdname scale_d3
102+
scale_colour_d3 = scale_color_d3
103+
104+
#' @export scale_fill_d3
105+
#' @importFrom ggplot2 discrete_scale
106+
#' @rdname scale_d3
107+
scale_fill_d3 = function (palette = c('category10', 'category20',
108+
'category20b', 'category20c'),
109+
alpha = 1, ...) {
110+
111+
palette = match.arg(palette)
112+
discrete_scale('fill', 'd3', pal_d3(palette, alpha), ...)
113+
114+
}

R/06.uchicago.R R/discrete.07.uchicago.R

+1-1
Original file line numberDiff line numberDiff line change
@@ -66,7 +66,7 @@ pal_uchicago = function (palette = c('default', 'light', 'dark'), alpha = 1) {
6666
#' p1 = ggplot(subset(diamonds, carat >= 2.2),
6767
#' aes(x = table, y = price, colour = cut)) +
6868
#' geom_point(alpha = 0.7) +
69-
#' geom_smooth(alpha = 0.1, size = 1, span = 1) +
69+
#' geom_smooth(method = "loess", alpha = 0.1, size = 1, span = 1) +
7070
#' theme_bw()
7171
#'
7272
#' p2 = ggplot(subset(diamonds, carat > 2.2 & depth > 55 & depth < 70),

R/07.simpsons.R R/discrete.08.simpsons.R

+1-1
Original file line numberDiff line numberDiff line change
@@ -59,7 +59,7 @@ pal_simpsons = function (palette = c('springfield'), alpha = 1) {
5959
#' ggplot(subset(diamonds, carat >= 2.2),
6060
#' aes(x = table, y = price, colour = cut)) +
6161
#' geom_point(alpha = 0.7) +
62-
#' geom_smooth(alpha = 0.1, size = 1, span = 1) +
62+
#' geom_smooth(method = "loess", alpha = 0.1, size = 1, span = 1) +
6363
#' theme_bw() + scale_color_simpsons()
6464
#'
6565
#' ggplot(subset(diamonds, carat > 2.2 & depth > 55 & depth < 70),

0 commit comments

Comments
 (0)