R中大师级的调色板

作者: R语言数据分析指南 | 来源:发表于2021-04-19 00:21 被阅读0次

    长期以来画图最折磨人的莫过于颜色的选择,一张图往往在颜色选择上都要耗费我不少的时间,正好自己最近有大型配色需求索性将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")
    

    相关文章

      网友评论

        本文标题:R中大师级的调色板

        本文链接:https://www.haomeiwen.com/subject/hoqelltx.html