R中大师级的调色板

长期以来画图最折磨人的莫过于颜色的选择,一张图往往在颜色选择上都要耗费我不少的时间,正好自己最近有大型配色需求索性将R中的颜色代码一并整理分享给大家,希望对各位小伙伴有所帮助

viridis 连续型调色板

这个配色我常用于热图的绘制

install.packages("viridis")

viridis使用案例

scale_fill_viridis(option="magma")中写入调色板名称即可

library(viridis)

unemp <- read.csv("http://datasets.flowingdata.com/unemployment09.csv",
                  header = FALSE, stringsAsFactors = FALSE)
names(unemp) <- c("id", "state_fips", "county_fips", "name", "year",
                  "?", "?", "?", "rate")
unemp$county <- tolower(gsub(" County, [A-Z]{2}", "", unemp$name))
unemp$county <- gsub("^(.*) parish, ..$","\\1", unemp$county)
unemp$state <- gsub("^.*([A-Z]{2}).*$", "\\1", unemp$name)

county_df <- map_data("county", projection = "albers", parameters = c(39, 45))
names(county_df) <- c("long", "lat", "group", "order", "state_name", "county")
county_df$state <- state.abb[match(county_df$state_name, tolower(state.name))]
county_df$state_name <- NULL

state_df <- map_data("state", projection = "albers", parameters = c(39, 45))

choropleth <- merge(county_df, unemp, by = c("state", "county"))
choropleth <- choropleth[order(choropleth$order), ]

ggplot(choropleth, aes(long, lat, group = group)) +
  geom_polygon(aes(fill = rate), colour = alpha("white", 1 / 2), size = 0.2) +
  geom_polygon(data = state_df, colour = "white", fill = NA) +
  coord_fixed() +
  theme_minimal() +
  ggtitle("US unemployment rate by county") +
  theme(axis.line = element_blank(), axis.text = element_blank(),
        axis.ticks = element_blank(), axis.title = element_blank()) +
  scale_fill_viridis(option="magma")
image

以下介绍的2种调色板多用于离散型数据,有了这麽多的颜色代码再也不用担心颜色不够用了,哈哈

wesanderson

devtools::install_github("karthik/wesanderson")
# CRAN version
install.packages("wesanderson")
image

wesanderson使用案例

只需要更换色条wes_palette("Zissou1")内名称即可

library(wesanderson)
library("ggplot2")
ggplot(mtcars, aes(factor(cyl), fill=factor(vs))) +  geom_bar() +
  scale_fill_manual(values = wes_palette("Zissou1"))
image

wesanderson 16进制颜色代码清单

wes_palettes <- list(
  BottleRocket1 = c("#A42820", "#5F5647", "#9B110E", "#3F5151", "#4E2A1E", "#550307", "#0C1707"),
  BottleRocket2 = c("#FAD510", "#CB2314", "#273046", "#354823", "#1E1E1E"),
  Rushmore1 = c("#E1BD6D", "#EABE94", "#0B775E", "#35274A" ,"#F2300F"),
  Rushmore = c("#E1BD6D", "#EABE94", "#0B775E", "#35274A" ,"#F2300F"),
  Royal1 = c("#899DA4", "#C93312", "#FAEFD1", "#DC863B"),
  Royal2 = c("#9A8822", "#F5CDB4", "#F8AFA8", "#FDDDA0", "#74A089"),
  Zissou1 = c("#3B9AB2", "#78B7C5", "#EBCC2A", "#E1AF00", "#F21A00"),
  Darjeeling1 = c("#FF0000", "#00A08A", "#F2AD00", "#F98400", "#5BBCD6"),
  Darjeeling2 = c("#ECCBAE", "#046C9A", "#D69C4E", "#ABDDDE", "#000000"),
  Chevalier1 = c("#446455", "#FDD262", "#D3DDDC", "#C7B19C"),
  FantasticFox1 = c("#DD8D29", "#E2D200", "#46ACC8", "#E58601", "#B40F20"),
  Moonrise1 = c("#F3DF6C", "#CEAB07", "#D5D5D3", "#24281A"),
  Moonrise2 = c("#798E87", "#C27D38", "#CCC591", "#29211F"),
  Moonrise3 = c("#85D4E3", "#F4B5BD", "#9C964A", "#CDC08C", "#FAD77B"),
  Cavalcanti1 = c("#D8B70A", "#02401B", "#A2A475", "#81A88D", "#972D15"),
  GrandBudapest1 = c("#F1BB7B", "#FD6467", "#5B1A18", "#D67236"),
  GrandBudapest2 = c("#E6A0C4", "#C6CDF7", "#D8A499", "#7294D4"),
  IsleofDogs1 = c("#9986A5", "#79402E", "#CCBA72", "#0F0D0E", "#D9D0D3", "#8D8680"),
  IsleofDogs2 = c("#EAD3BF", "#AA9486", "#B6854D", "#39312F", "#1C1718"))

ggsci

devtools::install_github("nanxstats/ggsci")

library(ggsci)
library(tidyverse)
library(scales)

ggsci用法如下图所示

image
pal_aaas("default")(10) 
show_col(pal_aaas("default")(10))
image
pal_npg("nrc")(10)
show_col(pal_npg("nrc")(10))
image
pal_nejm("default")(8)
show_col(pal_nejm("default")(8))
image
pal_lancet("lanonc")(9)
show_col(pal_lancet("lanonc")(9))
image
pal_jama("default")(7)
show_col(pal_jama("default")(7))
image
pal_jco("default")(10)
show_col(pal_jco("default")(10))
image
pal_d3("category10")(10)
show_col(pal_d3("category10")(10))
image
pal_locuszoom("default")(7)
show_col(pal_locuszoom("default")(7))
image
pal_uchicago("default")(9)
show_col(pal_uchicago("default")(9))
image
pal_startrek("uniform")(7)
show_col(pal_startrek("uniform")(7))
image
pal_tron("legacy")(7)
show_col(pal_tron("legacy")(7))
image
pal_futurama("planetexpress")(12)
show_col(pal_futurama("planetexpress")(12))
image
pal_simpsons("springfield")(16)
show_col(pal_simpsons("springfield")(16))
image

ggsci 16进制颜色代码清单

sci_palettes <- list(aaas=c("#3B4992FF","#EE0000FF","#008B45FF","#631879FF",
"#008280FF","#BB0021FF","#5F559BFF","#A20056FF","#808180FF","#1B1919FF"),
npg=c("#E64B35FF","#4DBBD5FF","#00A087FF","#3C5488FF","#F39B7FFF","#8491B4FF",
"#91D1C2FF","#DC0000FF","#7E6148FF","#B09C85FF"),
nejm=c("#BC3C29FF","#0072B5FF","#E18727FF","#20854EFF","#7876B1FF",
"#6F99ADFF","#FFDC91FF","#EE4C97FF"),
lancet=c("#00468BFF","#ED0000FF","#42B540FF","#0099B4FF","#925E9FFF",
"#FDAF91FF","#AD002AFF","#ADB6B6FF","#1B1919FF"),
jama=c("#374E55FF","#DF8F44FF","#00A1D5FF","#B24745FF","#79AF97FF",
"#6A6599FF","#80796BFF"),
jco=c("#0073C2FF","#EFC000FF","#868686FF","#CD534CFF","#7AA6DCFF","#003C67FF",
"#8F7700FF","#3B3B3BFF","#A73030FF","#4A6990FF"),
d3=c("#1F77B4FF","#FF7F0EFF","#2CA02CFF","#D62728FF","#9467BDFF","#8C564BFF",
"#E377C2FF","#7F7F7FFF","#BCBD22FF","#17BECFFF"),
locuszoom=c("#D43F3AFF","#EEA236FF","#5CB85CFF","#46B8DAFF","#357EBDFF",
"#9632B8FF","#B8B8B8FF"),
uchicago=c("#800000FF","#767676FF","#FFA319FF","#8A9045FF","#155F83FF",
"#C16622FF","#8F3931FF","#58593FFF","#350E20FF"),
startek=c("#CC0C00FF","#5C88DAFF","#84BD00FF","#FFCD00FF","#7C878EFF",
"#00B5E2FF","#00AF66FF"),
tron=c("#FF410DFF","#6EE2FFFF","#F7C530FF","#95CC5EFF","#D0DFE6FF",
"#F79D1EFF","#748AA6FF"),
futurama=c("#FF6F00FF","#C71000FF","#008EA0FF","#8A4198FF","#5A9599FF",
"#FF6348FF","#84D7E1FF","#FF95A8FF","#3D3B25FF","#ADE2D0FF","#1A5354FF","#3F4041FF"),
simpsons=c("#FED439FF","#709AE1FF","#8A9197FF","#D2AF81FF","#FD7446FF",
"#D5E4A2FF","#197EC0FF","#F05C3BFF","#46732EFF",
"#71D0F5FF","#370335FF","#075149FF","#C80813FF","#91331FFF","#1A9993FF","#FD8CC1FF")
©著作权归作者所有,转载或内容合作请联系作者
  • 序言:七十年代末,一起剥皮案震惊了整个滨河市,随后出现的几起案子,更是在滨河造成了极大的恐慌,老刑警刘岩,带你破解...
    沈念sama阅读 203,772评论 6 477
  • 序言:滨河连续发生了三起死亡事件,死亡现场离奇诡异,居然都是意外死亡,警方通过查阅死者的电脑和手机,发现死者居然都...
    沈念sama阅读 85,458评论 2 381
  • 文/潘晓璐 我一进店门,熙熙楼的掌柜王于贵愁眉苦脸地迎上来,“玉大人,你说我怎么就摊上这事。” “怎么了?”我有些...
    开封第一讲书人阅读 150,610评论 0 337
  • 文/不坏的土叔 我叫张陵,是天一观的道长。 经常有香客问我,道长,这世上最难降的妖魔是什么? 我笑而不...
    开封第一讲书人阅读 54,640评论 1 276
  • 正文 为了忘掉前任,我火速办了婚礼,结果婚礼上,老公的妹妹穿的比我还像新娘。我一直安慰自己,他们只是感情好,可当我...
    茶点故事阅读 63,657评论 5 365
  • 文/花漫 我一把揭开白布。 她就那样静静地躺着,像睡着了一般。 火红的嫁衣衬着肌肤如雪。 梳的纹丝不乱的头发上,一...
    开封第一讲书人阅读 48,590评论 1 281
  • 那天,我揣着相机与录音,去河边找鬼。 笑死,一个胖子当着我的面吹牛,可吹牛的内容都是我干的。 我是一名探鬼主播,决...
    沈念sama阅读 37,962评论 3 395
  • 文/苍兰香墨 我猛地睁开眼,长吁一口气:“原来是场噩梦啊……” “哼!你这毒妇竟也来了?” 一声冷哼从身侧响起,我...
    开封第一讲书人阅读 36,631评论 0 258
  • 序言:老挝万荣一对情侣失踪,失踪者是张志新(化名)和其女友刘颖,没想到半个月后,有当地人在树林里发现了一具尸体,经...
    沈念sama阅读 40,870评论 1 297
  • 正文 独居荒郊野岭守林人离奇死亡,尸身上长有42处带血的脓包…… 初始之章·张勋 以下内容为张勋视角 年9月15日...
    茶点故事阅读 35,611评论 2 321
  • 正文 我和宋清朗相恋三年,在试婚纱的时候发现自己被绿了。 大学时的朋友给我发了我未婚夫和他白月光在一起吃饭的照片。...
    茶点故事阅读 37,704评论 1 329
  • 序言:一个原本活蹦乱跳的男人离奇死亡,死状恐怖,灵堂内的尸体忽然破棺而出,到底是诈尸还是另有隐情,我是刑警宁泽,带...
    沈念sama阅读 33,386评论 4 319
  • 正文 年R本政府宣布,位于F岛的核电站,受9级特大地震影响,放射性物质发生泄漏。R本人自食恶果不足惜,却给世界环境...
    茶点故事阅读 38,969评论 3 307
  • 文/蒙蒙 一、第九天 我趴在偏房一处隐蔽的房顶上张望。 院中可真热闹,春花似锦、人声如沸。这庄子的主人今日做“春日...
    开封第一讲书人阅读 29,944评论 0 19
  • 文/苍兰香墨 我抬头看了看天上的太阳。三九已至,却和暖如春,着一层夹袄步出监牢的瞬间,已是汗流浃背。 一阵脚步声响...
    开封第一讲书人阅读 31,179评论 1 260
  • 我被黑心中介骗来泰国打工, 没想到刚下飞机就差点儿被人妖公主榨干…… 1. 我叫王不留,地道东北人。 一个月前我还...
    沈念sama阅读 44,742评论 2 349
  • 正文 我出身青楼,却偏偏与公主长得像,于是被迫代替她去往敌国和亲。 传闻我的和亲对象是个残疾皇子,可洞房花烛夜当晚...
    茶点故事阅读 42,440评论 2 342

推荐阅读更多精彩内容