2019-05-21

copy from https://cran.rproject.org/web/packages/ggsci/vignettes/ggsci.html
https://mp.weixin.qq.com/s/Y3H6CCgIiymiOfaQ8Unb7w

1 Introduction

My eyes were finally opened and I understood nature.

I learned at the same time to love it.

— Claude Monet

ggsci offers a collection of high-quality color palettes inspired by colors used in scientific journals, data visualization libraries, science fiction movies, and TV shows. The color palettes in ggsci are available as ggplot2scales.
(ggsci是ggplot的主题包,它提供一系列高质量的调色板,其灵感来自于科学期刊、数据可视化库、科幻电影和电视节目中使用的颜色。ggsci中的调色板可以作为ggplot2 scales使用。)
For all the color palettes, the corresponding scales are named as:
(所有有调色板通用格式为:)

scale_color_palname()
scale_fill_palname()

We also provided aliases, such as scale_colour_palname() for scale_color_palname().
使用的时候将palname替换为调色板的名称即可。
目前ggsci一共含有18中配色方案:

image.png

2 Discrete Color Palettes

We will use scatterplots with smooth curves, and bar plots to demonstrate the discrete color palettes in ggsci.

library("ggsci")
library("ggplot2")
library("gridExtra")
data("diamonds")
p1 = ggplot(subset(diamonds, carat >= 2.2), aes(x = table, y = price, colour = cut)) +geom_point(alpha = 0.7) + geom_smooth(method = "loess", alpha = 0.05, size = 1, span = 1) +theme_bw()
p2 = ggplot(subset(diamonds, carat > 2.2 & depth > 55 & depth < 70), aes(x = depth, fill = cut)) +geom_histogram(colour = "black", binwidth = 1, position = "dodge") + theme_bw()
p1

p2

2.1 NPG

The NPG palette is inspired by the plots in the journals published by Nature Publishing Group:
(NPC调色板的灵感来源于NATURE)

p1_npg = p1 + scale_color_npg()
p2_npg = p2 + scale_fill_npg()
grid.arrange(p1_npg, p2_npg, ncol = 2)
image.png

2.2 AAAS

The AAAS palette is inspired by the plots in the journals published by American Association for the Advancement of Science:
(AAAS 调色板的灵感来源于Science)

p1_aaas = p1 + scale_color_aaas()
p2_aaas = p2 + scale_fill_aaas()
grid.arrange(p1_aaas, p2_aaas, ncol = 2)
image.png

2.3 NEJM

The NEJM palette is inspired by the plots in The New England Journal of Medicine:
(NEJM调色板的灵感来源于新英格兰医学杂志)

p1_nejm = p1 + scale_color_nejm()
p2_nejm = p2 + scale_fill_nejm()
grid.arrange(p1_nejm, p2_nejm, ncol = 2)
image.png

2.4 Lancet

The Lancet palette is inspired by the plots in Lancet journals, such as Lancet Oncology:
(Lancet调色板的灵感来源于柳叶刀杂志)

p1_lancet = p1 + scale_color_lancet()
p2_lancet = p2 + scale_fill_lancet()
grid.arrange(p1_lancet, p2_lancet, ncol = 2)
image.png

2.5 JAMA

The JAMA palette is inspired by the plots in The Journal of the American Medical Association:
(JAMA调色板的灵感来源于美国医学协会杂志)

p1_jama = p1 + scale_color_jama()
p2_jama = p2 + scale_fill_jama()
grid.arrange(p1_jama, p2_jama, ncol = 2)
image.png

2.6 JCO

The JCO palette is inspired by the the plots in Journal of Clinical Oncology:
(JCO调色板的灵感来源于临床肿瘤杂志)

p1_jco = p1 + scale_color_jco()
p2_jco = p2 + scale_fill_jco()
grid.arrange(p1_jco, p2_jco, ncol = 2)
image.png

2.7 UCSCGB

The UCSCGB palette is from the colors used by UCSC Genome Browser for representing chromosomes. This palette has been intensively used in visualizations produced by Circos.
(UCSCGB调色板的灵感来源于UCSC 基因组浏览器,用于表示染色体的颜色。)

p1_ucscgb = p1 + scale_color_ucscgb()
p2_ucscgb = p2 + scale_fill_ucscgb()
grid.arrange(p1_ucscgb, p2_ucscgb, ncol = 2)
image.png

2.8 D3

The D3 palette is from the categorical colors used by D3.js (version 3.x and before). There are four palette types (category10, category20, category20b, category20c) available.
D3调色板的灵感来源于D3.js使用的分类颜色。有四种调色板类型(category10、category20、category20b、category20c)。

p1_d3 = p1 + scale_color_d3()
p2_d3 = p2 + scale_fill_d3()
grid.arrange(p1_d3, p2_d3, ncol = 2)
image.png

2.9 LocusZoom

The LocusZoom palette is based on the colors used by LocusZoom.
LocusZoom调色板的灵感来源于LocusZoom网站,其提供GWAS结果的快速可视化。

p1_locuszoom = p1 + scale_color_locuszoom()
p2_locuszoom = p2 + scale_fill_locuszoom()
grid.arrange(p1_locuszoom, p2_locuszoom, ncol = 2)
image.png

2.10 IGV

The IGV palette is from the colors used by Integrative Genomics Viewer for representing chromosomes. There are two palette types (default, alternating) available.
IGV调色板来自整合基因组查看器用于表示染色体的颜色。

p1_igv_default = p1 + scale_color_igv()
p2_igv_default = p2 + scale_fill_igv()
grid.arrange(p1_igv_default, p2_igv_default, ncol = 2)
image.png

2.11 UChicago

The UChicago palette is based on the colors used by the <emph style="font-style: oblique;">University of Chicago</emph>. There are three palette types (default, light, dark) available.
UChicago调色板的灵感来源于芝加哥大学

p1_uchicago = p1 + scale_color_uchicago()
p2_uchicago = p2 + scale_fill_uchicago()
grid.arrange(p1_uchicago, p2_uchicago, ncol = 2)
image.png

2.12 Star Trek

This palette is inspired by the (uniform) colors in Star Trek:
StarTrek调色板的灵感来源于《星际迷航》

p1_startrek = p1 + scale_color_startrek()
p2_startrek = p2 + scale_fill_startrek()
grid.arrange(p1_startrek, p2_startrek, ncol = 2)
image.png

2.13 Tron Legacy

This palette is inspired by the colors used in Tron Legacy. It is suitable for displaying data when using a dark theme:
TronLegacy调色板的灵感来源于《创战记》,它适用于在使用暗主题时显示数据。

p1_tron = p1 + theme_dark() + theme(
    panel.background = element_rect(fill = "#2D2D2D"),
    legend.key = element_rect(fill = "#2D2D2D")) +
  scale_color_tron()
p2_tron = p2 + theme_dark() + theme(
    panel.background = element_rect(fill = "#2D2D2D")) +
  scale_fill_tron()
grid.arrange(p1_tron, p2_tron, ncol = 2)
image.png

2.14 Futurama

This palette is inspired by the colors used in the TV show Futurama:
Futurama调色板的灵感来源于电视《Futurama》

p1_futurama = p1 + scale_color_futurama()
p2_futurama = p2 + scale_fill_futurama()
grid.arrange(p1_futurama, p2_futurama, ncol = 2)
image.png

2.15 Rick and Morty

This palette is inspired by the colors used in the TV show Rick and Morty:
Rick and Morty调色板的灵感来源于电视《 Rick and Morty》

p1_rickandmorty = p1 + scale_color_rickandmorty()
p2_rickandmorty = p2 + scale_fill_rickandmorty()
grid.arrange(p1_rickandmorty, p2_rickandmorty, ncol = 2)
image.png

2.16 The Simpsons

This palette is inspired by the colors used in the TV show The Simpsons:
TheSimpsons调色板的灵感来源于电视《辛普森一家》

p1_simpsons = p1 + scale_color_simpsons()
p2_simpsons = p2 + scale_fill_simpsons()
grid.arrange(p1_simpsons, p2_simpsons, ncol = 2)
image.png

3 Continuous Color Palettes

We will use a correlation matrix visualization (a special type of heatmap) to demonstrate the continuous color palettes in ggsci.

library("reshape2")

data("mtcars")
cor = cor(unname(cbind(mtcars, mtcars, mtcars, mtcars)))
cor_melt = melt(cor)

p3 = ggplot(cor_melt,
            aes(x = Var1, y = Var2, fill = value)) +
  geom_tile(colour = "black", size = 0.3) +
  theme_bw() +
  theme(axis.title.x = element_blank(),
        axis.title.y = element_blank())

3.1 GSEA

The GSEA palette (continuous) is inspired by the heatmaps generated by GSEA GenePattern.

p3_gsea     = p3 + scale_fill_gsea()
p3_gsea_inv = p3 + scale_fill_gsea(reverse = TRUE)
grid.arrange(p3_gsea, p3_gsea_inv, ncol = 2)
image.png

3.2 Material Design

The <emph style="font-style: oblique;">Material Design</emph> color palettes are from the material design color guidelines.

We generate a random matrix first:

library("reshape2")

set.seed(42)
k = 9
x = diag(k)
x[upper.tri(x)] = runif(sum(1:(k - 1)), 0, 1)
x_melt = melt(x)

p4 = ggplot(x_melt, aes(x = Var1, y = Var2, fill = value)) +
  geom_tile(colour = "black", size = 0.3) +
  scale_x_continuous(expand = c(0, 0)) +
  scale_y_continuous(expand = c(0, 0)) +
  theme_bw() + theme(
    legend.position = "none", plot.background = element_blank(),
    axis.line = element_blank(), axis.ticks = element_blank(),
    axis.text.x = element_blank(), axis.text.y = element_blank(),
    axis.title.x = element_blank(), axis.title.y = element_blank(),
    panel.background = element_blank(), panel.border = element_blank(),
    panel.grid.major = element_blank(), panel.grid.minor = element_blank())

Plot the matrix with the 19 material design color palettes:

grid.arrange(
  p4 + scale_fill_material("red"),         p4 + scale_fill_material("pink"),
  p4 + scale_fill_material("purple"),      p4 + scale_fill_material("deep-purple"),
  p4 + scale_fill_material("indigo"),      p4 + scale_fill_material("blue"),
  p4 + scale_fill_material("light-blue"),  p4 + scale_fill_material("cyan"),
  p4 + scale_fill_material("teal"),        p4 + scale_fill_material("green"),
  p4 + scale_fill_material("light-green"), p4 + scale_fill_material("lime"),
  p4 + scale_fill_material("yellow"),      p4 + scale_fill_material("amber"),
  p4 + scale_fill_material("orange"),      p4 + scale_fill_material("deep-orange"),
  p4 + scale_fill_material("brown"),       p4 + scale_fill_material("grey"),
  p4 + scale_fill_material("blue-grey"),
  ncol = 6)
image.png

From the figure above, we can see that even though an identical matrix was visualized by all plots, some palettes are more preferrable than the others because our eyes are more sensitive to the changes of their saturation levels.

4 Non-ggplot2 Graphics

To apply the color palettes in ggsci to other graphics systems (such as base graphics and lattice graphics), simply use the palette generator functions in the table above. For example:
对于非ggplot2的图形系统,ggsci还提供把调色板的颜色单独提取成颜色代码。

# 从NPC调色板中提取9个颜色
mypal = pal_npg("nrc", alpha = 0.7)(9)
mypal
#系统给出的NPC调色板中的9个颜色为:
## [1] "#E64B35B2" "#4DBBD5B2" "#00A087B2" "#3C5488B2" "#F39B7FB2" "#8491B4B2"
## [7] "#91D1C2B2" "#DC0000B2" "#7E6148B2"
#具体的颜色展示为:
library("scales")
show_col(mypal)
image.png

You will be able to use the generated hex color codes for such graphics systems accordingly. The transparent level of the entire palette is easily adjustable via the argument "alpha" in every generator or scale function.

5 Discussion

Please note some of the palettes might not be the best choice for certain purposes, such as color-blind safe, photocopy safe, or print friendly. If you do have such considerations, you might want to check out color palettes like ColorBrewer and viridis.

The color palettes in this package are solely created for research purposes. The authors are not responsible for the usage of such palettes.

最后编辑于
©著作权归作者所有,转载或内容合作请联系作者
  • 序言:七十年代末,一起剥皮案震惊了整个滨河市,随后出现的几起案子,更是在滨河造成了极大的恐慌,老刑警刘岩,带你破解...
    沈念sama阅读 204,293评论 6 478
  • 序言:滨河连续发生了三起死亡事件,死亡现场离奇诡异,居然都是意外死亡,警方通过查阅死者的电脑和手机,发现死者居然都...
    沈念sama阅读 85,604评论 2 381
  • 文/潘晓璐 我一进店门,熙熙楼的掌柜王于贵愁眉苦脸地迎上来,“玉大人,你说我怎么就摊上这事。” “怎么了?”我有些...
    开封第一讲书人阅读 150,958评论 0 337
  • 文/不坏的土叔 我叫张陵,是天一观的道长。 经常有香客问我,道长,这世上最难降的妖魔是什么? 我笑而不...
    开封第一讲书人阅读 54,729评论 1 277
  • 正文 为了忘掉前任,我火速办了婚礼,结果婚礼上,老公的妹妹穿的比我还像新娘。我一直安慰自己,他们只是感情好,可当我...
    茶点故事阅读 63,719评论 5 366
  • 文/花漫 我一把揭开白布。 她就那样静静地躺着,像睡着了一般。 火红的嫁衣衬着肌肤如雪。 梳的纹丝不乱的头发上,一...
    开封第一讲书人阅读 48,630评论 1 281
  • 那天,我揣着相机与录音,去河边找鬼。 笑死,一个胖子当着我的面吹牛,可吹牛的内容都是我干的。 我是一名探鬼主播,决...
    沈念sama阅读 38,000评论 3 397
  • 文/苍兰香墨 我猛地睁开眼,长吁一口气:“原来是场噩梦啊……” “哼!你这毒妇竟也来了?” 一声冷哼从身侧响起,我...
    开封第一讲书人阅读 36,665评论 0 258
  • 序言:老挝万荣一对情侣失踪,失踪者是张志新(化名)和其女友刘颖,没想到半个月后,有当地人在树林里发现了一具尸体,经...
    沈念sama阅读 40,909评论 1 299
  • 正文 独居荒郊野岭守林人离奇死亡,尸身上长有42处带血的脓包…… 初始之章·张勋 以下内容为张勋视角 年9月15日...
    茶点故事阅读 35,646评论 2 321
  • 正文 我和宋清朗相恋三年,在试婚纱的时候发现自己被绿了。 大学时的朋友给我发了我未婚夫和他白月光在一起吃饭的照片。...
    茶点故事阅读 37,726评论 1 330
  • 序言:一个原本活蹦乱跳的男人离奇死亡,死状恐怖,灵堂内的尸体忽然破棺而出,到底是诈尸还是另有隐情,我是刑警宁泽,带...
    沈念sama阅读 33,400评论 4 321
  • 正文 年R本政府宣布,位于F岛的核电站,受9级特大地震影响,放射性物质发生泄漏。R本人自食恶果不足惜,却给世界环境...
    茶点故事阅读 38,986评论 3 307
  • 文/蒙蒙 一、第九天 我趴在偏房一处隐蔽的房顶上张望。 院中可真热闹,春花似锦、人声如沸。这庄子的主人今日做“春日...
    开封第一讲书人阅读 29,959评论 0 19
  • 文/苍兰香墨 我抬头看了看天上的太阳。三九已至,却和暖如春,着一层夹袄步出监牢的瞬间,已是汗流浃背。 一阵脚步声响...
    开封第一讲书人阅读 31,197评论 1 260
  • 我被黑心中介骗来泰国打工, 没想到刚下飞机就差点儿被人妖公主榨干…… 1. 我叫王不留,地道东北人。 一个月前我还...
    沈念sama阅读 44,996评论 2 349
  • 正文 我出身青楼,却偏偏与公主长得像,于是被迫代替她去往敌国和亲。 传闻我的和亲对象是个残疾皇子,可洞房花烛夜当晚...
    茶点故事阅读 42,481评论 2 342