R語言常用配色大全


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])
image.png
# 傳入獲取顏色的個(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)
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
#藍(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)
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
#藍(lán)漸變粉 
cols <- cm.colors(10) 
head(cols)
## [1] "#00A600FF" "#2DB600FF" "#63C600FF" "#A0D600FF" "#E6E600FF" "#E8C32EFF"

barplot(1:10, col = cols, names.arg = cols)
image.png
#梯度灰色
# 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()
image.png
# 查看diverging離散型顏色畫板瘫寝,每個(gè)顏色畫板中包含11種顏色
display.brewer.all(type="div")
image.png
# 查看sequential連續(xù)型顏色畫板初嘹,每個(gè)顏色畫板中包含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()函數(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)
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

查看連續(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
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
最后編輯于
?著作權(quán)歸作者所有,轉(zhuǎn)載或內(nèi)容合作請聯(lián)系作者
  • 序言:七十年代末及汉,一起剝皮案震驚了整個(gè)濱河市,隨后出現(xiàn)的幾起案子屯烦,更是在濱河造成了極大的恐慌坷随,老刑警劉巖,帶你破解...
    沈念sama閱讀 206,126評論 6 481
  • 序言:濱河連續(xù)發(fā)生了三起死亡事件驻龟,死亡現(xiàn)場離奇詭異温眉,居然都是意外死亡,警方通過查閱死者的電腦和手機(jī)翁狐,發(fā)現(xiàn)死者居然都...
    沈念sama閱讀 88,254評論 2 382
  • 文/潘曉璐 我一進(jìn)店門类溢,熙熙樓的掌柜王于貴愁眉苦臉地迎上來,“玉大人露懒,你說我怎么就攤上這事豌骏。” “怎么了隐锭?”我有些...
    開封第一講書人閱讀 152,445評論 0 341
  • 文/不壞的土叔 我叫張陵,是天一觀的道長计贰。 經(jīng)常有香客問我钦睡,道長,這世上最難降的妖魔是什么躁倒? 我笑而不...
    開封第一講書人閱讀 55,185評論 1 278
  • 正文 為了忘掉前任荞怒,我火速辦了婚禮,結(jié)果婚禮上秧秉,老公的妹妹穿的比我還像新娘褐桌。我一直安慰自己,他們只是感情好象迎,可當(dāng)我...
    茶點(diǎn)故事閱讀 64,178評論 5 371
  • 文/花漫 我一把揭開白布荧嵌。 她就那樣靜靜地躺著,像睡著了一般砾淌。 火紅的嫁衣襯著肌膚如雪啦撮。 梳的紋絲不亂的頭發(fā)上,一...
    開封第一講書人閱讀 48,970評論 1 284
  • 那天汪厨,我揣著相機(jī)與錄音赃春,去河邊找鬼。 笑死劫乱,一個(gè)胖子當(dāng)著我的面吹牛织中,可吹牛的內(nèi)容都是我干的锥涕。 我是一名探鬼主播,決...
    沈念sama閱讀 38,276評論 3 399
  • 文/蒼蘭香墨 我猛地睜開眼狭吼,長吁一口氣:“原來是場噩夢啊……” “哼层坠!你這毒婦竟也來了?” 一聲冷哼從身側(cè)響起搏嗡,我...
    開封第一講書人閱讀 36,927評論 0 259
  • 序言:老撾萬榮一對情侶失蹤窿春,失蹤者是張志新(化名)和其女友劉穎,沒想到半個(gè)月后采盒,有當(dāng)?shù)厝嗽跇淞掷锇l(fā)現(xiàn)了一具尸體旧乞,經(jīng)...
    沈念sama閱讀 43,400評論 1 300
  • 正文 獨(dú)居荒郊野嶺守林人離奇死亡,尸身上長有42處帶血的膿包…… 初始之章·張勛 以下內(nèi)容為張勛視角 年9月15日...
    茶點(diǎn)故事閱讀 35,883評論 2 323
  • 正文 我和宋清朗相戀三年磅氨,在試婚紗的時(shí)候發(fā)現(xiàn)自己被綠了尺栖。 大學(xué)時(shí)的朋友給我發(fā)了我未婚夫和他白月光在一起吃飯的照片。...
    茶點(diǎn)故事閱讀 37,997評論 1 333
  • 序言:一個(gè)原本活蹦亂跳的男人離奇死亡烦租,死狀恐怖延赌,靈堂內(nèi)的尸體忽然破棺而出,到底是詐尸還是另有隱情叉橱,我是刑警寧澤挫以,帶...
    沈念sama閱讀 33,646評論 4 322
  • 正文 年R本政府宣布,位于F島的核電站窃祝,受9級特大地震影響掐松,放射性物質(zhì)發(fā)生泄漏。R本人自食惡果不足惜粪小,卻給世界環(huán)境...
    茶點(diǎn)故事閱讀 39,213評論 3 307
  • 文/蒙蒙 一大磺、第九天 我趴在偏房一處隱蔽的房頂上張望。 院中可真熱鬧探膊,春花似錦杠愧、人聲如沸。這莊子的主人今日做“春日...
    開封第一講書人閱讀 30,204評論 0 19
  • 文/蒼蘭香墨 我抬頭看了看天上的太陽。三九已至猾担,卻和暖如春袭灯,著一層夾襖步出監(jiān)牢的瞬間,已是汗流浹背绑嘹。 一陣腳步聲響...
    開封第一講書人閱讀 31,423評論 1 260
  • 我被黑心中介騙來泰國打工稽荧, 沒想到剛下飛機(jī)就差點(diǎn)兒被人妖公主榨干…… 1. 我叫王不留,地道東北人。 一個(gè)月前我還...
    沈念sama閱讀 45,423評論 2 352
  • 正文 我出身青樓姨丈,卻偏偏與公主長得像畅卓,于是被迫代替她去往敵國和親。 傳聞我的和親對象是個(gè)殘疾皇子蟋恬,可洞房花燭夜當(dāng)晚...
    茶點(diǎn)故事閱讀 42,722評論 2 345

推薦閱讀更多精彩內(nèi)容