ggsci | 让你的配色Nature化

ggsci_cover.jpg

R科研绘图调色板—ggsci包

简介

ggsci

ggsci提供了一系列高质量的调色板,其灵感来自于期刊、电影等。ggsci中的调色板可用于ggplot2。基本用法:

  • scale_color_palname()
  • scale_fill_palname()
#安装并加载ggsci包
install.packages("ggsci")
library("ggsci")
library("ggplot2")
library("gridExtra")

离散型调色板

使用散点图和条形图来演示ggsci中的调色板。

#示例数据集
data("diamonds")#使用diamonds数据集
head(diamonds)
> head(diamonds)
# A tibble: 6 x 10
  carat cut       color clarity depth table price     x     y     z
                 
1 0.23  Ideal     E     SI2      61.5    55   326  3.95  3.98  2.43
2 0.21  Premium   E     SI1      59.8    61   326  3.89  3.84  2.31
3 0.23  Good      E     VS1      56.9    65   327  4.05  4.07  2.31
4 0.290 Premium   I     VS2      62.4    58   334  4.2   4.23  2.63
5 0.31  Good      J     SI2      63.3    58   335  4.34  4.35  2.75
6 0.24  Very Good J     VVS2     62.8    57   336  3.94  3.96  2.48
#原始配色
p1 <- ggplot(
  subset(diamonds, carat >= 2.2), #筛选数据
  aes(x = table, y = price, colour = cut) #确定xy
) +
  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) #确定x
) +
  geom_histogram(colour = "black", binwidth = 1, position = "dodge") + #画直方图
  theme_bw() #主题
grid.arrange(p1, p2, ncol = 2) #合并两图
origin

Nature配色

p1_npg <- p1 + scale_color_npg()
p2_npg <- p2 + scale_fill_npg()
grid.arrange(p1_npg, p2_npg, ncol = 2)
NPG

美国科学促进会(AAAS)

p1_aaas <- p1 + scale_color_aaas()
p2_aaas <- p2 + scale_fill_aaas()
grid.arrange(p1_aaas, p2_aaas, ncol = 2)
AAAS

新英格兰杂志(NEJM)

p1_lancet <- p1 + scale_color_lancet()
p2_lancet <- p2 + scale_fill_lancet()
grid.arrange(p1_lancet, p2_lancet, ncol = 2)
NEJM

柳叶刀(Lancet)

p1_lancet <- p1 + scale_color_lancet()
p2_lancet <- p2 + scale_fill_lancet()
grid.arrange(p1_lancet, p2_lancet, ncol = 2)
LANCET

美国医学会杂志(JAMA)

p1_jama <- p1 + scale_color_jama()
p2_jama <- p2 + scale_fill_jama()
grid.arrange(p1_jama, p2_jama, ncol = 2)
JAMA

临床肿瘤学杂志(JCO)

p1_jco <- p1 + scale_color_jco()
p2_jco <- p2 + scale_fill_jco()
grid.arrange(p1_jco, p2_jco, ncol = 2)
JCO

创:战纪(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()
grid.arrange(p1_tron, p2_tron, ncol = 2)
Tron Legacy

辛普森(The Simpsons)

p1_simpsons <- p1 + scale_color_simpsons()
p2_simpsons <- p2 + scale_fill_simpsons()
grid.arrange(p1_simpsons, p2_simpsons, ncol = 2)
The Simpsons

连续型调色板

用相关性热图来展示。

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()
  )
Continuous Color Palettes

GSEA

GSEA调色板的灵感来自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)
GSEA_COLOR

Material Design

The Material Design color palettes are from the material design color guidelines.

先生成一个随机矩阵

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()
  )

用19种Material Design调色板绘制矩阵:

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
)
Material Design

非ggplot图

如果不是··作的图,可以直接调用颜色参数。

mypal <- pal_npg("nrc", alpha = 0.7)(9)
mypal
library("scales")
show_col(mypal)
mypal

总结

下表总结了所有可用的调色板。

Name Scales Palette Types Palette Generator
NPG scale_color_npg() scale_fill_npg() "nrc" pal_npg()
AAAS scale_color_aaas() scale_fill_aaas() "default" pal_aaas()
NEJM scale_color_nejm() scale_fill_nejm() "default" pal_nejm()
Lancet scale_color_lancet() scale_fill_lancet() "lanonc" pal_lancet()
JAMA scale_color_jama() scale_fill_jama() "default" pal_jama()
JCO scale_color_jco() scale_fill_jco() "default" pal_jco()
UCSCGB scale_color_ucscgb() scale_fill_ucscgb() "default" pal_ucscgb()
D3 scale_color_d3() scale_fill_d3() "category10" "category20" "category20b" "category20c" pal_d3()
LocusZoom scale_color_locuszoom() scale_fill_locuszoom() "default" pal_locuszoom()
IGV scale_color_igv() scale_fill_igv() "default" "alternating" pal_igv()
COSMIC scale_color_cosmic() scale_fill_cosmic() "hallmarks_light" "hallmarks_dark" "signature_substitutions" pal_cosmic()
UChicago scale_color_uchicago() scale_fill_uchicago() "default" "light" "dark" pal_uchicago()
Star Trek scale_color_startrek() scale_fill_startrek() "uniform" pal_startrek()
Tron Legacy scale_color_tron() scale_fill_tron() "legacy" pal_tron()
Futurama scale_color_futurama() scale_fill_futurama() "planetexpress" pal_futurama()
Rick and Morty scale_color_rickandmorty() scale_fill_rickandmorty() "schwifty" pal_rickandmorty()
The Simpsons scale_color_simpsons() scale_fill_simpsons() "springfield" pal_simpsons()
GSEA scale_color_gsea() scale_fill_gsea() "default" pal_gsea()
Material Design scale_color_material() scale_fill_material() "red" "pink" "purple" "deep-purple" "indigo" "blue" "light-blue" "cyan" "teal" "green" "light-green" "lime" "yellow" "amber" "orange" "deep-orange" "brown" "grey" "blue-grey" pal_material()

参考

https://nanx.me/ggsci/articles/ggsci.html

代码领取:后台回复"ggsci"即可领取完整代码。


往期内容:

单细胞转录组|Seurat 4.0 使用指南

R绘图实战|GSEA富集分析图

你可能感兴趣的:(ggsci | 让你的配色Nature化)