美文网首页
ggsci | 一行代码拥有Sci级配色

ggsci | 一行代码拥有Sci级配色

作者: 生命数据科学 | 来源:发表于2022-12-27 12:03 被阅读0次

    在期刊发表过程中,一个好看的配色总是能让文章更加和谐,我自己也认为,在数据不错的情况下,一篇文章中的图片如果更好看一点的话,一来是证明作者态度比较认真,二来也是科研能力的体现,三来就是审美的一致性了

    当然,这些都是个人感受,与科研实际结果无关,只是在能一行代码拥有好看配色的情况下,何不尝试一下呢

    1. 运行环境

    > sessionInfo()
    R version 4.2.2 (2022-10-31 ucrt)
    Platform: x86_64-w64-mingw32/x64 (64-bit)
    Running under: Windows 10 x64 (build 19045)
    
    Matrix products: default
    
    locale:
    [1] LC_COLLATE=Chinese (Simplified Han)_Hong Kong SAR.utf8 
    [2] LC_CTYPE=Chinese (Simplified Han)_Hong Kong SAR.utf8   
    [3] LC_MONETARY=Chinese (Simplified Han)_Hong Kong SAR.utf8
    [4] LC_NUMERIC=C                                           
    [5] LC_TIME=Chinese (Simplified Han)_Hong Kong SAR.utf8    
    
    attached base packages:
    [1] stats     graphics  grDevices utils     datasets  methods   base     
    
    other attached packages:
    [1] gridExtra_2.3 ggplot2_3.4.0 ggsci_2.9     dplyr_1.0.10 
    
    loaded via a namespace (and not attached):
     [1] zip_2.2.2         Rcpp_1.0.9        pillar_1.8.1      compiler_4.2.2   
     [5] tools_4.2.2       viridisLite_0.4.1 lattice_0.20-45   nlme_3.1-160     
     [9] lifecycle_1.0.3   tibble_3.1.8      gtable_0.3.1      mgcv_1.8-41      
    [13] pkgconfig_2.0.3   rlang_1.0.6       Matrix_1.5-3      openxlsx_4.2.5.1 
    [17] DBI_1.1.3         cli_3.4.1         rstudioapi_0.14   withr_2.5.0      
    [21] generics_0.1.3    vctrs_0.5.1       grid_4.2.2        tidyselect_1.2.0 
    [25] glue_1.6.2        R6_2.5.1          fansi_1.0.3       farver_2.1.1     
    [29] magrittr_2.0.3    scales_1.2.1      splines_4.2.2     assertthat_0.2.1 
    [33] colorspace_2.0-3  labeling_0.4.2    utf8_1.2.2        stringi_1.7.8    
    [37] munsell_0.5.0     crayon_1.5.2 
    

    2. 准备工作

    # 所需要的包
    library("ggsci") #必须
    library("ggplot2") #必须
    library("gridExtra") #示例数据,非必须
    
    data("diamonds") # 加载示例数据
    
    # 浅看diamonds数据内容
    > head(diamonds,10)
    # A tibble: 10 × 10
       carat cut       color clarity depth table price     x     y     z
       <dbl> <ord>     <ord> <ord>   <dbl> <dbl> <int> <dbl> <dbl> <dbl>
     1  0.23 Ideal     E     SI2      61.5    55   326  3.95  3.98  2.43
     2  0.21 Premium   E     SI1      59.8    61   326  3.89  3.84  2.31
     3  0.23 Good      E     VS1      56.9    65   327  4.05  4.07  2.31
     4  0.29 Premium   I     VS2      62.4    58   334  4.2   4.23  2.63
     5  0.31 Good      J     SI2      63.3    58   335  4.34  4.35  2.75
     6  0.24 Very Good J     VVS2     62.8    57   336  3.94  3.96  2.48
     7  0.24 Very Good I     VVS1     62.3    57   336  3.95  3.98  2.47
     8  0.26 Very Good H     SI1      61.9    55   337  4.07  4.11  2.53
     9  0.22 Fair      E     VS2      65.1    61   337  3.87  3.78  2.49
    10  0.23 Very Good H     VS1      59.4    61   338  4     4.05  2.39
    
    # 本文其实只使用了4列数据
    > diamonds[,c("price","table","cut","depth")]
    # A tibble: 53,940 × 4
       price table cut       depth
       <int> <dbl> <ord>     <dbl>
     1   326    55 Ideal      61.5
     2   326    61 Premium    59.8
     3   327    65 Good       56.9
     4   334    58 Premium    62.4
     5   335    58 Good       63.3
     6   336    57 Very Good  62.8
     7   336    57 Very Good  62.3
     8   337    55 Very Good  61.9
     9   337    61 Fair       65.1
    10   338    61 Very Good  59.4
    # … with 53,930 more rows
    # ℹ Use `print(n = ...)` to see more rows
    

    3. 出个图

    3.1 点线图

    这里是点、线图,将diamonds数据集中carat大于等于2.2的行筛选出来进行绘图

    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()
    p1
    

    p1 :

    image

    上图是一个简单的点、线图,cut分组颜色过于相似,因此区分不是特别明显,我个人认为可以采用明显不同的颜色进行绘图,比如柳叶刀配色

    p1_lancet <- 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() + scale_color_lancet()
    p1_lancet
    

    p1_lancet:

    image

    再来试试新英格兰医学杂志配色

    p1_nejm <- 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() + scale_color_lancet()
    p1_nejm
    

    p1_nejm:

    image

    3.2 不同配色方案汇总

    其实对比柳叶刀新英格兰的代码可以发现,仅需修改一行代码即可scale_color_lancet()
    所有的套路都是如此,可以自行尝试~

    Name Scales
    NPG scale_color_npg() scale_fill_npg()
    AAAS scale_color_aaas() scale_fill_aaas()
    NEJM scale_color_nejm() scale_fill_nejm()
    Lancet scale_color_lancet() scale_fill_lancet()
    JAMA scale_color_jama() scale_fill_jama()
    JCO scale_color_jco() scale_fill_jco()
    UCSCGB scale_color_ucscgb() scale_fill_ucscgb()
    D3 scale_color_d3() scale_fill_d3()
    LocusZoom scale_color_locuszoom() scale_fill_locuszoom()
    IGV scale_color_igv() scale_fill_igv()
    COSMIC scale_color_cosmic() scale_fill_cosmic()
    UChicago scale_color_uchicago() scale_fill_uchicago()
    Star Trek scale_color_startrek() scale_fill_startrek()
    Tron Legacy scale_color_tron() scale_fill_tron()
    Futurama scale_color_futurama() scale_fill_futurama()
    Rick and Morty scale_color_rickandmorty() scale_fill_rickandmorty()
    The Simpsons scale_color_simpsons() scale_fill_simpsons()
    GSEA scale_color_gsea() scale_fill_gsea()
    Material Design scale_color_material() scale_fill_material()

    那上面表中的scale_fill_xxx是什么呢?

    这其实是ggplot2的语法,scale_color_xxx代表的是描边,其实可以理解为一维数据比如点、线scale_fill_xxx代表填充,类似二维数据,比如平面

    3.3柱形图

    举个栗子

    # 还是使用的diamonds数据集
    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()+
      scale_fill_nejm()
    p2
    
    image
    类似于以上的柱状图,就需要用scale_fill_xxx

    3.4 补充

    之前所有的图都是基于ggplot2语法,因此用法比较固定,当然还有一种需求,就是把色号提取出来,自由搭配,这里展示提取不同配色方案颜色的CMYK值

    # 总的来说,语法就是pal_xxx()
    
    # 比如lancet:
    > mypal <- pal_lancet(palette = c("lanonc"), alpha = 1)(9)
    > mypal
    [1] "#00468BFF" "#ED0000FF" "#42B540FF" "#0099B4FF" "#925E9FFF" "#FDAF91FF"
    [7] "#AD002AFF" "#ADB6B6FF" "#1B1919FF"
    
    # alpha 代表透明度,(9)代表提取9种颜色
    
    # 关于用法可以查看帮助
    ??pal_lancet
    
    # 查看颜色
    library("scales")
    show_col(mypal)
    
    image

    感谢观看,如果有用还请点赞,关注,收藏,转发

    相关文章

      网友评论

          本文标题:ggsci | 一行代码拥有Sci级配色

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