两种GO条形图,你选哪个?

西瓜站长 生信益站 2024-04-18 08:00
图片

生信益站,一点就有益祝友友们天天开心,月月发 CNS~



图片
设我为🌟星标,每天就能第一时间看到推送啦~

常规的 GO 分类条形图如下所示(想要此图代码的私聊我):

图片

好看是好看,就是文本太占空间。

其实这时候,我们可以把 x 轴的 text,也就是GO term向上平移到条形图之中。如下所示:

图片

今天绘图用到的数据如下:

图片

具体处理可以参考这篇推文:《R 绘图技巧 (2):双坐标图-以 GO 分类图为例》。

基本图形

maxp <- max(WEGO2$`Percentage of genes`)
library(ggplot2)

ggplot(data = WEGO2,
       aes(x = fct_reorder(Description, desc(Position)),
           y = `Percentage of genes`,
           fill = Level1)) +
    scale_fill_manual(values = c("#25A17C","#D96622","#6C73B2")) +
    geom_col(data = WEGO2,
             width = 0.5,
             position = "dodge",
             aes(x = fct_reorder(Description, desc(Position)),
                 y = `Number of genes`/normalizer)) +

    # 双坐标轴的关键属性sec.axis
    scale_y_continuous(limits = c(0, maxp+3),
                       sec.axis = sec_axis(trans = ~.*normalizer,
                                           name = "Number of genes"),
                       expand = c(0,0)) +
    theme_classic() +
    theme(axis.text.x = element_blank(),
          axis.ticks.x = element_blank(),
          axis.title.y = element_text(size = 10),
          legend.text = element_text(size = 7),
          legend.title = element_text(size = 8),
          legend.key.size =  unit(0.2"in"),
          legend.position = "none",
          panel.border = element_blank(),
          axis.line.y  = element_line(color = "black",
                                      size = 1,
                                      lineend = "square"),
          plot.title = element_text(size = 15,
                                    hjust = 0.5)) +
    labs(x = NULL,
         title = "Gene Ontology Classification",
         fill = NULL) -> p

print(p)
图片

使用分面

library(ggh4x)

strip <- strip_themed(background_x = elem_list_rect(color = c("#25A17C","#D96622","#6C73B2")))
p + facet_wrap2(~Level1,
                nrow = 1,
                scales = "free_x",
                strip.position = "bottom",
                strip = strip) +
    geom_text(aes(y = rep(1, nrow(WEGO2)),
                  label = Description),
              size = 7,
              angle = 90,
              hjust = 0) +
    theme(axis.line.x = element_blank(),
          strip.background = element_rect(size = 2),
          strip.text = element_text(face = "bold",
                                    size = rel(1.5))) +
    annotate(geom = 'segment',
             y = Inf,
             yend = Inf,
             color = 'black',
             x = -Inf,
             xend = Inf,
             linewidth = 2)

# ggsave("go_classify_plot.png",width = 14,height = 6,dpi = 300)
图片

条形图 width 调整

p + facet_grid2(~Level1,
                scales = "free_x",
                space = "free_x",
                strip = strip,
                switch = "both") +
    geom_text(aes(y = rep(1,nrow(WEGO2)),
                  label = Description),
              size = 7,
              angle = 90,
              hjust = 0) +
    theme(axis.line.x = element_blank(),
          strip.background = element_rect(size = 2),
          strip.text = element_text(face = "bold",
                                   size = rel(1.5))) +
    annotate(geom = 'segment',
             y = Inf,
             yend = Inf,
             color = 'black',
             x = -Inf,
             xend = Inf,
             linewidth = 2)

# ggsave("go_classify_plot.png",width = 14,height = 6,dpi = 300)
图片

如果想让bar降序排列,如下:

图片

那么在最前面的代码中,就不要用desc()

图片

sessionInfo()

R version 4.1.2 (2021-11-01)

attached base packages:
[1] stats     graphics  grDevices utils     datasets  methods   base     

other attached packages:
 [1] ggh4x_0.2.3     forcats_0.5.1   stringr_1.4.0   dplyr_1.0.8     purrr_0.3.4     readr_2.1.2     tidyr_1.2.0    
 [8] tibble_3.1.6    ggplot2_3.4.4   tidyverse_1.3.1

loaded via a namespace (and not attached):
  [1] Seurat_4.3.0           Rtsne_0.16             colorspace_2.0-3       deldir_1.0-6           ellipsis_0.3.2        
  [6] ggridges_0.5.3         fs_1.5.2               rstudioapi_0.13        spatstat.data_3.0-0    farver_2.1.0          
 [11] leiden_0.4.3           listenv_0.8.0          ggrepel_0.9.1          lubridate_1.8.0        fansi_1.0.3           
 [16] xml2_1.3.3             codetools_0.2-18       splines_4.1.2          knitr_1.38             polyclip_1.10-0       
 [21] jsonlite_1.8.0         broom_0.7.12           ica_1.0-3              dbplyr_2.1.1           cluster_2.1.3         
 [26] png_0.1-7              uwot_0.1.14            shiny_1.7.1            sctransform_0.3.5      spatstat.sparse_3.0-0 
 [31] compiler_4.1.2         httr_1.4.2             backports_1.4.1        assertthat_0.2.1       SeuratObject_4.1.3    
 [36] Matrix_1.5-1           fastmap_1.1.0          lazyeval_0.2.2         cli_3.4.1              later_1.3.0           
 [41] htmltools_0.5.5        tools_4.1.2            igraph_1.3.0           gtable_0.3.0           glue_1.6.2            
 [46] RANN_2.6.1             reshape2_1.4.4         tinytex_0.38           Rcpp_1.0.8.3           scattermore_0.8       
 [51] cellranger_1.1.0       vctrs_0.5.1            spatstat.explore_3.0-5 nlme_3.1-157           progressr_0.11.0      
 [56] lmtest_0.9-40          spatstat.random_3.0-1  xfun_0.39              globals_0.16.2         rvest_1.0.2           
 [61] mime_0.12              miniUI_0.1.1.1         lifecycle_1.0.3        irlba_2.3.5.1          goftest_1.2-3         
 [66] future_1.29.0          MASS_7.3-56            zoo_1.8-10             scales_1.3.0           hms_1.1.1             
 [71] promises_1.2.0.1       spatstat.utils_3.0-1   parallel_4.1.2         ggunchull_0.1.0        RColorBrewer_1.1-3    
 [76] yaml_2.3.5             reticulate_1.26        pbapply_1.6-0          gridExtra_2.3          stringi_1.7.6         
 [81] rlang_1.1.3            pkgconfig_2.0.3        matrixStats_0.61.0     evaluate_0.15          lattice_0.20-45       
 [86] ROCR_1.0-11            tensor_1.5             labeling_0.4.2         patchwork_1.1.1        htmlwidgets_1.6.2     
 [91] cowplot_1.1.1          tidyselect_1.1.2       parallelly_1.32.1      RcppAnnoy_0.0.20       plyr_1.8.7            
 [96] magrittr_2.0.3         R6_2.5.1               generics_0.1.2         DBI_1.1.2              withr_2.5.0           
[101] haven_2.5.0            pillar_1.7.0           fitdistrplus_1.1-8     survival_3.3-1         abind_1.4-5           
[106] sp_1.5-1               future.apply_1.10.0    modelr_0.1.8           crayon_1.5.1           KernSmooth_2.23-20    
[111] utf8_1.2.2             spatstat.geom_3.0-3    plotly_4.10.0          tzdb_0.3.0             rmarkdown_2.21        
[116] readxl_1.4.0           grid_4.1.2             data.table_1.14.2      reprex_2.0.1           digest_0.6.29         
[121] xtable_1.8-4           httpuv_1.6.5           munsell_0.5.0          viridisLite_0.4.0     

OK,今天的分享到此为止。咱们明天见~

联系站长

本篇文章有疑问,或者有科研服务需求的友友可以在益站发消息留言,也欢迎各位童鞋扫下面的二维码加入我们的 QQ 交流群。

图片
科研服务

图片
最新QQ群

图片
您的关注、点赞、在看、转发是对益站最大的鼓励和支持

绘图专题 · 目录
上一篇多组学整合分析(七) ——多组学网络分析下一篇抛弃火山图的第一天...
个人观点,仅供参考

微信扫一扫
关注该公众号