ggrepel icon indicating copy to clipboard operation
ggrepel copied to clipboard

ggrepel does not put labels at correct position of dotplot

Open svenbioinf opened this issue 3 years ago • 1 comments

Summary

When creating a plot with geom_dotplot, geom_text_repel does not draw the label arrows to the exact point location but rather to the center. (See points B1,B2,B3) Is there any way to do this?

Minimal code example

Here is the minimum amount of code needed to demonstrate the issue:

df=data.frame(name=c("A1","A2","B1","B2","B3"),value=c(1,2,2,2,2),group=c("A","A","B","B","B"))
ggplot(df,aes(x=group,y=value,label=name))+
  geom_dotplot(binaxis='y', stackdir='center', dotsize=0.7)+
  geom_text_repel(aes(segment.color="red"),box.padding = 1,max.overlaps=10)

Here is an image of the output produced by the code:

dotplot

Suggestions

I tried extracting the correct positions from geom_dotplot to feed them to geom_text_repel which did not work, so now I am out of ideas.

Version information

Here is the output from sessionInfo() in my R session:

sessionInfo()
R version 4.0.3 (2020-10-10)
Platform: x86_64-pc-linux-gnu (64-bit)
Running under: Linux Mint 19

Matrix products: default
BLAS:   /usr/lib/x86_64-linux-gnu/blas/libblas.so.3.7.1
LAPACK: /usr/lib/x86_64-linux-gnu/lapack/liblapack.so.3.7.1

locale:
 [1] LC_CTYPE=en_US.UTF-8       LC_NUMERIC=C               LC_TIME=en_US.UTF-8        LC_COLLATE=en_US.UTF-8    
 [5] LC_MONETARY=de_DE.UTF-8    LC_MESSAGES=en_US.UTF-8    LC_PAPER=de_DE.UTF-8       LC_NAME=C                 
 [9] LC_ADDRESS=C               LC_TELEPHONE=C             LC_MEASUREMENT=de_DE.UTF-8 LC_IDENTIFICATION=C       

attached base packages:
 [1] grid      tcltk     parallel  stats4    stats     graphics  grDevices utils     datasets  methods   base     

other attached packages:
 [1] ggpubr_0.4.0                waffle_1.0.1                scales_1.1.1               
 [4] ggsci_2.9                   PCAtools_2.2.0              ggrepel_0.9.1              
 [7] flextable_0.6.6             RColorBrewer_1.1-2          VennDiagram_1.6.20         
[10] futile.logger_1.4.3         Mfuzz_2.50.0                DynDoc_1.68.0              
[13] widgetTools_1.68.0          e1071_1.7-6                 pheatmap_1.0.12            
[16] ggplot2_3.3.3               Rmisc_1.5                   plyr_1.8.6                 
[19] lattice_0.20-44             tidyr_1.1.3                 biomaRt_2.47.9             
[22] DESeq2_1.30.1               SummarizedExperiment_1.20.0 Biobase_2.50.0             
[25] MatrixGenerics_1.2.1        matrixStats_0.58.0          GenomicRanges_1.42.0       
[28] GenomeInfoDb_1.26.7         IRanges_2.24.1              S4Vectors_0.28.1           
[31] BiocGenerics_0.36.1         tximport_1.18.0             data.table_1.14.0          

loaded via a namespace (and not attached):
  [1] readxl_1.3.1              uuid_0.1-4                backports_1.2.1           BiocFileCache_1.14.0     
  [5] systemfonts_1.0.2         splines_4.0.3             BiocParallel_1.24.1       digest_0.6.27            
  [9] htmltools_0.5.1.1         fansi_0.4.2               magrittr_2.0.1            memoise_2.0.0            
 [13] openxlsx_4.2.3            limma_3.46.0              annotate_1.68.0           extrafont_0.17           
 [17] officer_0.3.18            extrafontdb_1.0           prettyunits_1.1.1         colorspace_2.0-1         
 [21] blob_1.2.1                rappdirs_0.3.3            haven_2.4.1               xfun_0.23                
 [25] dplyr_1.0.6               crayon_1.4.1              RCurl_1.98-1.3            genefilter_1.72.1        
 [29] survival_3.2-11           glue_1.4.2                gtable_0.3.0              zlibbioc_1.36.0          
 [33] XVector_0.30.0            DelayedArray_0.16.3       car_3.0-10                BiocSingular_1.6.0       
 [37] Rttf2pt1_1.3.8            abind_1.4-5               futile.options_1.0.1      DBI_1.1.1                
 [41] rstatix_0.7.0             Rcpp_1.0.6                xtable_1.8-4              progress_1.2.2           
 [45] dqrng_0.3.0               foreign_0.8-81            bit_4.0.4                 rsvd_1.0.5               
 [49] proxy_0.4-25              DT_0.18.1                 htmlwidgets_1.5.3         httr_1.4.2               
 [53] ellipsis_0.3.2            pkgconfig_2.0.3           XML_3.99-0.6              farver_2.1.0             
 [57] dbplyr_2.1.1              locfit_1.5-9.4            utf8_1.2.1                tidyselect_1.1.1         
 [61] labeling_0.4.2            rlang_0.4.11              tkWidgets_1.68.0          reshape2_1.4.4           
 [65] AnnotationDbi_1.52.0      cellranger_1.1.0          munsell_0.5.0             tools_4.0.3              
 [69] cachem_1.0.5              cli_2.5.0                 generics_0.1.0            RSQLite_2.2.7            
 [73] broom_0.7.6               evaluate_0.14             stringr_1.4.0             fastmap_1.1.0            
 [77] knitr_1.33                bit64_4.0.5               zip_2.1.1                 purrr_0.3.4              
 [81] sparseMatrixStats_1.2.1   formatR_1.9               xml2_1.3.2                compiler_4.0.3           
 [85] rstudioapi_0.13           curl_4.3.1                ggsignif_0.6.1            tibble_3.1.2             
 [89] geneplotter_1.68.0        stringi_1.6.2             forcats_0.5.1             gdtools_0.2.3            
 [93] Matrix_1.3-3              vctrs_0.3.8               pillar_1.6.1              lifecycle_1.0.0          
 [97] cowplot_1.1.1             bitops_1.0-7              irlba_2.3.3               R6_2.5.0                 
[101] rio_0.5.26                gridExtra_2.3             lambda.r_1.2.4            assertthat_0.2.1         
[105] withr_2.4.2               GenomeInfoDbData_1.2.4    hms_1.1.0                 beachmat_2.6.4           
[109] class_7.3-19              rmarkdown_2.8             DelayedMatrixStats_1.12.3 carData_3.0-4            
[113] base64enc_0.1-3 

svenbioinf avatar Sep 30 '21 09:09 svenbioinf

@svenbioinf Have you considered whether any other visual presentation might be a better way to communicates your message to your audience? Sometimes tables are more effective than graphical figures.

This issue won't be fixed, because as far as I know, there is no ggplot2::position_*() function that is complementary to ggplot2::geom_dotplot().

Typically, the strategy in this situation is to first define a position and then draw a plot, e.g.:

my_pos <- ggplot2::position_dodge(width = 0.5)
ggplot(...) + geom_point(position = my_pos) + geom_text_repel(position = my_pos)

My reprex below is a hacky attempt to get something along these lines to work, but I don't recommend it.

Please consider reviewing related posts:

  • A duplicate ggrepel issue https://github.com/slowkow/ggrepel/issues/139

  • Related posts from Stack Overflow:

    • https://stackoverflow.com/questions/44991607/how-do-i-label-the-dots-of-a-geom-dotplot-in-ggplot2
    • https://stackoverflow.com/questions/48545286/geom-dotplot-with-text-instead-of-dots

library(ggrepel)
#> Loading required package: ggplot2
df <- data.frame(
  name = c("A1", "A2", "B1", "B2", "B3"),
  value = c(1, 2, 2, 2, 2),
  group = c("A", "A", "B", "B", "B")
)
ggplot(df, aes(x = group, y = value, label = name)) +
  geom_dotplot(binaxis = "y", stackdir = "center", dotsize = 0.7) +
  geom_text_repel(aes(segment.color = "red"), box.padding = 1, max.overlaps = 10)
#> Bin width defaults to 1/30 of the range of the data. Pick better value with `binwidth`.


ggplot(df, aes(x = group, y = value, label = name)) +
  geom_dotplot(binaxis = "y", stackdir = "center", dotsize = 0.7) +
  geom_text_repel(
    aes(segment.color = "red", group = name),
    box.padding = 1, max.overlaps = 10,
    position = position_dodge(width = 0.1)
  )
#> Bin width defaults to 1/30 of the range of the data. Pick better value with `binwidth`.

Created on 2021-09-30 by the reprex package (v0.3.0)

Session info
devtools::session_info()
#> ─ Session info ───────────────────────────────────────────────────────────────
#>  setting  value                       
#>  version  R version 4.0.3 (2020-10-10)
#>  os       macOS Catalina 10.15.7      
#>  system   x86_64, darwin17.0          
#>  ui       X11                         
#>  language (EN)                        
#>  collate  en_US.UTF-8                 
#>  ctype    en_US.UTF-8                 
#>  tz       America/New_York            
#>  date     2021-09-30                  
#> 
#> ─ Packages ───────────────────────────────────────────────────────────────────
#>  package     * version    date       lib source                             
#>  assertthat    0.2.1      2019-03-21 [2] CRAN (R 4.0.2)                     
#>  blob          1.2.1      2020-01-20 [2] CRAN (R 4.0.2)                     
#>  callr         3.5.1      2020-10-13 [2] CRAN (R 4.0.2)                     
#>  cli           2.3.1      2021-02-23 [1] CRAN (R 4.0.3)                     
#>  colorspace    2.0-0      2020-11-11 [2] CRAN (R 4.0.2)                     
#>  crayon        1.4.1      2021-02-08 [1] CRAN (R 4.0.2)                     
#>  curl          4.3        2019-12-02 [2] CRAN (R 4.0.1)                     
#>  DBI           1.1.0      2019-12-15 [2] CRAN (R 4.0.2)                     
#>  debugme       1.1.0      2017-10-22 [1] CRAN (R 4.0.2)                     
#>  desc          1.2.0      2018-05-01 [2] CRAN (R 4.0.2)                     
#>  devtools      2.3.0      2020-04-10 [2] CRAN (R 4.0.2)                     
#>  digest        0.6.27     2020-10-24 [2] CRAN (R 4.0.2)                     
#>  dplyr         1.0.4      2021-02-02 [1] CRAN (R 4.0.2)                     
#>  ellipsis      0.3.1      2020-05-15 [2] CRAN (R 4.0.2)                     
#>  evaluate      0.14       2019-05-28 [2] CRAN (R 4.0.1)                     
#>  fansi         0.4.2      2021-01-15 [1] CRAN (R 4.0.2)                     
#>  farver        2.0.3      2020-01-16 [2] CRAN (R 4.0.2)                     
#>  fs            1.5.0      2020-07-31 [1] CRAN (R 4.0.2)                     
#>  generics      0.1.0      2020-10-31 [2] CRAN (R 4.0.2)                     
#>  ggplot2     * 3.3.5      2021-06-25 [1] CRAN (R 4.0.2)                     
#>  ggrepel     * 0.9.1.9999 2021-01-22 [1] local                              
#>  glue          1.4.2      2020-08-27 [2] CRAN (R 4.0.2)                     
#>  gtable        0.3.0      2019-03-25 [2] CRAN (R 4.0.2)                     
#>  highr         0.8        2019-03-20 [2] CRAN (R 4.0.2)                     
#>  htmltools     0.5.1.1    2021-01-22 [1] CRAN (R 4.0.2)                     
#>  httr          1.4.2      2020-07-20 [2] CRAN (R 4.0.2)                     
#>  knitr         1.31       2021-01-27 [1] CRAN (R 4.0.2)                     
#>  labeling      0.4.2      2020-10-20 [2] CRAN (R 4.0.2)                     
#>  lifecycle     1.0.0      2021-02-15 [1] CRAN (R 4.0.2)                     
#>  magrittr      2.0.1.9000 2020-12-15 [1] Github (tidyverse/magrittr@bb1c86a)
#>  memoise       1.1.0.9000 2020-12-15 [1] Github (r-lib/memoise@0901e3f)     
#>  mime          0.10       2021-02-13 [1] CRAN (R 4.0.2)                     
#>  munsell       0.5.0      2018-06-12 [2] CRAN (R 4.0.2)                     
#>  pillar        1.5.0      2021-02-22 [1] CRAN (R 4.0.3)                     
#>  pkgbuild      1.1.0      2020-07-13 [2] CRAN (R 4.0.2)                     
#>  pkgconfig     2.0.3      2019-09-22 [2] CRAN (R 4.0.2)                     
#>  pkgload       1.1.0      2020-05-29 [2] CRAN (R 4.0.2)                     
#>  prettyunits   1.1.1      2020-01-24 [2] CRAN (R 4.0.2)                     
#>  processx      3.4.5      2020-11-30 [2] CRAN (R 4.0.2)                     
#>  ps            1.5.0      2020-12-05 [2] CRAN (R 4.0.2)                     
#>  purrr         0.3.4      2020-04-17 [2] CRAN (R 4.0.2)                     
#>  R6            2.5.0      2020-10-28 [2] CRAN (R 4.0.2)                     
#>  Rcpp          1.0.6      2021-01-15 [1] CRAN (R 4.0.2)                     
#>  remotes       2.2.0      2020-07-21 [1] CRAN (R 4.0.2)                     
#>  rlang         0.4.10     2020-12-30 [1] CRAN (R 4.0.2)                     
#>  rmarkdown     2.6        2020-12-14 [1] CRAN (R 4.0.2)                     
#>  rprojroot     2.0.2      2020-11-15 [2] CRAN (R 4.0.2)                     
#>  scales        1.1.1      2020-05-11 [2] CRAN (R 4.0.2)                     
#>  sessioninfo   1.1.1      2018-11-05 [2] CRAN (R 4.0.2)                     
#>  stringi       1.5.3      2020-09-09 [2] CRAN (R 4.0.2)                     
#>  stringr       1.4.0      2019-02-10 [2] CRAN (R 4.0.2)                     
#>  testthat      3.0.0      2020-10-31 [2] CRAN (R 4.0.2)                     
#>  tibble        3.0.6      2021-01-29 [1] CRAN (R 4.0.2)                     
#>  tidyselect    1.1.0      2020-05-11 [2] CRAN (R 4.0.2)                     
#>  usethis       1.6.1      2020-04-29 [2] CRAN (R 4.0.2)                     
#>  utf8          1.1.4      2018-05-24 [2] CRAN (R 4.0.2)                     
#>  vctrs         0.3.6      2020-12-17 [1] CRAN (R 4.0.2)                     
#>  withr         2.4.1      2021-01-26 [1] CRAN (R 4.0.2)                     
#>  xfun          0.21       2021-02-10 [1] CRAN (R 4.0.2)                     
#>  xml2          1.3.2      2020-04-23 [2] CRAN (R 4.0.2)                     
#>  yaml          2.2.1      2020-02-01 [2] CRAN (R 4.0.2)                     
#> 
#> [1] /Users/kamil/Library/R/4.0/library
#> [2] /Library/Frameworks/R.framework/Versions/4.0/Resources/library

slowkow avatar Sep 30 '21 12:09 slowkow