R语言常用配色大全
2021-01-05 本文已影响0人
Davey1220
R内置基本配色
R自带的grDevices包含有以下调色板:
# colors()函数,共包含657种颜色
cols <- colors()
head(cols)
## [1] "white" "aliceblue" "antiquewhite" "antiquewhite1"
## [5] "antiquewhite2" "antiquewhite3"
length(cols)
## [1] 657
# 查看前15种颜色
pie(1:15,col=cols[1:15],labels = cols[1:15])
image.png
# 传入获取颜色的个数,就能获得相应个数的颜色列表。
#彩虹色
cols <- rainbow(10)
head(cols)
## [1] "#FF0000FF" "#FF9900FF" "#CCFF00FF" "#33FF00FF" "#00FF66FF" "#00FFFFFF"
plot(1:10, col=cols, pch=1:10, cex=2)
image.png
#红渐变黄
cols <- heat.colors(10)
head(cols)
## [1] "#FF0000FF" "#FF2400FF" "#FF4900FF" "#FF6D00FF" "#FF9200FF" "#FFB600FF"
barplot(1:10, col = cols, names.arg = cols)
image.png
#蓝绿变黄
cols <- topo.colors(10)
head(cols)
## [1] "#4C00FFFF" "#0019FFFF" "#0080FFFF" "#00E5FFFF" "#00FF4DFF" "#4DFF00FF"
dotchart(1:10, color = cols, pt.cex = 2, pch = 1:10, labels = cols)
image.png
#绿黄变白
cols <- terrain.colors(10)
head(cols)
## [1] "#00A600FF" "#2DB600FF" "#63C600FF" "#A0D600FF" "#E6E600FF" "#E8C32EFF"
image(1:10,1,as.matrix(1:10),col=cols,
xlab="terrain.colors(10)",
ylab="",xaxt="n",yaxt="n",bty="n")
image.png
#蓝渐变粉
cols <- cm.colors(10)
head(cols)
## [1] "#00A600FF" "#2DB600FF" "#63C600FF" "#A0D600FF" "#E6E600FF" "#E8C32EFF"
barplot(1:10, col = cols, names.arg = cols)
image.png
#梯度灰色
# gray()函数的参数需在[0,1]
gray(0.5)
## [1] "#808080"
R中色彩相关的函数
palette() #调色板
## [1] "black" "red" "green3" "blue" "cyan" "magenta" "yellow"
## [8] "gray"
rgb(red = 1, green = 0.5, blue = 0.5) #生成颜色编码
## [1] "#FF8080"
col2rgb(col = "red") #将颜色转为rgb色值
## [,1]
## red 255
## green 0
## blue 0
rgb2hsv(col2rgb("blue")) #将rgb色值转换为hsv色值
## [,1]
## h 0.6666667
## s 1.0000000
## v 1.0000000
alpha(colour = "red", alpha = 0.5) #调节颜色透明度
## [1] "#FF000080"
使用RColorBrewer包配色
# 安装并加载RColorBrewer包
#install.packages("RColorBrewer")
library(RColorBrewer)
# 查看所有颜色画板的信息
brewer.pal.info
## maxcolors category colorblind
## BrBG 11 div TRUE
## PiYG 11 div TRUE
## PRGn 11 div TRUE
## PuOr 11 div TRUE
## RdBu 11 div TRUE
## RdGy 11 div FALSE
## RdYlBu 11 div TRUE
## RdYlGn 11 div FALSE
## Spectral 11 div FALSE
## Accent 8 qual FALSE
## Dark2 8 qual TRUE
## Paired 12 qual TRUE
## Pastel1 9 qual FALSE
## Pastel2 8 qual FALSE
## Set1 9 qual FALSE
## Set2 8 qual TRUE
## Set3 12 qual FALSE
## Blues 9 seq TRUE
## BuGn 9 seq TRUE
## BuPu 9 seq TRUE
## GnBu 9 seq TRUE
## Greens 9 seq TRUE
## Greys 9 seq TRUE
## Oranges 9 seq TRUE
## OrRd 9 seq TRUE
## PuBu 9 seq TRUE
## PuBuGn 9 seq TRUE
## PuRd 9 seq TRUE
## Purples 9 seq TRUE
## RdPu 9 seq TRUE
## Reds 9 seq TRUE
## YlGn 9 seq TRUE
## YlGnBu 9 seq TRUE
## YlOrBr 9 seq TRUE
## YlOrRd 9 seq TRUE
# 查看某一颜色画板的信息
brewer.pal.info["Blues",]
## maxcolors category colorblind
## Blues 9 seq TRUE
# 查看所有颜色画板
display.brewer.all()
image.png
# 查看diverging离散型颜色画板,每个颜色画板中包含11种颜色
display.brewer.all(type="div")
image.png
# 查看sequential连续型颜色画板,每个颜色画板中包含9种颜色
display.brewer.all(type="seq")
image.png
# 查看qualitative极端型颜色画板
display.brewer.all(type="qual")
image.png
# 查看某一颜色画板中的7种颜色
display.brewer.pal(7,"Accent")
image.png
使用colorRampPalette()函数可以扩展颜色
cols <- colorRampPalette(brewer.pal(5,"Blues"))(20)
cols
## [1] "#EFF3FF" "#E4EDF9" "#D9E7F4" "#CFE1EF" "#C4DBEA" "#B8D4E6" "#A7CCE2"
## [8] "#96C3DE" "#84BADB" "#73B2D7" "#64A9D3" "#58A0CE" "#4C96C8" "#408DC3"
## [15] "#3484BE" "#2A7AB7" "#216FB0" "#1965A9" "#105BA2" "#08519C"
pie(rep(1,20), col = cols, labels = cols)
image.png
使用colorspace包配色
# 安装并加载colorspace包
#install.packages("colorspace")
library(colorspace)
# choosing HCL-based color palettes
# 查看所有的颜色画板
hcl_palettes(plot = TRUE)
image.png
# 选取某一颜色画板中的4种颜色
q4 <- qualitative_hcl(4, palette = "Dark 3")
q4
## [1] "#E16A86" "#909800" "#00AD9A" "#9183E6"
demoplot(q4, "bar")
image.png
# 选取某一颜色画板中的6种颜色
s6 <- sequential_hcl(6, palette = "Purples 3")
s6
## [1] "#312271" "#6154AB" "#9187D4" "#BDB6ED" "#E2DFFD" "#F9F9F9"
demoplot(s6, "heatmap")
image.png
# 选取某一颜色画板中的9种颜色
d9 <- diverging_hcl(9, "Tropic")
d9
## [1] "#009B9F" "#00B0B3" "#7DC5C7" "#BADADC" "#F1F1F1" "#E8CDDE" "#DEA9CC"
## [8] "#D385BB" "#C75DAA"
demoplot(d9, "scatter")
image.png
使用viridis包配色
# 安装并加载viridis包
#install.packages("viridis")
library(viridis)
# 查看画板颜色
library(scales)
show_col(viridis_pal(option = "D")(30))
image.png
library(ggplot2)
p <- ggplot(data = data.frame(x = rnorm(10000), y = rnorm(10000)),
aes(x = x, y = y)) + geom_hex() + coord_fixed() + theme_bw()
p
image.png
viridis包提供了5种颜色画板
image.png# "option" (A: magma; B: inferno; C: plasma; D: viridis; E: cividis)
p + scale_fill_viridis(option = "A")
image.png
p + scale_fill_viridis(option = "B")
image.png
p + scale_fill_viridis(option = "C")
image.png
p + scale_fill_viridis(option = "D")
image.png
p + scale_fill_viridis(option = "E")
image.png
使用ggsci包配色
# 安装并加载ggsci包
#install.packages("ggsci")
library(ggsci)
library("ggplot2")
library("gridExtra")
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()
grid.arrange(p1, p2, ncol = 2)
image.png
ggsci包提供了以下颜色画板
image.png查看离散型颜色画板
# 01.NPG(Nature Publishing Group)期刊颜色
p1_npg = p1 + scale_color_npg()
p2_npg = p2 + scale_fill_npg()
grid.arrange(p1_npg, p2_npg, ncol = 2)
image.png
# 02.AAAS(American Association for the Advancement of Science)期刊颜色
p1_aaas = p1 + scale_color_aaas()
p2_aaas = p2 + scale_fill_aaas()
grid.arrange(p1_aaas, p2_aaas, ncol = 2)
image.png
# 03.NEJM(The New England Journal of Medicine)期刊颜色
p1_aaas = p1 + scale_color_aaas()
p2_aaas = p2 + scale_fill_aaas()
grid.arrange(p1_aaas, p2_aaas, ncol = 2)
image.png
# 04.Lancet(Lancet)期刊颜色
p1_lancet = p1 + scale_color_lancet()
p2_lancet = p2 + scale_fill_lancet()
grid.arrange(p1_lancet, p2_lancet, ncol = 2)
image.png
# 05.JAMA(The Journal of the American Medical Association)期刊颜色
p1_jama = p1 + scale_color_jama()
p2_jama = p2 + scale_fill_jama()
grid.arrange(p1_jama, p2_jama, ncol = 2)
image.png
# 06.JCO(Journal of Clinical Oncology)期刊颜色
p1_jco = p1 + scale_color_jco()
p2_jco = p2 + scale_fill_jco()
grid.arrange(p1_jco, p2_jco, ncol = 2)
image.png
# 07.UCSCGB颜色画板
p1_ucscgb = p1 + scale_color_ucscgb()
p2_ucscgb = p2 + scale_fill_ucscgb()
grid.arrange(p1_ucscgb, p2_ucscgb, ncol = 2)
image.png
# 08.D3颜色画板
p1_d3 = p1 + scale_color_d3()
p2_d3 = p2 + scale_fill_d3()
grid.arrange(p1_d3, p2_d3, ncol = 2)
image.png
# 09.LocusZoom颜色画板
p1_locuszoom = p1 + scale_color_locuszoom()
p2_locuszoom = p2 + scale_fill_locuszoom()
grid.arrange(p1_locuszoom, p2_locuszoom, ncol = 2)
image.png
# 10.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
# 11.UChicago颜色画板
p1_uchicago = p1 + scale_color_uchicago()
p2_uchicago = p2 + scale_fill_uchicago()
grid.arrange(p1_uchicago, p2_uchicago, ncol = 2)
image.png
# 12.Star Trek颜色画板
p1_startrek = p1 + scale_color_startrek()
p2_startrek = p2 + scale_fill_startrek()
grid.arrange(p1_startrek, p2_startrek, ncol = 2)
image.png
# 13.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)
image.png
# 14.Futurama颜色画板
p1_futurama = p1 + scale_color_futurama()
p2_futurama = p2 + scale_fill_futurama()
grid.arrange(p1_futurama, p2_futurama, ncol = 2)
image.png
# 15.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
# 16.The Simpsons颜色画板
p1_simpsons = p1 + scale_color_simpsons()
p2_simpsons = p2 + scale_fill_simpsons()
grid.arrange(p1_simpsons, p2_simpsons, ncol = 2)
image.png
查看连续型颜色画板
library("reshape2")
cor = cor(unname(cbind(mtcars, mtcars, mtcars, mtcars)))
cor_melt = melt(cor)
head(cor_melt)
## Var1 Var2 value
## 1 1 1 1.0000000
## 2 2 1 -0.8521620
## 3 3 1 -0.8475514
## 4 4 1 -0.7761684
## 5 5 1 0.6811719
## 6 6 1 -0.8676594
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())
p3
image.png
# 01.GSEA颜色画板
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
# 02.Material Design颜色画板
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)
head(x_melt)
## Var1 Var2 value
## 1 1 1 1
## 2 2 1 0
## 3 3 1 0
## 4 4 1 0
## 5 5 1 0
## 6 6 1 0
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())
p4
image.png
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
sessionInfo()
## R version 3.6.0 (2019-04-26)
## Platform: x86_64-w64-mingw32/x64 (64-bit)
## Running under: Windows 10 x64 (build 18363)
##
## Matrix products: default
##
## locale:
## [1] LC_COLLATE=Chinese (Simplified)_China.936
## [2] LC_CTYPE=Chinese (Simplified)_China.936
## [3] LC_MONETARY=Chinese (Simplified)_China.936
## [4] LC_NUMERIC=C
## [5] LC_TIME=Chinese (Simplified)_China.936
##
## attached base packages:
## [1] stats graphics grDevices utils datasets methods base
##
## other attached packages:
## [1] reshape2_1.4.3 gridExtra_2.3 ggsci_2.9
## [4] ggplot2_3.3.2 scales_1.0.0 viridis_0.5.1
## [7] viridisLite_0.3.0 colorspace_1.4-1 RColorBrewer_1.1-2
##
## loaded via a namespace (and not attached):
## [1] Rcpp_1.0.5 plyr_1.8.4 pillar_1.4.2 compiler_3.6.0
## [5] tools_3.6.0 digest_0.6.20 nlme_3.1-139 evaluate_0.14
## [9] tibble_2.1.3 lifecycle_0.2.0 gtable_0.3.0 lattice_0.20-38
## [13] mgcv_1.8-28 pkgconfig_2.0.2 rlang_0.4.7 Matrix_1.2-17
## [17] yaml_2.2.0 hexbin_1.27.3 xfun_0.8 withr_2.1.2
## [21] stringr_1.4.0 dplyr_1.0.2 knitr_1.23 generics_0.0.2
## [25] vctrs_0.3.2 grid_3.6.0 tidyselect_1.1.0 glue_1.4.2
## [29] R6_2.4.0 rmarkdown_1.13 purrr_0.3.2 magrittr_1.5
## [33] splines_3.6.0 htmltools_0.3.6 labeling_0.3 stringi_1.4.3
## [37] munsell_0.5.0 crayon_1.3.4