看到 PMID:39024031 文章的代码中,有颜色设置的语句:
pal <- paletteer_d("ggsci::category20_d3")[c(1,2,3,4,5,6,7,8,9,10,11,12,13,14,15,16,17,18)]
DimPlot(MM,reduction = 'umap',group.by = "sample",label = F,pt.size = 0.1,cols = pal)+
labs(title = 'Samples',size= 60)+
theme_bw(base_size = 14) +
theme(panel.grid = element_blank(),
axis.ticks = element_blank(),
aspect.ratio = 1,
strip.background = element_rect(colour = NA,fill = NA),
axis.text = element_blank(),
plot.title = element_text(hjust = 0.5))
DimPlot(MM,reduction = 'umap',group.by = "sample",split.by = "sample",label = F,pt.size = 0.1,cols = pal,ncol = 5)&
labs(title = 'Samples',size= 60)&
theme_bw(base_size = 14) &
theme(panel.grid = element_blank(),
axis.ticks = element_blank(),
aspect.ratio = 1,
strip.background = element_rect(colour = NA,fill = NA),
axis.text = element_blank(),
plot.title = element_text(hjust = 0.5))
pal <- paletteer_d("ggsci::nrc_npg")[c(1,2,3,4,5,6,7,8,9,10)]
pal <- paletteer_d("ggsci::category20c_d3")[c(1,2,3,4,5,6,7,8,9,10,11,12,13,14,15,16,17,18,19,20)]
pal <- paletteer_d("ggsci::default_igv")[c(1,2,3,4,5,6,7,8,9,10,11,12,13,14,15,16,17,18,19,20,21,22,23,24,25,26,27,28,29,30,
31,32,33,34,35,36,37,38,39,40,41,42,43,44,45,46,47,48,49,50,51)]
pal <- paletteer_d("ggsci::category20c_d3")[c(1,2,3,4,5,6,7,8,9,10,11,12,13,14,15,16,17,18,19,20)]
为什么不写成 1:20的形式呢?
搜索发现,这是paletteer包的函数。
据R包作者介绍,这个包收集了52个R包的2100种调色板,R用户可以通过paletteer包来直接调用其他R包的调色板。
- https://r-charts.com/color-palettes/ 查看这些调色板。
快速查看 颜色效果:
scales::show_col( paletteer_d("ggsci::nrc_npg")[1:8] )
# #E64B35FF #4DBBD5FF #00A087FF #3C5488FF #F39B7FFF #8491B4FF #91D1C2FF #DC0000FF
scales::show_col( paletteer_d("ggsci::default_igv")[1:51] )
# #5050FFFF #CE3D32FF #749B58FF #F0E685FF #466983FF #BA6338FF #5DB1DDFF #802268FF #6BD76BFF #D595A7FF #924822FF #837B8DFF #C75127FF #D58F5CFF #7A65A5FF #E4AF69FF #3B1B53FF #CDDEB7FF #612A79FF #AE1F63FF #E7C76FFF #5A655EFF #CC9900FF #99CC00FF #A9A9A9FF #CC9900FF #99CC00FF #33CC00FF #00CC33FF #00CC99FF #0099CCFF #0A47FFFF #4775FFFF #FFC20AFF #FFD147FF #990033FF #991A00FF #996600FF #809900FF #339900FF #00991AFF #009966FF #008099FF #003399FF #1A0099FF #660099FF #990080FF #D60047FF #FF1463FF #00D68FFF #14FFB1FF
1. 安装
install.packages("paletteer")
library(paletteer)
2. 直接获取颜色
paletteer包也将各种配色方案分成3类:CONTINUOUS、DISCRETE与DYNAMIC
(1) 连续型颜色(300多个)
> palettes_c_names|>dim()
[1] 319 3
> palettes_c_names|> head(n=2)
# A tibble: 2 × 3
package palette type
<chr> <chr> <chr>
1 ggthemes Blue-Green Sequential sequential
2 ggthemes Blue Light sequential
调用函数:paletteer_c(),在括号里加入包的名称与颜色集即可,注意需要设置过渡颜色数量 n
paletteer_c("ggthemes::Blue-Green Sequential", 30)
# 需要安装ggthemes包
(2) 离散型颜色(2k多个)
> palettes_d_names |> dim()
[1] 2415 5
> palettes_d_names |> head(n=3)
# A tibble: 3 × 5
package palette length type novelty
<chr> <chr> <int> <chr> <lgl>
1 awtools a_palette 8 sequential TRUE
2 awtools ppalette 8 qualitative TRUE
3 awtools bpalette 16 qualitative TRUE
调用函数:paletteer_d(),在括号里加入包的名称与颜色集即可。不添加颜色数量 n 默认输出组内全部颜色编码。
例1:
barplot( rep(1, 8), col=paletteer_d("awtools::ppalette")[1:8], border=NA, space=0.2, axes=F)
例2:
> palettes_d_names[grep("sci",palettes_d_names$package), ]
# A tibble: 49 × 5
package palette length type novelty
<chr> <chr> <int> <chr> <lgl>
1 ggsci nrc_npg 10 qualitative TRUE
2 ggsci default_aaas 10 qualitative TRUE
3 ggsci default_nejm 8 qualitative TRUE
4 ggsci lanonc_lancet 9 qualitative TRUE
5 ggsci default_jama 7 qualitative TRUE
> paletteer_d("ggsci::nrc_npg")
(3) 动态颜色集(25个)
> palettes_dynamic_names |> dim()
[1] 25 4
> palettes_dynamic_names |> head(n=3)
package palette length type
1 cartography blue.pal 20 sequential
2 cartography orange.pal 20 sequential
3 cartography red.pal 20 sequential
调用函数:paletteer_dynamic(),除了在括号里加入包的名称与颜色集,也必须设置输出颜色数量 n,否则报错
paletteer_dynamic("cartography::blue.pal", 20)
3. 在ggplot2中使用 scale_ 系列函数
与ggsci包类似,paletteer包不仅可以使用paletteer_c(),paletteer_d(),paletteer_dynamic()函数提取具体颜色编码;也可以使用比例尺(scales)函数直接取代ggplot2原有scales函数。
#scale_color_paletteer_d()
#scale_fill_paletteer_d()
#scale_color_paletteer_c()
#scale_fill_paletteer_c()
示例:
p1=ggplot(data = iris, aes(x = Species,y = Sepal.Length)) +
geom_violin(aes(fill = Species),alpha = 0.75, color="white") +
theme_classic()+
scale_fill_paletteer_d("ggsci::nrc_npg") +
#scale_fill_manual(values = as.character(paletteer_d("ggsci::nrc_npg")[1:8])) +
ggtitle("scale_fill_paletteer_d('ggsci::nrc_npg')")
p2=ggplot(data = iris, aes(x = Species,y = Sepal.Length)) +
geom_violin(aes(fill = Species),alpha = 0.75, color="white") +
theme_classic()+
#scale_fill_paletteer_d("ggsci::nrc_npg") +
scale_fill_manual(values = as.character(paletteer_d("ggsci::nrc_npg")[1:8])) +
ggtitle('as.character(paletteer_d("ggsci::nrc_npg")[1:8])')
p1 + p2
例2:
ggplot(data = iris, aes(x = Species,y = Sepal.Length)) +
geom_violin(aes(fill = Species),alpha = 0.75, color="white") +
theme_classic()+
scale_fill_manual(values = as.character(paletteer_d("awtools::ppalette")[1:8]))