corybrunson / ggalluvial

ggplot2 extension for alluvial plots
http://corybrunson.github.io/ggalluvial/
GNU General Public License v3.0
499 stars 34 forks source link

X axis not separating #115

Closed gpessoaamorim closed 1 year ago

gpessoaamorim commented 1 year ago

I have data in the following structure:

image

image

Using the code below

alluvial_plot_data%>%
  ggplot(aes(x=Period, 
             stratum=Event,
             alluvium=participant_id,
             fill=Event,
             label=Event
  ))+
  geom_flow(stat="alluvium" )+
  geom_stratum(na.rm=T)+
  facet_wrap(~labels, 
             scales="free_y",
             labeller = label_wrap_gen(width = 20))+
  geom_text(
    data=.%>%filter(Period=="At\nrandomisation"),
    stat="stratum",
    aes(label=
          paste0(after_stat(count),
                 "\n(",
                 round(after_stat(prop)*100,1), "%)")
    ),
    nudge_x = -0.4,
  )+
  geom_text(data=.%>%filter(Period!="At\nrandomisation"),
            stat="stratum",
            aes(label=
                  paste0(after_stat(count), 
                         "\n(",
                         round(after_stat(prop)*100,1), "%)")
            ),
            nudge_x = 0.4,
  )+

  geom_text_repel(
    stat="flow", 
    aes(color = Period == "At\nrandomisation",
        label=after_stat(count)),
    nudge_x=0.3,
    show.legend = F,
    segment.size=0.2,
    segment.color = 'transparent'

  )+
  theme(text=element_text(size=20,
                          color="black"),
        legend.position="bottom")+
  scale_color_manual(values=c("00000000", "Black"))+
  scale_y_continuous(expand=expansion(c(0.2,0.2)))

I was able to produce the following plot: image

For some reason, I have rerun my code and now I get something similar to this:

image

It seems that the x axis is not being properly separated, despite both the upstream code and the plotting code remaining the same. Note the x axis is specified as factors as shown on top

Session info here:

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=English_United Kingdom.utf8 
[2] LC_CTYPE=English_United Kingdom.utf8   
[3] LC_MONETARY=English_United Kingdom.utf8
[4] LC_NUMERIC=C                           
[5] LC_TIME=English_United Kingdom.utf8    

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

other attached packages:
 [1] beepr_1.3          irr_0.84.1         lpSolve_5.6.15    
 [4] fmsb_0.7.3         officer_0.4.3      flextable_0.7.2   
 [7] gtsummary_1.6.1    tableone_0.13.2    palettes_0.1.1    
[10] RColorBrewer_1.1-3 ggalluvial_0.12.3  ggside_0.2.1      
[13] ggthemes_4.2.4     ggdark_0.2.1       ggrepel_0.9.1     
[16] viridis_0.6.2      viridisLite_0.4.0  ggridges_0.5.3    
[19] patchwork_1.1.1    scales_1.2.0       skimr_2.1.4       
[22] magrittr_2.0.3     lubridate_1.8.0    forcats_0.5.1     
[25] stringr_1.4.0      dplyr_1.1.2        purrr_0.3.4       
[28] readr_2.1.2        tidyr_1.2.0        tibble_3.2.1      
[31] ggplot2_3.4.0      tidyverse_1.3.2    readxl_1.4.0      
[34] Rcpp_1.0.9         RODBC_1.3-19       DBI_1.1.3         
[37] odbc_1.3.3        

loaded via a namespace (and not attached):
 [1] fs_1.5.2            bit64_4.0.5         httr_1.4.3         
 [4] repr_1.1.4          tools_4.2.2         backports_1.4.1    
 [7] utf8_1.2.2          R6_2.5.1            colorspace_2.0-3   
[10] withr_2.5.0         tidyselect_1.2.0    gridExtra_2.3      
[13] bit_4.0.4           compiler_4.2.2      textshaping_0.3.6  
[16] cli_3.3.0           rvest_1.0.2         gt_0.6.0           
[19] xml2_1.3.3          labeling_0.4.2      systemfonts_1.0.4  
[22] digest_0.6.29       rmarkdown_2.14      base64enc_0.1-3    
[25] pkgconfig_2.0.3     htmltools_0.5.3     dbplyr_2.2.1       
[28] fastmap_1.1.0       rlang_1.1.1         rstudioapi_0.13    
[31] farver_2.1.1        generics_0.1.3      jsonlite_1.8.0     
[34] zip_2.2.0           googlesheets4_1.0.0 Matrix_1.5-1       
[37] munsell_0.5.0       fansi_1.0.3         gdtools_0.2.4      
[40] lifecycle_1.0.3     stringi_1.7.8       plyr_1.8.7         
[43] grid_4.2.2          blob_1.2.3          crayon_1.5.1       
[46] lattice_0.20-45     haven_2.5.0         splines_4.2.2      
[49] hms_1.1.1           knitr_1.39          pillar_1.9.0       
[52] uuid_1.1-0          reprex_2.0.1        glue_1.6.2         
[55] evaluate_0.15       mitools_2.4         data.table_1.14.2  
[58] broom.helpers_1.8.0 modelr_0.1.8        vctrs_0.6.3        
[61] tzdb_0.3.0          cellranger_1.1.0    gtable_0.3.1       
[64] assertthat_0.2.1    xfun_0.31           broom_1.0.0        
[67] survey_4.1-1        ragg_1.2.4          survival_3.4-0     
[70] googledrive_2.0.0   audio_0.1-10        gargle_1.2.0       
[73] ellipsis_0.3.2     

Thanks for your thoughts

gpessoaamorim commented 1 year ago

I believe the only change I have made to my session lately was to update tidyverse to the latest version; my ggalluvial version is still 0.12.3

corybrunson commented 1 year ago

@gpessoaamorim thanks a lot for raising this issue. I will get to it within a week.

Would you be able to share a subset or toy version of your data that reproduces the second (bad) plot?

gpessoaamorim commented 1 year ago

Thanks - subset with toy data attached

alluvial_plot_data_sample.csv

corybrunson commented 1 year ago

Thank you! I ran the code with these data using both versions of {ggalluvial}—0.12.5 and 0.12.3. Both runs produced the same plot, the "good" one above. Could it be that one of your other packages is out of date? If you upgrade to 0.12.5, does this resolve the problem? (Or is there a reason you can't upgrade right now?)

gpessoaamorim commented 1 year ago

You're absolutely right - just updating {ggalluvial} to 0.12.5 did the trick - I wasn't expecting that to be the issue from the release notes, but I guess it's always worth a try! Thanks for looking into this and apologies for the unwarranted issue request - closed now

corybrunson commented 1 year ago

@gpessoaamorim no problem! And this was not an unwarranted issue—it's valuable to know that this behavior is possible, even if it's possibly not a problem with {ggalluvial} itself. Thanks again.