R內(nèi)置基本配色
R自帶的grDevices包含有以下調(diào)色板:
# colors()函數(shù),共包含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])
# 傳入獲取顏色的個(gè)數(shù)兜叨,就能獲得相應(yīng)個(gè)數(shù)的顏色列表。
#彩虹色
cols <- rainbow(10)
head(cols)
## [1] "#FF0000FF" "#FF9900FF" "#CCFF00FF" "#33FF00FF" "#00FF66FF" "#00FFFFFF"
plot(1:10, col=cols, pch=1:10, cex=2)
#紅漸變黃
cols <- heat.colors(10)
head(cols)
## [1] "#FF0000FF" "#FF2400FF" "#FF4900FF" "#FF6D00FF" "#FF9200FF" "#FFB600FF"
barplot(1:10, col = cols, names.arg = cols)
#藍(lán)綠變黃
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)
#綠黃變白
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")
#藍(lán)漸變粉
cols <- cm.colors(10)
head(cols)
## [1] "#00A600FF" "#2DB600FF" "#63C600FF" "#A0D600FF" "#E6E600FF" "#E8C32EFF"
barplot(1:10, col = cols, names.arg = cols)
#梯度灰色
# gray()函數(shù)的參數(shù)需在[0,1]
gray(0.5)
## [1] "#808080"
R中色彩相關(guān)的函數(shù)
palette() #調(diào)色板
## [1] "black" "red" "green3" "blue" "cyan" "magenta" "yellow"
## [8] "gray"
rgb(red = 1, green = 0.5, blue = 0.5) #生成顏色編碼
## [1] "#FF8080"
col2rgb(col = "red") #將顏色轉(zhuǎn)為rgb色值
## [,1]
## red 255
## green 0
## blue 0
rgb2hsv(col2rgb("blue")) #將rgb色值轉(zhuǎn)換為hsv色值
## [,1]
## h 0.6666667
## s 1.0000000
## v 1.0000000
alpha(colour = "red", alpha = 0.5) #調(diào)節(jié)顏色透明度
## [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()
# 查看diverging離散型顏色畫板瘫寝,每個(gè)顏色畫板中包含11種顏色
display.brewer.all(type="div")
# 查看sequential連續(xù)型顏色畫板初嘹,每個(gè)顏色畫板中包含9種顏色
display.brewer.all(type="seq")
# 查看qualitative極端型顏色畫板
display.brewer.all(type="qual")
# 查看某一顏色畫板中的7種顏色
display.brewer.pal(7,"Accent")
使用colorRampPalette()函數(shù)可以擴(kuò)展顏色
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)
使用colorspace包配色
# 安裝并加載colorspace包
#install.packages("colorspace")
library(colorspace)
# choosing HCL-based color palettes
# 查看所有的顏色畫板
hcl_palettes(plot = TRUE)
# 選取某一顏色畫板中的4種顏色
q4 <- qualitative_hcl(4, palette = "Dark 3")
q4
## [1] "#E16A86" "#909800" "#00AD9A" "#9183E6"
demoplot(q4, "bar")
# 選取某一顏色畫板中的6種顏色
s6 <- sequential_hcl(6, palette = "Purples 3")
s6
## [1] "#312271" "#6154AB" "#9187D4" "#BDB6ED" "#E2DFFD" "#F9F9F9"
demoplot(s6, "heatmap")
# 選取某一顏色畫板中的9種顏色
d9 <- diverging_hcl(9, "Tropic")
d9
## [1] "#009B9F" "#00B0B3" "#7DC5C7" "#BADADC" "#F1F1F1" "#E8CDDE" "#DEA9CC"
## [8] "#D385BB" "#C75DAA"
demoplot(d9, "scatter")
使用viridis包配色
# 安裝并加載viridis包
#install.packages("viridis")
library(viridis)
# 查看畫板顏色
library(scales)
show_col(viridis_pal(option = "D")(30))
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
viridis包提供了5種顏色畫板
# "option" (A: magma; B: inferno; C: plasma; D: viridis; E: cividis)
p + scale_fill_viridis(option = "A")
p + scale_fill_viridis(option = "B")
p + scale_fill_viridis(option = "C")
p + scale_fill_viridis(option = "D")
p + scale_fill_viridis(option = "E")
使用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)
ggsci包提供了以下顏色畫板
查看離散型顏色畫板
# 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)
# 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)
# 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)
# 04.Lancet(Lancet)期刊顏色
p1_lancet = p1 + scale_color_lancet()
p2_lancet = p2 + scale_fill_lancet()
grid.arrange(p1_lancet, p2_lancet, ncol = 2)
# 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)
# 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)
# 07.UCSCGB顏色畫板
p1_ucscgb = p1 + scale_color_ucscgb()
p2_ucscgb = p2 + scale_fill_ucscgb()
grid.arrange(p1_ucscgb, p2_ucscgb, ncol = 2)
# 08.D3顏色畫板
p1_d3 = p1 + scale_color_d3()
p2_d3 = p2 + scale_fill_d3()
grid.arrange(p1_d3, p2_d3, ncol = 2)
# 09.LocusZoom顏色畫板
p1_locuszoom = p1 + scale_color_locuszoom()
p2_locuszoom = p2 + scale_fill_locuszoom()
grid.arrange(p1_locuszoom, p2_locuszoom, ncol = 2)
# 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)
# 11.UChicago顏色畫板
p1_uchicago = p1 + scale_color_uchicago()
p2_uchicago = p2 + scale_fill_uchicago()
grid.arrange(p1_uchicago, p2_uchicago, ncol = 2)
# 12.Star Trek顏色畫板
p1_startrek = p1 + scale_color_startrek()
p2_startrek = p2 + scale_fill_startrek()
grid.arrange(p1_startrek, p2_startrek, ncol = 2)
# 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)
# 14.Futurama顏色畫板
p1_futurama = p1 + scale_color_futurama()
p2_futurama = p2 + scale_fill_futurama()
grid.arrange(p1_futurama, p2_futurama, ncol = 2)
# 15.Rick and Morty顏色畫板
p1_rickandmorty = p1 + scale_color_rickandmorty()
p2_rickandmorty = p2 + scale_fill_rickandmorty()
grid.arrange(p1_rickandmorty, p2_rickandmorty, ncol = 2)
# 16.The Simpsons顏色畫板
p1_simpsons = p1 + scale_color_simpsons()
p2_simpsons = p2 + scale_fill_simpsons()
grid.arrange(p1_simpsons, p2_simpsons, ncol = 2)
查看連續(xù)型顏色畫板
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
# 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)
# 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
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)
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