YuLab-SMU / ggtree

:christmas_tree:Visualization and annotation of phylogenetic trees
https://yulab-smu.top/treedata-book/
830 stars 172 forks source link

scale_color_manual not working when geom_fruit (with geom = geom_boxplot) is used #551

Closed Suirotras closed 1 year ago

Suirotras commented 1 year ago

I might have found a bug in the ggtree and ggtreeextra relating to the use of geom_fruit(). I found that using geom_fruit with geom = geom_boxplot makes the plot unresponsive to scale_color_manual. I have put a simple example in this post.

library(ggtree)
library(dplyr)
library(tibble)
library(ggtreeExtra)
library(ggplot2)
library(MetBrewer)

### create fake data

tree <- rtree(n = 9)

tree_tibble <- as.tibble(tree)

fake_data <- tibble("label" = tree_tibble$label, "data" = rnorm(nrow(tree_tibble)))

### scale_fill_manuel with geom = geom_bar

p1 <- ggtree(tree, aes(x, y, col = label), size = 2, show.legend = FALSE) + 
  geom_fruit(data = fake_data, aes(y = label, x = data, fill = label), geom = geom_bar, 
             stat = "identity")

p2 <- p1 + scale_color_manual(values = MetBrewer::met.brewer("Cross")) +
  scale_fill_manual(values = MetBrewer::met.brewer("Cross"))

p2

### scale_fill_manuel with geom = geom_boxplot

p1 <- ggtree(tree, aes(x, y, col = label), size = 2, show.legend = FALSE) + 
  geom_fruit(data = fake_data, aes(y = label, x = data, fill = label), 
             geom = geom_boxplot, stat = "boxplot")

p2 <- p1 + scale_color_manual(values = MetBrewer::met.brewer("Cross")) +
  scale_fill_manual(values = MetBrewer::met.brewer("Cross"))

p2

Using geom_fruit without geom_boxplot appears to be no problem, as the the above code produces the following: image

However, scale_color_manual does not change the color values anymore when using geom_boxplot with geom = geom_boxplot, as it produces the following plot.

image

Maybe it is a local problem on my computer, but I thought I it would still be useful to post it here.

A temporary workaround I used was to change the standard ggplot color values:

options(ggplot2.discrete.colour= MetBrewer::met.brewer("Cross"))

Here is the output of sessioninfo for the specific version information.

R version 4.2.1 (2022-06-23 ucrt)
Platform: x86_64-w64-mingw32/x64 (64-bit)
Running under: Windows 10 x64 (build 22621)

Matrix products: default

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

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

other attached packages:
[1] MetBrewer_0.2.0   ggplot2_3.3.6     ggtreeExtra_1.6.1 tibble_3.1.8      dplyr_1.0.10     
[6] ggtree_3.4.2     

loaded via a namespace (and not attached):
 [1] Rcpp_1.0.9         pillar_1.8.1       compiler_4.2.1     yulab.utils_0.0.5  tools_4.2.1       
 [6] digest_0.6.29      aplot_0.1.7        jsonlite_1.8.0     tidytree_0.4.1     lifecycle_1.0.2   
[11] nlme_3.1-159       gtable_0.3.1       lattice_0.20-45    pkgconfig_2.0.3    rlang_1.0.6       
[16] cli_3.4.1          DBI_1.1.3          ggplotify_0.1.0    rstudioapi_0.14    patchwork_1.1.2   
[21] parallel_4.2.1     treeio_1.20.2      withr_2.5.0        generics_0.1.3     vctrs_0.4.1       
[26] gridGraphics_0.5-1 grid_4.2.1         tidyselect_1.1.2   ggnewscale_0.4.7   glue_1.6.2        
[31] R6_2.5.1           fansi_1.0.3        farver_2.1.1       purrr_0.3.4        tidyr_1.2.1       
[36] magrittr_2.0.3     scales_1.2.1       assertthat_0.2.1   ape_5.6-2          colorspace_2.0-3  
[41] labeling_0.4.2     utf8_1.2.2         lazyeval_0.2.2     munsell_0.5.0      crayon_1.5.1      
[46] ggfun_0.0.7
xiangpin commented 1 year ago

This is because the color of geom_boxplot in geom_fruit was used in the internal. You can use aesthetics='colour_new_new' in the scale_color_manual to do it.

p2 <- p1 + 
         scale_color_manual(aesthetics='colour_new_new', values = MetBrewer::met.brewer("Cross")) +
         scale_fill_manual(values = MetBrewer::met.brewer("Cross"))
Suirotras commented 1 year ago

Thanks for the reply and the nice solution. That worked!

And thank you for developing this package, which has been immensely helpfull in my visualizations