Skip to content

bizarre difference between locations of geom_text vs. geom_label?  #182

Closed

Description

@beanumber

Summary

geom_label_repel() is placing labels in strange locations that are very far from the (correct) geom_text_repel() placements. This behavior appears to be new in v0.9.0.

Minimal code example

set.seed(23)
library(tidyverse)
library(alr3)
#> Loading required package: car
#> Loading required package: carData
#> 
#> Attaching package: 'car'
#> The following object is masked from 'package:dplyr':
#> 
#>     recode
#> The following object is masked from 'package:purrr':
#> 
#>     some

challeng <- challeng %>%
  mutate(
    label = case_when(
      Damage == 11 ~ "SRM 15",
      Damage == 4 & Temp == 75 ~ "SRM 22",
      TRUE ~ ""
    )
  )

challenger_plot <- ggplot(data = challeng, aes(x = Temp, y = Damage)) + 
  geom_jitter(size = 5, height = 0.25, width = 0.25, alpha = 0.5) + 
  geom_smooth()

challenger_plot + ggrepel::geom_text_repel(aes(label = label))
#> `geom_smooth()` using method = 'loess' and formula 'y ~ x'

challenger_plot + ggrepel::geom_label_repel(aes(label = label))
#> `geom_smooth()` using method = 'loess' and formula 'y ~ x'

sessionInfo()
#> R version 4.0.3 (2020-10-10)
#> Platform: x86_64-pc-linux-gnu (64-bit)
#> Running under: Ubuntu 18.04.5 LTS
#> 
#> 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              
#>  [3] LC_TIME=en_US.UTF-8        LC_COLLATE=en_US.UTF-8    
#>  [5] LC_MONETARY=en_US.UTF-8    LC_MESSAGES=en_US.UTF-8   
#>  [7] LC_PAPER=en_US.UTF-8       LC_NAME=C                 
#>  [9] LC_ADDRESS=C               LC_TELEPHONE=C            
#> [11] LC_MEASUREMENT=en_US.UTF-8 LC_IDENTIFICATION=C       
#> 
#> attached base packages:
#> [1] stats     graphics  grDevices utils     datasets  methods   base     
#> 
#> other attached packages:
#>  [1] alr3_2.0.8      car_3.0-10      carData_3.0-4   forcats_0.5.0  
#>  [5] stringr_1.4.0   dplyr_1.0.2     purrr_0.3.4     readr_1.4.0    
#>  [9] tidyr_1.1.2     tibble_3.0.4    ggplot2_3.3.3   tidyverse_1.3.0
#> 
#> loaded via a namespace (and not attached):
#>  [1] ggrepel_0.9.0     Rcpp_1.0.5        lattice_0.20-41   lubridate_1.7.9.2
#>  [5] assertthat_0.2.1  digest_0.6.27     R6_2.5.0          cellranger_1.1.0 
#>  [9] backports_1.2.1   reprex_0.3.0      evaluate_0.14     httr_1.4.2       
#> [13] highr_0.8         pillar_1.4.7      rlang_0.4.10      curl_4.3         
#> [17] readxl_1.3.1      data.table_1.13.6 Matrix_1.3-0      rmarkdown_2.6    
#> [21] labeling_0.4.2    splines_4.0.3     foreign_0.8-81    munsell_0.5.0    
#> [25] broom_0.7.3       compiler_4.0.3    modelr_0.1.8      xfun_0.19        
#> [29] pkgconfig_2.0.3   mgcv_1.8-33       htmltools_0.5.0   tidyselect_1.1.0 
#> [33] rio_0.5.16        fansi_0.4.1       crayon_1.3.4      dbplyr_2.0.0     
#> [37] withr_2.3.0       grid_4.0.3        nlme_3.1-151      jsonlite_1.7.2   
#> [41] gtable_0.3.0      lifecycle_0.2.0   DBI_1.1.0         magrittr_2.0.1   
#> [45] scales_1.1.1      zip_2.1.1         cli_2.2.0         stringi_1.5.3    
#> [49] farver_2.0.3      fs_1.5.0          xml2_1.3.2        ellipsis_0.3.1   
#> [53] generics_0.1.0    vctrs_0.3.6       openxlsx_4.2.3    tools_4.0.3      
#> [57] glue_1.4.2        hms_0.5.3         abind_1.4-5       yaml_2.2.1       
#> [61] colorspace_2.0-0  rvest_0.3.6       knitr_1.30        haven_2.3.1

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

Activity

  1. beanumber commented on Jan 9, 2021

    @beanumber
    Author

    FYI, adding data = filter(challeng, label > "") fixes the problem.

    So I solved my immediate problem, but it still seems weird that the two functions behave differently. I'm leaving this open in case you want to pursue it further.

  2. slowkow commented on Jan 9, 2021

    @slowkow
    Owner

    Thanks for reporting the issue! I appreciate it.

    I can't run your code, because I don't have your data.

    Please consider sharing an example that I can run on my own computer.

    Here's what I see:

    library(ggrepel)
    #> Loading required package: ggplot2
    
    cars <- mtcars
    cars$car <- rownames(cars)
    
    cars$car[1:(nrow(cars)-2)] <- NA
    
    p <- ggplot(cars, aes(x = wt, y = mpg)) +
      geom_jitter(aes(color = is.na(car))) +
      geom_smooth()
    
    p + geom_text_repel(aes(label = car))
    #> `geom_smooth()` using method = 'loess' and formula 'y ~ x'
    #> Warning: Removed 30 rows containing missing values (geom_text_repel).

    p + geom_label_repel(aes(label = car))
    #> `geom_smooth()` using method = 'loess' and formula 'y ~ x'
    #> Warning: Removed 30 rows containing missing values (geom_label_repel).

    Created on 2021-01-09 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-01-09                  
    #> 
    #> ─ Packages ───────────────────────────────────────────────────────────────────
    #>  package     * version    date       lib source                             
    #>  assertthat    0.2.1      2019-03-21 [2] CRAN (R 4.0.2)                     
    #>  callr         3.5.1      2020-10-13 [2] CRAN (R 4.0.2)                     
    #>  cli           2.2.0      2020-11-20 [2] CRAN (R 4.0.2)                     
    #>  colorspace    2.0-0      2020-11-11 [2] CRAN (R 4.0.2)                     
    #>  crayon        1.3.4      2017-09-16 [2] CRAN (R 4.0.2)                     
    #>  curl          4.3        2019-12-02 [2] CRAN (R 4.0.1)                     
    #>  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.2      2020-08-18 [2] 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.1      2020-01-08 [2] 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.2.9000 2020-12-08 [2] Github (tidyverse/ggplot2@b5cc4d6) 
    #>  ggrepel     * 0.9.0.9999 2021-01-04 [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.0      2020-06-16 [2] CRAN (R 4.0.2)                     
    #>  httr          1.4.2      2020-07-20 [2] CRAN (R 4.0.2)                     
    #>  knitr         1.30       2020-09-22 [1] CRAN (R 4.0.2)                     
    #>  labeling      0.4.2      2020-10-20 [2] CRAN (R 4.0.2)                     
    #>  lattice       0.20-41    2020-04-02 [2] CRAN (R 4.0.3)                     
    #>  lifecycle     0.2.0      2020-03-06 [2] CRAN (R 4.0.2)                     
    #>  magrittr      2.0.1.9000 2020-12-15 [1] Github (tidyverse/magrittr@bb1c86a)
    #>  Matrix        1.2-18     2019-11-27 [2] CRAN (R 4.0.3)                     
    #>  memoise       1.1.0.9000 2020-12-15 [1] Github (r-lib/memoise@0901e3f)     
    #>  mgcv          1.8-33     2020-08-27 [2] CRAN (R 4.0.3)                     
    #>  mime          0.9        2020-02-04 [2] CRAN (R 4.0.2)                     
    #>  munsell       0.5.0      2018-06-12 [2] CRAN (R 4.0.2)                     
    #>  nlme          3.1-149    2020-08-23 [2] CRAN (R 4.0.3)                     
    #>  pillar        1.4.7      2020-11-20 [2] CRAN (R 4.0.2)                     
    #>  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.5.4    2020-12-08 [2] Github (RcppCore/Rcpp@a72a27c)     
    #>  remotes       2.2.0      2020-07-21 [1] CRAN (R 4.0.2)                     
    #>  rlang         0.4.9      2020-11-26 [2] CRAN (R 4.0.2)                     
    #>  rmarkdown     2.3.2      2020-07-17 [2] Github (rstudio/rmarkdown@ff1b279) 
    #>  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.4      2020-10-12 [2] 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)                     
    #>  vctrs         0.3.5      2020-11-17 [2] CRAN (R 4.0.2)                     
    #>  withr         2.3.0      2020-09-22 [2] CRAN (R 4.0.2)                     
    #>  xfun          0.19       2020-10-30 [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
  3. slowkow commented on Jan 9, 2021

    @slowkow
    Owner
    > install.packages("alr3")
    Installing package into ‘/Users/kamil/Library/R/4.0/library’
    (as ‘lib’ is unspecified)
    Warning in install.packages :
      package ‘alr3’ is not available for this version of R
    
    A version of this package for your version of R might be available elsewhere,
    see the ideas at
    https://cran.r-project.org/doc/manuals/r-patched/R-admin.html#Installing-packages
    
    
  4. slowkow commented on Jan 9, 2021

    @slowkow
    Owner

    Hm... I think I might be seeing the same strange behavior you reported. It's difficult to make a reproducible example, though!

  5. slowkow commented on Jan 9, 2021

    @slowkow
    Owner

    I just pushed 8adf073

    While I can't make an example that looks similar to yours, I did notice that this commit seemed to change the way geom_label_repel() positions the labels. Maybe this fixed it? I don't know.

    Could I please ask you to try running your example again with this the GitHub version of ggrepel?

    remotes::install_github("slowkow/ggrepel@8adf073")
    
    # run your example again ...

    Thanks again for your report! I don't usually use geom_label_repel(), and I rely on the examples to make sure that the code is working, but I guess I probably need more tests.

  6. beanumber commented on Jan 9, 2021

    @beanumber
    Author
  7. beanumber commented on Jan 9, 2021

    @beanumber
    Author

    Thanks for the quick response!

    I still see the problem persisting in the patch you mentioned.

    I can also confirm that the behavior does NOT occur in v0.8.2.

    It looks like alr3 was pulled from CRAN, but you can get an archived version at the link above if you want to verify the bug.

  8. slowkow commented on Jan 9, 2021

    @slowkow
    Owner

    I was able to reproduce your example after installing an old version of alr3.

    Thanks again. I'll let you know when I figure out what is going on...

  9. slowkow commented on Jan 9, 2021

    @slowkow
    Owner

    @beanumber I found a bug and fixed it! Thank you so much. That was an important bug, so I plan to push a fix to CRAN soon.

  10. beanumber commented on Jan 9, 2021

    @beanumber
    Author

    Glad to be of service. Thanks for everything you've put into this!

  11. added a commit that references this issue on Feb 20, 2024
    6578994
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Projects

    No projects

      Milestone

      No milestone

      Relationships

      None yet

      Development

      No branches or pull requests

      Issue actions