0. 問(wèn)題導(dǎo)入
之前繪圖的時(shí)候,我經(jīng)常會(huì)去RGB顏色對(duì)照表上手動(dòng)去摘選顏色碼,然后手動(dòng)粘到ggplot-scale_fill/color_manual(values = c('我選的色帶'))。但是呢,大家都知道的,男生嘛,對(duì)于顏色這方面,是吧......大家都懂的(手動(dòng)汗顏,表示要向李佳琦好好學(xué)習(xí))。一般繪圖常態(tài)就是畫(huà)圖15秒,調(diào)色一小時(shí),簡(jiǎn)直是開(kāi)了無(wú)數(shù)窗口,最后還是強(qiáng)迫癥爆炸般地覺(jué)得下一個(gè)色帶一定是最好的。
BUT, HOWEVER
組會(huì)上把精心調(diào)好的圖展示出來(lái)的時(shí)候,還是有時(shí)候會(huì)覺(jué)得不太好看
于是乎就有了今天這篇帖子,旨在搜羅網(wǎng)上比較全的顏色貼,好好滴總結(jié)一波,希望可以幫到同樣是選擇困難癥的你。
繪圖所用軟件包附于文末, PS:多圖預(yù)警??!圖片加載可能需要畫(huà)20秒左右,內(nèi)容精彩,值得期待哈~
1. 示例數(shù)據(jù)
本次演示采用“全球sc-PDSI(干旱指數(shù))1901-2018年的月尺度數(shù)據(jù)” 中的2018年12月的數(shù)據(jù)進(jìn)行繪圖示例。同之前,為了大家下載方便,下附百度云下載鏈接(如果覺(jué)得慢,也可以去數(shù)據(jù)官網(wǎng)進(jìn)行下載):
數(shù)據(jù)下載鏈接
2. 數(shù)據(jù)導(dǎo)入與底圖繪制
input_data = 'L:\\JianShu\\2019-12-07\\data\\scpdsi_1901_2018.nc'
data = stack(input_data)
data = data[[1416]] #2018-12
df = as.data.frame(data,xy = T)
colnames(df) = c('long','lat','scpdsi')
na_index = which(is.na(df$scpdsi))
df = df[-na_index,]
df$DC = cut(df$scpdsi,
breaks = c(-Inf,-5,-4,-3,-2,-1,Inf))
df$DC = factor(df$DC,
labels = c('Exceptional Drought',
'Extreme Drought',
'Severe Drought',
"Moderate Drought",
'Abnormally Dry',
'No Drought'))
# fill bar
p = ggplot()+
geom_hline(aes(yintercept = 50),linetype = 'dashed',alpha = 0.5,lwd = 0.5,color = 'black')+
geom_hline(aes(yintercept = 0),linetype = 'dashed',alpha = 0.5,lwd = 0.5,color = 'black')+
geom_hline(aes(yintercept = -50),linetype = 'dashed',alpha = 0.5,lwd = 0.5,color = 'black')+
geom_vline(aes(xintercept = 0),linetype = 'dashed',alpha = 0.5,lwd = 0.5,color = 'black')+
geom_vline(aes(xintercept = -100),linetype = 'dashed',alpha = 0.5,lwd = 0.5,color = 'black')+
geom_vline(aes(xintercept = 100),linetype = 'dashed',alpha = 0.5,lwd = 0.5,color = 'black')+
geom_tile(data = df, aes(x = long,y = lat, fill = DC))+
theme(panel.background = element_rect(fill = 'transparent',color = 'black'),
axis.text = element_text(face='bold',colour='black',size=fontsize,hjust=.5),
axis.title = element_text(face='bold',colour='black',size=fontsize,hjust=.5),
legend.position=c('bottom'),
legend.direction = c('horizontal'))+
coord_fixed(1.3)+
guides(fill=guide_legend(nrow=3))+
xlab('Longitude')+
ylab('Latitude')
3. 增加Viridis 色帶
Viridis 色帶包由Simon Garnier研發(fā), 包含viridis, magma, plasma, inferno及默認(rèn)共5個(gè)色帶組(圖1-2),對(duì)應(yīng)scale_fill/color_viridis(option =" " )中的"A", "B", "C", "D","E"五個(gè)參數(shù)。

png('L:\\JianShu\\2019-12-07\\plot\\plot_viridis.png',
height = 15,
width = 26,
units = 'cm',
res = 1000)
# print(p_viridis)
p_viridis = grid.arrange(
p + scale_fill_viridis(option = 'A',discrete = T)+labs(x="Virdis A", y=NULL),
p + scale_fill_viridis(option = 'B',discrete = T)+labs(x="Virdis B", y=NULL),
p + scale_fill_viridis(option = 'C',discrete = T)+labs(x="Virdis C", y=NULL),
p + scale_fill_viridis(option = 'D',discrete = T)+labs(x="Virdis D", y=NULL),
p + scale_fill_viridis(option = 'E',discrete = T)+labs(x="Virdis E", y=NULL),
ncol=3, nrow=2
)
dev.off()

4. 增加 RColorBrewer 色帶
RColorBrewer這個(gè)應(yīng)用很廣泛了,附色帶圖譜及應(yīng)用示例(圖3-6)。

4.1 RColorBrewer 色帶組1
png('L:\\JianShu\\2019-12-07\\plot\\plot_rcolor_brewer.png',
height = 26,
width = 26,
units = 'cm',
res = 1000)
p_rcolor_brewer = grid.arrange(
p+scale_fill_brewer(palette = 'YlOrRd')+labs(x="ColorBand: YlOrRd", y=NULL),
p+scale_fill_brewer(palette = 'YlOrBr')+labs(x="ColorBand: YlOrBr", y=NULL),
p+scale_fill_brewer(palette = 'YlGnBu')+labs(x="ColorBand: YlGnBu", y=NULL),
p+scale_fill_brewer(palette = 'YlGn')+labs(x="ColorBand: YlGn", y=NULL),
p+scale_fill_brewer(palette = 'Reds')+labs(x="ColorBand: Reds", y=NULL),
p+scale_fill_brewer(palette = 'RdPu')+labs(x="ColorBand: RdPu", y=NULL),
p+scale_fill_brewer(palette = 'Purples')+labs(x="ColorBand: Purples", y=NULL),
p+scale_fill_brewer(palette = 'PuRd')+labs(x="ColorBand: PuRd", y=NULL),
p+scale_fill_brewer(palette = 'PuBuGn')+labs(x="ColorBand: PuBuGn", y=NULL),
ncol = 3
)
dev.off()

4.1 RColorBrewer 色帶組2
png('L:\\JianShu\\2019-12-07\\plot\\plot_rcolor_brewer2.png',
height = 26,
width = 26,
units = 'cm',
res = 1000)
p_rcolor_brewer = grid.arrange(
p+scale_fill_brewer(palette = 'PuBu')+labs(x="ColorBand: PuBu", y=NULL),
p+scale_fill_brewer(palette = 'OrRd')+labs(x="ColorBand: OrRd", y=NULL),
p+scale_fill_brewer(palette = 'Oranges')+labs(x="ColorBand: Oranges", y=NULL),
p+scale_fill_brewer(palette = 'Greys')+labs(x="ColorBand: Greys", y=NULL),
p+scale_fill_brewer(palette = 'Greens')+labs(x="ColorBand: Greens", y=NULL),
p+scale_fill_brewer(palette = 'GnBu')+labs(x="ColorBand: GnBu", y=NULL),
p+scale_fill_brewer(palette = 'BuPu')+labs(x="ColorBand: BuPu", y=NULL),
p+scale_fill_brewer(palette = 'BuGn')+labs(x="ColorBand: BuGn", y=NULL),
p+scale_fill_brewer(palette = 'Blues')+labs(x="ColorBand: Blues", y=NULL),
ncol = 3
)
dev.off()

4.1 RColorBrewer 色帶組3
png('L:\\JianShu\\2019-12-07\\plot\\plot_rcolor_brewer3.png',
height = 15,
width = 26,
units = 'cm',
res = 1000)
p_rcolor_brewer = grid.arrange(
p+scale_fill_brewer(palette = 'RdYlBu')+labs(x="ColorBand: RdYlBu", y=NULL),
p+scale_fill_brewer(palette = 'RdBu')+labs(x="ColorBand: RdBu", y=NULL),
p+scale_fill_brewer(palette = 'PuOr')+labs(x="ColorBand: PuOr", y=NULL),
p+scale_fill_brewer(palette = 'PRGn')+labs(x="ColorBand: PRGn", y=NULL),
p+scale_fill_brewer(palette = 'PiYG')+labs(x="ColorBand: PiYG", y=NULL),
p+scale_fill_brewer(palette = 'BrBG')+labs(x="ColorBand: BrBG", y=NULL),
ncol = 3
)
dev.off()

5. 增加 GGSCI 色帶(來(lái)自一些頂級(jí)期刊,如柳葉刀)
GGSCI這個(gè)色帶組收集了一些主流SCI期刊中比較受歡迎與經(jīng)典的色帶組(圖7),分別包括:
- scale_color/fill_npg(): 對(duì)應(yīng)Nature Publishing Group色帶
- scale_color/fill_aaas(): 對(duì)應(yīng)American Association for the Advancement of Science 色帶
- scale_color/fill_lancet: 對(duì)應(yīng)Lancet (柳葉刀)期刊搜集的色帶組
- scale_color/fill_jco: 對(duì)應(yīng)Journal of Clinical Oncology 色帶組
- scale_color/fill_tron: 對(duì)應(yīng)Tron Legacy 色帶組
png('L:\\JianShu\\2019-12-07\\plot\\plot_ggsci.png',
height = 15,
width = 26,
units = 'cm',
res = 1000)
p_rcolor_brewer = grid.arrange(
p+scale_fill_npg()+labs(x="ColorBand: NRC", y=NULL),
p+scale_fill_aaas()+labs(x = 'ColorBand: AAAS',y = NULL),
p+scale_fill_lancet()+labs(x = 'ColorBand: Lancet',y = NULL),
p+scale_fill_jco()+labs(x = 'ColorBand: JCO',y = NULL),
p+scale_fill_tron()+labs(x = 'ColorBand: TRON',y = NULL),
ncol = 3
)
dev.off()

6. 增加 Wes Anderson色帶(來(lái)自一些頂級(jí)期刊,如柳葉刀)
由于Wes Anderson 色帶組中大多顏色帶所含顏色數(shù)量為4-5個(gè)(圖8-9),因此在此,我對(duì) df 中的干旱分類組進(jìn)行壓縮,將Abnormal dry 與 No drought 進(jìn)行合并。

df$DC2 = cut(df$scpdsi,
breaks = c(-Inf,-5,-4,-3,-2,Inf))
df$DC2 = factor(df$DC2,
labels = c('Exceptional Drought',
'Extreme Drought',
'Severe Drought',
"Moderate Drought",
'AD and No Drought'))
p1 = ggplot()+
geom_hline(aes(yintercept = 50),linetype = 'dashed',alpha = 0.5,lwd = 0.5,color = 'black')+
geom_hline(aes(yintercept = 0),linetype = 'dashed',alpha = 0.5,lwd = 0.5,color = 'black')+
geom_hline(aes(yintercept = -50),linetype = 'dashed',alpha = 0.5,lwd = 0.5,color = 'black')+
geom_vline(aes(xintercept = 0),linetype = 'dashed',alpha = 0.5,lwd = 0.5,color = 'black')+
geom_vline(aes(xintercept = -100),linetype = 'dashed',alpha = 0.5,lwd = 0.5,color = 'black')+
geom_vline(aes(xintercept = 100),linetype = 'dashed',alpha = 0.5,lwd = 0.5,color = 'black')+
geom_tile(data = df, aes(x = long,y = lat, fill = DC2))+
theme(panel.background = element_rect(fill = 'transparent',color = 'black'),
axis.text = element_text(face='bold',colour='black',size=fontsize,hjust=.5),
axis.title = element_text(face='bold',colour='black',size=fontsize,hjust=.5),
legend.position=c('bottom'),
legend.direction = c('horizontal'))+
coord_fixed(1.3)+
guides(fill=guide_legend(nrow=3))+
xlab('Longitude')+
ylab('Latitude')
png('L:\\JianShu\\2019-12-07\\plot\\plot_web_anderson.png',
height = 26,
width = 26,
units = 'cm',
res = 1000)
p_rcolor_brewer = grid.arrange(
p1+scale_fill_manual(values = wes_palette('Rushmore1'))+labs(x="ColorBand: Rushmore1", y=NULL),
p1+scale_fill_manual(values = wes_palette('Zissou1'))+labs(x="ColorBand: Zissou1", y=NULL),
p1+scale_fill_manual(values = wes_palette('Darjeeling1'))+labs(x="ColorBand: Darjeeling1", y=NULL),
p1+scale_fill_manual(values = wes_palette('BottleRocket2'))+labs(x="ColorBand: BottleRocket2", y=NULL),
p1+scale_fill_manual(values = wes_palette('Royal2'))+labs(x="ColorBand: Royal2", y=NULL),
p1+scale_fill_manual(values = wes_palette('Darjeeling2'))+labs(x="ColorBand: Darjeeling2", y=NULL),
p1+scale_fill_manual(values = wes_palette('FantasticFox1'))+labs(x="ColorBand: FantasticFox1", y=NULL),
p1+scale_fill_manual(values = wes_palette('Moonrise3'))+labs(x="ColorBand: Moonrise3", y=NULL),
p1+scale_fill_manual(values = wes_palette('Cavalcanti1'))+labs(x="ColorBand: Cavalcanti1", y=NULL),
ncol = 3
)
dev.off()

7. 本文所用軟件包-木有的話,可以用install.packages('軟件包名')進(jìn)行安裝
library(viridis)
library(RColorBrewer)
library(ggsci)
library(wesanderson)
library(ggplot2)
library(gridExtra)
8. 致謝
大家如果覺(jué)得有用,還麻煩大家關(guān)注點(diǎn)贊,也可以擴(kuò)散到朋友圈,幫助到繪圖中同樣陷入顏色選擇困難癥的TA
大家如果在使用本文代碼的過(guò)程有遇到問(wèn)題的,可以留言評(píng)論,也可以私信我哈~~