参考:https://www.jianshu.com/p/b4abcb1c0f38

    直接使用ggplot 的内置数据集画图:

    1. library(ggsci)
    2. library(ggplot2)
    3. p1 <- ggplot(subset(diamonds, carat > 2.2), aes(x = color, y = price, fill = color)) + geom_boxplot(color = 'black') + theme_bw()
    4. p1

    01. R 科研绘图调色板--ggsci - 图1

    可以直接参见ggsci 的官方文档:

    Name Scales Palette Types Palette Generator
    NPG scale_color_npg()
    scale_fill_npg()
    "nrc" pal_npg()
    AAAS scale_color_aaas()
    scale_fill_aaas()
    "default" pal_aaas()
    NEJM scale_color_nejm()
    scale_fill_nejm()
    "default" pal_nejm()
    Lancet scale_color_lancet()
    scale_fill_lancet()
    "lanonc" pal_lancet()
    JAMA scale_color_jama()
    scale_fill_jama()
    "default" pal_jama()
    JCO scale_color_jco()
    scale_fill_jco()
    "default" pal_jco()
    UCSCGB scale_color_ucscgb()
    scale_fill_ucscgb()
    "default" pal_ucscgb()
    D3 scale_color_d3()
    scale_fill_d3()
    "category10"
    "category20"
    "category20b"
    "category20c"
    pal_d3()
    LocusZoom scale_color_locuszoom()
    scale_fill_locuszoom()
    "default" pal_locuszoom()
    IGV scale_color_igv()
    scale_fill_igv()
    "default"
    "alternating"
    pal_igv()
    UChicago scale_color_uchicago()
    scale_fill_uchicago()
    "default"
    "light"
    "dark"
    pal_uchicago()
    Star Trek scale_color_startrek()
    scale_fill_startrek()
    "uniform" pal_startrek()
    Tron Legacy scale_color_tron()
    scale_fill_tron()
    "legacy" pal_tron()
    Futurama scale_color_futurama()
    scale_fill_futurama()
    "planetexpress" pal_futurama()
    Rick and Morty scale_color_rickandmorty()
    scale_fill_rickandmorty()
    "schwifty" pal_rickandmorty()
    The Simpsons scale_color_simpsons()
    scale_fill_simpsons()
    "springfield" pal_simpsons()
    GSEA scale_color_gsea()
    scale_fill_gsea()
    "default" pal_gsea()
    Material Design scale_color_material()
    scale_fill_material()
    "red"
    "pink"
    "purple"
    "deep-purple"
    "indigo"
    "blue"
    "light-blue"
    "cyan"
    "teal"
    "green"
    "light-green"
    "lime"
    "yellow"
    "amber"
    "orange"
    "deep-orange"
    "brown"
    "grey"
    "blue-grey"
    pal_material()

    里面提供了多种配色方法。

    其中的fill 与color 分别对应ggplot 对象中的对应属性。

    该函数调用非常简单,直接尝试一下:

    1. p1 <- ggplot(subset(diamonds, carat > 2.2), aes(x = color, y = price, fill = color)) + geom_boxplot(color = 'black') + theme_bw()
    2. p1 + scale_fill_nejm()

    01. R 科研绘图调色板--ggsci - 图2

    新英格兰杂志(NEJM)

    1. p2 <- 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) +
    2. theme_bw()
    3. p2 + scale_color_aaas()

    01. R 科研绘图调色板--ggsci - 图3

    AAAS