ggplot2配色神器:ggsci

作者: Hayley笔记 | 来源:发表于2021-07-08 23:28 被阅读0次

    1. ggsci介绍

    ggsci提供了一系列配置好的颜色给ggplot2绘制出的图片调色,包括一些知名杂志期刊或者软件甚至是知名科幻电影、动画等的经典配色风格。将这些配色风格直接应用于我们绘制出的图,方便又好看。

    # 下载
    # download ggsci package
    install.packages("ggsci")
    
    #install.packages("devtools")
    devtools::install_github("nanxstats/ggsci")
    

    对所有的调色板,使用方法:p+scale_color_xx()p+scale_fill_xx()
    p是绘制好的ggplot图,xx指调色板的名字
    所有调色板的名字:npg, aaas, nejm, lancet, jama, jco, ucscgb, d3, locuszoom, igv, cosmic, uchicago, startrek, tron, futurama, rickandmorty, simpsons, gsea, material

    2. ggsci使用

    2.1 离散型调色板,应用于离散型数值

    使用diamond数据集做演示,先使用diamond数据集绘制两张图

    library("ggsci")
    library("ggplot2")
    library("gridExtra")
    library("patchwork")
    
    data("diamonds")
    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()
    p1|p2
    
    • NPG(将NPG杂志的配色风格应用到刚刚绘制的两张图)
    p1_npg <- p1 + scale_color_npg()
    p2_npg <- p2 + scale_fill_npg()
    grid.arrange(p1_npg, p2_npg, ncol = 2)
    

    颜色提取

    show_col(pal_npg("nrc", alpha = 0.6)(10))
    ##上面palette=c('nrc),所以取颜色时pal_npg()第一个参数就是‘nrc’。如果是‘Default’就填‘Default’,其它的函数都是一样的取法。
    
    • AAAS(使用AAAS杂志的配色风格)
    p1_aaas <- p1 + scale_color_aaas()
    p2_aaas <- p2 + scale_fill_aaas()
    grid.arrange(p1_aaas, p2_aaas, ncol = 2)
    
    • NEJM(新英格兰配色风格)
    p1_nejm <- p1 + scale_color_nejm()
    p2_nejm <- p2 + scale_fill_nejm()
    grid.arrange(p1_nejm, p2_nejm, ncol = 2)
    
    • Lancet
    p1_lancet <- p1 + scale_color_lancet()
    p2_lancet <- p2 + scale_fill_lancet()
    grid.arrange(p1_lancet, p2_lancet, ncol = 2)
    
    • JAMA
    p1_jama <- p1 + scale_color_jama()
    p2_jama <- p2 + scale_fill_jama()
    grid.arrange(p1_jama, p2_jama, ncol = 2)
    
    • JCO
    p1_jco <- p1 + scale_color_jco()
    p2_jco <- p2 + scale_fill_jco()
    grid.arrange(p1_jco, p2_jco, ncol = 2)
    
    • UCSCGB
    p1_ucscgb <- p1 + scale_color_ucscgb()
    p2_ucscgb <- p2 + scale_fill_ucscgb()
    grid.arrange(p1_ucscgb, p2_ucscgb, ncol = 2)
    
    • D3
    p1_d3 <- p1 + scale_color_d3()
    p2_d3 <- p2 + scale_fill_d3()
    grid.arrange(p1_d3, p2_d3, ncol = 2)
    
    • LocusZoom
    p1_locuszoom <- p1 + scale_color_locuszoom()
    p2_locuszoom <- p2 + scale_fill_locuszoom()
    grid.arrange(p1_locuszoom, p2_locuszoom, ncol = 2)
    
    • 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)
    
    • UChicago
    p1_uchicago <- p1 + scale_color_uchicago()
    p2_uchicago <- p2 + scale_fill_uchicago()
    grid.arrange(p1_uchicago, p2_uchicago, ncol = 2)
    
    • Star Trek
    p1_startrek <- p1 + scale_color_startrek()
    p2_startrek <- p2 + scale_fill_startrek()
    grid.arrange(p1_startrek, p2_startrek, ncol = 2)
    
    • Tron Legend
    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)
    
    • Futurama
    p1_futurama <- p1 + scale_color_futurama()
    p2_futurama <- p2 + scale_fill_futurama()
    grid.arrange(p1_futurama, p2_futurama, ncol = 2)
    
    • Rick and Morty(我非常喜欢的动漫)
    p1_rickandmorty <- p1 + scale_color_rickandmorty()
    p2_rickandmorty <- p2 + scale_fill_rickandmorty()
    grid.arrange(p1_rickandmorty, p2_rickandmorty, ncol = 2)
    
    • The Simpsons
    p1_simpsons <- p1 + scale_color_simpsons()
    p2_simpsons <- p2 + scale_fill_simpsons()
    grid.arrange(p1_simpsons, p2_simpsons, ncol = 2)
    
    2.2 连续型调色板

    使用mtcars数据集做演示

    library("reshape2")
    
    data("mtcars")
    cor <- cor(unname(cbind(mtcars, mtcars, mtcars, mtcars)))
    cor_melt <- melt(cor)
    
    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()
      )
    
    • GSEA
      将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)
    

    参考:https://nanx.me/ggsci/articles/ggsci.html

    相关文章

      网友评论

        本文标题:ggplot2配色神器:ggsci

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