前言
在绘图时,如何搭配颜色,是一个很让人头疼的问题。今天要介绍的包 ggsci
能大大减轻我们的负担。
ggsci
提供了很多高质量的颜色搭配,其灵感来自于各种 SCI
杂志的配色,其提供的配色函数也是以对应的杂志或数据库名称来命名的
所有颜色标度函数的形式为:
scale_color/colour_palname()
scale_fill_palname()
其中 palname
表示对应颜色板的名称,此外,还有对应的画板颜色生成函数
pal_palname()
离散型配色
安装导入
install.packages("ggsci")
library(ggsci)
我们使用下面的两张图,分别为其设置不同的颜色标度,来展示不同标度函数的效果
library(tidyverse)
library(cowplot)
p1 <- filter(diamonds, carat >= 2.2) %>%
ggplot(aes(table, price, colour = cut)) +
geom_point(alpha = 0.7) +
geom_smooth(method = "loess", alpha = 0.05, size = 1, span = 1) +
theme_bw()
p2 <- filter(diamonds, carat > 2.2 & depth > 55 & depth < 70) %>%
ggplot(aes(x = depth, fill = cut)) +
geom_histogram(colour = "black", binwidth = 1, position = "dodge") +
theme_bw()
plot_grid(p1, p2, ncol = 2)
1. NPG
该配色对应于 Nature Publishing Group
杂志中的配色,使用方式为
p1_npg <- p1 + scale_color_npg()
p2_npg <- p2 + scale_fill_npg()
plot_grid(p1_npg, p2_npg, ncol = 2)
该画板生成函数为 pal_npg
,最多可生成 10
种颜色
> library("scales")
> pal_npg("nrc")(10)
[1] "#E64B35FF" "#4DBBD5FF" "#00A087FF" "#3C5488FF" "#F39B7FFF" "#8491B4FF"
[7] "#91D1C2FF" "#DC0000FF" "#7E6148FF" "#B09C85FF"
> show_col(pal_npg("nrc")(10))
添加透明度
show_col(pal_npg("nrc", alpha = 0.6)(9))
2. AAAS
该配色对应于 American Association for the Advancement of Science
杂志,例如
p1_aaas <- p1 + scale_color_aaas()
p2_aaas <- p2 + scale_fill_aaas()
plot_grid(p1_aaas, p2_aaas, ncol = 2)
最多支持 10
种颜色
show_col(pal_aaas("default", alpha = 0.6)(10))
3. NEJM
该配色对应于 New England Journal of Medicine
杂志
p1_nejm <- p1 + scale_color_nejm()
p2_nejm <- p2 + scale_fill_nejm()
plot_grid(p1_nejm, p2_nejm, ncol = 2)
最多包含 8
种颜色
show_col(pal_nejm("default", alpha = 0.6)(8))
4. Lancet
柳叶刀杂志
p1_lancet <- p1 + scale_color_lancet()
p2_lancet <- p2 + scale_fill_lancet()
plot_grid(p1_lancet, p2_lancet, ncol = 2)
最多包含 9
种颜色
show_col(pal_lancet(palette = c("lanonc"), alpha = 0.6)(9))
5. JAMA
对应于 The Journal of the American Medical Association
杂志
p1_jama <- p1 + scale_color_jama()
p2_jama <- p2 + scale_fill_jama()
plot_grid(p1_jama, p2_jama, ncol = 2)
最多包含 7
种颜色
show_col(pal_jama("default", alpha = 0.6)(7))
6. JCO
Journal of Clinical Oncology
杂志
p1_jco <- p1 + scale_color_jco()
p2_jco <- p2 + scale_fill_jco()
plot_grid(p1_jco, p2_jco, ncol = 2)
最多包含 10
种颜色
show_col(pal_jco("default", alpha = 0.6)(9))
7. UCSCGB
UCSC
数据库
p1_ucscgb <- p1 + scale_color_ucscgb()
p2_ucscgb <- p2 + scale_fill_ucscgb()
plot_grid(p1_ucscgb, p2_ucscgb, ncol = 2)
最多包含 26
种颜色
show_col(pal_ucscgb("default", alpha = 0.6)(26))
8. D3
使用 JavaScript
绘图库 D3.js
的配色
p1_d3 <- p1 + scale_color_d3()
p2_d3 <- p2 + scale_fill_d3()
plot_grid(p1_d3, p2_d3, ncol = 2)
支持 4
种调色板类型:
-
"category10"
包含10
种颜色 -
"category20"
包含20
种颜色 -
"category20b"
包含20
种颜色 -
"category20c"
包含20
种颜色
例如
show_col(pal_d3("category20")(20))
show_col(pal_d3("category10")(10))
9. LocusZoom
LocusZoom
配色
p1_locuszoom <- p1 + scale_color_locuszoom()
p2_locuszoom <- p2 + scale_fill_locuszoom()
plot_grid(p1_locuszoom, p2_locuszoom, ncol = 2)
最多支持 7
种颜色
show_col(pal_locuszoom("default", alpha = 0.6)(7))
10. IGV
IGV
中的配色
p1_igv_default <- p1 + scale_color_igv()
p2_igv_default <- p2 + scale_fill_igv()
plot_grid(p1_igv_default, p2_igv_default, ncol = 2)
支持 2 种调色板类型:
-
"default"
包含51
种颜色 -
"alternating"
包含2
种颜色
例如
show_col(pal_igv("default")(51))
show_col(pal_igv("alternating", alpha = 0.5)(2))
11. UChicago
基于 University of Chicago
的配色
p1_uchicago <- p1 + scale_color_uchicago()
p2_uchicago <- p2 + scale_fill_uchicago()
plot_grid(p1_uchicago, p2_uchicago, ncol = 2)
包含 3 种调色板:
-
"default"
包含9
种颜色 -
"light"
包含9
种颜色 -
"dark"
包含9
种颜色
例如
show_col(pal_uchicago("default", alpha = 0.6)(9))
show_col(pal_uchicago("light", alpha = 0.6)(9))
show_col(pal_uchicago("dark", alpha = 0.6)(9))
12. Star Trek
Star Trek
配色方案
p1_startrek <- p1 + scale_color_startrek()
p2_startrek <- p2 + scale_fill_startrek()
plot_grid(p1_startrek, p2_startrek, ncol = 2)
最多支持 7
种颜色
show_col(pal_startrek("uniform", alpha = 0.6)(7))
13. Tron Legacy
Tron Legacy
配色
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()
plot_grid(p1_tron, p2_tron, ncol = 2)
最多支持 7
种颜色
show_col(pal_tron("legacy", alpha = 0.6)(7))
14. Futurama
Futurama
配色
p1_futurama <- p1 + scale_color_futurama()
p2_futurama <- p2 + scale_fill_futurama()
plot_grid(p1_futurama, p2_futurama, ncol = 2)
最多支持 12
种颜色
show_col(pal_futurama("planetexpress", alpha = 0.6)(12))
15. Rick and Morty
Rick and Morty
配色
p1_rickandmorty <- p1 + scale_color_rickandmorty()
p2_rickandmorty <- p2 + scale_fill_rickandmorty()
plot_grid(p1_rickandmorty, p2_rickandmorty, ncol = 2)
最多支持 12
种颜色
show_col(pal_rickandmorty("schwifty", alpha = 0.6)(12))
16. The Simpsons
The Simpsons
配色
p1_simpsons <- p1 + scale_color_simpsons()
p2_simpsons <- p2 + scale_fill_simpsons()
plot_grid(p1_simpsons, p2_simpsons, ncol = 2)
最多支持 16
种颜色
show_col(pal_simpsons("springfield", alpha = 0.6)(16))
连续型配色
对于连续型配色方案,我们使用相关系数矩阵来进行说明。例如
mat <- as.data.frame(round(cor(mtcars), 2))
mat$var1 <- rownames(mat)
p3 <- gather(mat, key = "var2", value = "corr", -var1) %>%
ggplot(aes(x = var1, y = var2, fill = corr)) +
geom_tile(colour = "black", size = 0.3) +
theme_bw() +
theme(
axis.title.x = element_blank(),
axis.title.y = element_blank()
)
p3
1. GSEA
GSEA
配色
p3_gsea <- p3 + scale_fill_gsea()
p3_gsea_inv <- p3 + scale_fill_gsea(reverse = TRUE)
plot_grid(p3_gsea, p3_gsea_inv, ncol = 2)
颜色生成函数为
pal_gsea(palette = c("default"), n = 12, alpha = 1, reverse = FALSE)
例如
show_col(pal_gsea("default", n = 30, alpha = 0.6, reverse = TRUE)(16))
2. Material Design
Material Design
支持 19
种颜色配色
p4 <- ggplot(data, aes(x = var1, y = var2, fill = corr)) +
geom_tile(colour = "black", size = 0.3) +
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_grid(
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
)
颜色生成函数为
pal_material(
palette = c("red", "pink", "purple",
"deep-purple", "indigo",
"blue", "light-blue",
"cyan", "teal", "green",
"light-green", "lime",
"yellow", "amber", "orange",
"deep-orange", "brown",
"grey", "blue-grey"),
n = 10,
alpha = 1,
reverse = FALSE
)
例如
show_col(pal_material("indigo")(9))