Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

How to change the color of log2 fold change bars? #136

Open
marwa38 opened this issue Jan 31, 2025 · 3 comments
Open

How to change the color of log2 fold change bars? #136

marwa38 opened this issue Jan 31, 2025 · 3 comments

Comments

@marwa38
Copy link

marwa38 commented Jan 31, 2025

Hi all
Once of my paper colors for the groups in comparison is the same as the color of log2 fold change bars, any advice how to chnage it? as it is confusing.

here is the code and the figure

metadata <- read_delim("metadata.tsv", delim = "\t", escape_double = FALSE, trim_ws = TRUE) 


metadata_VMV_ds_py <- metadata %>% filter(Regime == "VMV" & Region %in% c("distal", "pyloric"))

sample_ids_VMV_ds_py <- metadata_VMV_ds_py$sample.ID  # Replace `sample.ID` with the actual column name in metadata
metacyc_VMV_ds_py <- metacyc_abundance[, colnames(metacyc_abundance) %in% sample_ids_VMV_ds_py]

metacyc_daa_results_df_VMV_ds_py <- pathway_daa(abundance = metacyc_VMV_ds_py, 
                                                metadata = metadata_VMV_ds_py, group = "Region", daa_method = "LinDA")

# Remove pathways with fewer than 3 nonzero values
metacyc_MMV_ds_py_filtered <- metacyc_MMV_ds_py[rowSums(metacyc_MMV_ds_py != 0) >= 3, ]

# Run LinDA analysis again
metacyc_daa_results_df_MMV_ds_py <- pathway_daa(
  abundance = metacyc_MMV_ds_py_filtered, 
  metadata = metadata_MMV_ds_py, 
  group = "Region", 
  daa_method = "LinDA"
)
metadata_MMV_ds_py$Region <- factor(metadata_MMV_ds_py$Region, levels = c("pyloric", "distal"))
pathway_errorbar(abundance = metacyc_MMV_ds_py,
                 daa_results_df = metacyc_daa_annotated_results_df_MMV_ds_py, 
                 Group = metadata_MMV_ds_py$Region, 
                 ko_to_kegg = FALSE, 
                 p_values_threshold = 0.05, order = "group", select = NULL, 
                 p_value_bar = TRUE,
                 colors = c("distal" = "#87CEEB", "pyloric" = "#FFA500"),  # Define colors directly
                 x_lab = "description")

Image

another side warning note that I am getting not sure it is affecting my figures etc, is

Warning message:
No shared levels found between `names(values)` of the manual scale and the data's colour values. 

although I converted the column metadata_MMV_ds_py$Region to factor

another point that I noticed is that although I changed the levels to be in a specific order pyloric then distal that doesn't change that the pyloric bars be above the distal bars although they changed the order in the legend as I changed to (pyloric then distal, orginally was distal above then pyloric down in the legend). I think there is something that need to change in the function pathway_errorbar()?

Thanks
Marwa

@marwa38
Copy link
Author

marwa38 commented Jan 31, 2025

> sessionInfo()
R version 4.4.2 (2024-10-31 ucrt)
Platform: x86_64-w64-mingw32/x64
Running under: Windows 11 x64 (build 26100)

Matrix products: default


locale:
[1] LC_COLLATE=English_United States.utf8  LC_CTYPE=English_United States.utf8   
[3] LC_MONETARY=English_United States.utf8 LC_NUMERIC=C                          
[5] LC_TIME=English_United States.utf8    

time zone: Europe/Paris
tzcode source: internal

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

other attached packages:
 [1] patchwork_1.3.0       ggprism_1.0.5         lubridate_1.9.4       forcats_1.0.0        
 [5] stringr_1.5.1         dplyr_1.1.4           purrr_1.0.2           tidyr_1.3.1          
 [9] ggplot2_3.5.1         tidyverse_2.0.0       tibble_3.2.1          ggpicrust2_1.7.4     
[13] readr_2.1.5           ALDEx2_1.28.0         zCompositions_1.5.0-4 truncnorm_1.0-9      
[17] NADA_1.6-1.1          survival_3.8-3        MASS_7.3-64          

loaded via a namespace (and not attached):
  [1] Rdpack_2.6.2                rlang_1.1.4                 magrittr_2.0.3             
  [4] clue_0.3-66                 matrixStats_1.5.0           compiler_4.4.2             
  [7] vctrs_0.6.5                 RcppZiggurat_0.1.6          rmutil_1.1.10              
 [10] pkgconfig_2.0.3             crayon_1.5.3                fastmap_1.2.0              
 [13] XVector_0.46.0              labeling_0.4.3              utf8_1.2.4                 
 [16] modeest_2.4.0               rmarkdown_2.29              tzdb_0.4.0                 
 [19] nloptr_2.1.1                UCSC.utils_1.2.0            bit_4.5.0.1                
 [22] xfun_0.49                   Rfast_2.1.3                 zlibbioc_1.52.0            
 [25] aplot_0.2.4                 GenomeInfoDb_1.42.1         jsonlite_1.8.9             
 [28] DelayedArray_0.32.0         BiocParallel_1.40.0         cluster_2.1.8              
 [31] parallel_4.4.2              R6_2.5.1                    RColorBrewer_1.1-3         
 [34] stringi_1.8.4               GGally_2.2.1                MicrobiomeStat_1.2         
 [37] rpart_4.1.24                boot_1.3-31                 numDeriv_2016.8-1.1        
 [40] GenomicRanges_1.58.0        Rcpp_1.0.13-1               SummarizedExperiment_1.36.0
 [43] iterators_1.0.14            knitr_1.49                  IRanges_2.40.1             
 [46] Matrix_1.7-1                splines_4.4.2               timechange_0.3.0           
 [49] tidyselect_1.2.1            rstudioapi_0.17.1           abind_1.4-8                
 [52] yaml_2.3.10                 timeDate_4041.110           codetools_0.2-20           
 [55] plyr_1.8.9                  lattice_0.22-6              lmerTest_3.1-3             
 [58] Biobase_2.66.0              withr_3.0.2                 stable_1.1.6               
 [61] evaluate_1.0.3              gridGraphics_0.5-1          ggstats_0.8.0              
 [64] RcppParallel_5.1.9          pillar_1.10.1               MatrixGenerics_1.18.1      
 [67] foreach_1.5.2               stats4_4.4.2                reformulas_0.4.0           
 [70] ggfun_0.1.8                 generics_0.1.3              vroom_1.6.5                
 [73] S4Vectors_0.44.0            hms_1.1.3                   munsell_0.5.1              
 [76] scales_1.3.0                timeSeries_4041.111         minqa_1.2.8                
 [79] glue_1.8.0                  statip_0.2.3                tools_4.4.2                
 [82] spatial_7.3-18              lme4_1.1-36                 fBasics_4041.97            
 [85] fs_1.6.5                    grid_4.4.2                  rbibutils_2.3              
 [88] colorspace_2.1-1            nlme_3.1-166                GenomeInfoDbData_1.2.13    
 [91] cli_3.6.3                   S4Arrays_1.6.0              gtable_0.3.6               
 [94] ggh4x_0.3.0                 stabledist_0.7-2            yulab.utils_0.1.9          
 [97] digest_0.6.37               BiocGenerics_0.52.0         SparseArray_1.6.0          
[100] ggrepel_0.9.6               ggplotify_0.1.2             farver_2.1.2               
[103] htmltools_0.5.8.1           multtest_2.62.0             lifecycle_1.0.4            
[106] httr_1.4.7                  statmod_1.5.0               bit64_4.5.2  

@PeterUgbanu
Copy link

Hi @marwa38 ,

Unfortunately, the colour of log2 fold change bars is hardcoded in the function. So you might just have to run your own plot function manually.

The warning error is probably because the errorbar plot function scales both the fill and colour manually while the main aesthetics is just fill. I would just ignore the error.

About the order of the bar, I think it is a ggplot2 specific thing. It basically plots the first datapoint according to your factor before the second (virtually ordering from zero to max y value). So the pyloric comes first before the distal in the order of the bars (e.g imaging that y is from 0 to 1. so the first value is placed closest to zero and the the other values comes after -> this is how I think about it).
I think if you don't factor the groups, the distal will be first while the pyloric comes second giving you the order you want but the legend arrangement will be switched.

Alternatively, you could make the plot manually and use position_dodge2(reverse=TRUE) as position argument in the geom bar function.
Or use add breaks to scale fill manually to change the legend order to match the dodged plot order.

Truly I am not sure how you can modify these using the plot variable from the plot errorbar function. So I personally doing my plots myself.

@marwa38
Copy link
Author

marwa38 commented Feb 4, 2025

@PeterUgbanu Thanks so much for your reply.

I think if you don't factor the groups, the distal will be first while the pyloric comes second giving you the order you want but the legend arrangement will be switched.

That exactly what happened and I tried yesterday.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

2 participants