对于我这条使用R语言来绘图的生信而言,最令我绝望的便是关于配色的问题,很多时候自己选的颜色看上去会特别生硬,就算使用colorRampPalette产生渐变色也感觉不够理想。无意间,我看到了“ggsci”这个包,它完全就是为了SCI配色而设计的,下面我将挑选一些重要的函数来讲解,详细的内容可以去看官网的说明。
这些是帮助页面中的函数以及简短的介绍:
pal_aaas AAAS Journal Color Palettes
pal_d3 D3.js Color Palettes
pal_futurama The Futurama Color Palettes
pal_gsea The GSEA GenePattern Color Palettes
pal_igv Integrative Genomics Viewer (IGV) Color Palettes
pal_jama Journal of the American Medical Association Color Palettes
pal_jco Journal of Clinical Oncology Color Palettes
pal_lancet Lancet Journal Color Palettes
pal_locuszoom LocusZoom Color Palette
pal_material Material Design Color Palettes
pal_nejm NEJM Color Palettes
pal_npg NPG Journal Color Palettes
pal_rickandmorty Rick and Morty Color Palettes
pal_simpsons The Simpsons Color Palettes
pal_startrek Star Trek Color Palettes
pal_tron Tron Legacy Color Palettes
pal_uchicago The University of Chicago Color Palettes
pal_ucscgb UCSC Genome Browser Color Palette
rgb_gsea The GSEA GenePattern Color Palettes
rgb_material Material Design Color Palettes
scale_color_aaas AAAS Journal Color Scales
scale_color_d3 D3.js Color Scales
scale_color_futurama The Futurama Color Scales
scale_color_gsea The GSEA GenePattern Color Scales
scale_color_igv Integrative Genomics Viewer (IGV) Color Scales
scale_color_jama Journal of the American Medical Association Color Scales
scale_color_jco Journal of Clinical Oncology Color Scales
scale_color_lancet Lancet Journal Color Scales
scale_color_locuszoom LocusZoom Color Scales
scale_color_material Material Design Color Palettes
scale_color_nejm NEJM Color Scales
scale_color_npg NPG Journal Color Scales
scale_color_rickandmorty Rick and Morty Color Scales
scale_color_simpsons The Simpsons Color Scales
scale_color_startrek Star Trek Color Scales
scale_color_tron Tron Legacy Color Scales
scale_color_uchicago The University of Chicago Color Scales
scale_color_ucscgb UCSC Genome Browser Color Scales
scale_colour_aaas AAAS Journal Color Scales
scale_colour_d3 D3.js Color Scales
scale_colour_futurama The Futurama Color Scales
scale_colour_gsea The GSEA GenePattern Color Scales
scale_colour_igv Integrative Genomics Viewer (IGV) Color Scales
scale_colour_jama Journal of the American Medical Association Color Scales
scale_colour_jco Journal of Clinical Oncology Color Scales
scale_colour_lancet Lancet Journal Color Scales
scale_colour_locuszoom LocusZoom Color Scales
scale_colour_material Material Design Color Palettes
scale_colour_nejm NEJM Color Scales
scale_colour_npg NPG Journal Color Scales
scale_colour_rickandmorty Rick and Morty Color Scales
scale_colour_simpsons The Simpsons Color Scales
scale_colour_startrek Star Trek Color Scales
scale_colour_tron Tron Legacy Color Scales
scale_colour_uchicago The University of Chicago Color Scales
scale_colour_ucscgb UCSC Genome Browser Color Scales
scale_fill_aaas AAAS Journal Color Scales
scale_fill_d3 D3.js Color Scales
scale_fill_futurama The Futurama Color Scales
scale_fill_gsea The GSEA GenePattern Color Scales
scale_fill_igv Integrative Genomics Viewer (IGV) Color Scales
scale_fill_jama Journal of the American Medical Association Color Scales
scale_fill_jco Journal of Clinical Oncology Color Scales
scale_fill_lancet Lancet Journal Color Scales
scale_fill_locuszoom LocusZoom Color Scales
scale_fill_material Material Design Color Palettes
scale_fill_nejm NEJM Color Scales
scale_fill_npg NPG Journal Color Scales
scale_fill_rickandmorty Rick and Morty Color Scales
scale_fill_simpsons The Simpsons Color Scales
scale_fill_startrek Star Trek Color Scales
scale_fill_tron Tron Legacy Color Scales
scale_fill_uchicago The University of Chicago Color Scales
scale_fill_ucscgb UCSC Genome Browser Color Scales
根据说明书我们可以简单的将它们分成三类:pal,rgb,scale,前两个可以生成不同的颜色组,scale可以直接用与ggplot中。
pal的调用都是一个模板,其中只有两个参数:
palette的default默认参数(只用7个基础色),alpha代表透明度,取值在(0,1)间,越大越透明.
在pal后面加上(6)代表只产生6个颜色。
pal_aaas(palette = c("default"), alpha = 1)(6)
上面是pal_aaas的一个例子,若想查看具体是哪一些颜色可以使用“scales”包来实现:
library("scales")
library("ggsci")
show_col(pal_aaas("default")(7)) #图一
show_col(pal_aaas("default", alpha = 0.6)(7)) #图二
show_col(pal_aaas("default", alpha = 0.2)(4)) #图三
图一至图三:
以上是pal颜色的介绍,它的配色方案采用的是很多论文中的技巧,例如Integrative Genomics Viewer (IGV)的配色,Lancet Journal (柳叶刀)的配色,Journal of Clinical Oncology的配色等等,可以按照需求利用这些配色方案来完成绘图任务。
在ggsci包中,只有rgb_gsea和rgb_material这两个基于RGB色彩模式的函数【红、绿、蓝】,但是它们的用法还是有一些差别:
rgb_gsea(palette = c("default"), n = 12, alpha = 1, reverse = FALSE)
rgb_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)
参数详解:
n:产生多少个独立的颜色
alpha:透明度
reverse:逻辑值,决定颜色的排序是否反序
palette:在rgb_material中有19种配色方案
实例:
library("scales")
show_col(pal_material("indigo")(10))
show_col(pal_material("indigo", n = 20, alpha = 0.6, reverse = TRUE)(13))
个人感觉scale函数是整个“ggsci”包中最简单和常用的包,只需要在ggplot后面+上看中的scale颜色方案,即可完成配色,下面是一些实例的使用,分别是基于柳叶刀的配色方案(scale_fill_lancet()),基于GSEA(scale_fill_gsea())和基于Integrative Genomics Viewer (IGV)的配色方案( scale_fill_igv())
library("ggplot2")
data("diamonds")
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() + scale_fill_lancet()
library("ggplot2")
library("reshape2")
data("mtcars")
cor = cor(mtcars)
cor_melt = melt(cor)
ggplot(cor_melt,
aes(x = Var1, y = Var2, fill = value)) +
geom_tile(colour = "black", size = 0.3) +
theme_bw() + scale_fill_gsea()
library("ggplot2")
data("diamonds")
p = 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()
p + scale_fill_igv()
以上便是ggsci包的简介,希望对每一个学习生信的人都有所帮助,我这里也会不断把学习中遇到的问题和经验发上来,希望志同道合的朋友能和我多多讨论,一起学习。
ggsci的官网地址:https://nanx.me/ggsci/articles/ggsci.html#discrete-color-palettes