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

Bad repulsion and position of segments in geom_text_repel() with rotation #211

Closed
aphalo opened this issue Nov 11, 2021 · 3 comments
Closed

Comments

@aphalo
Copy link
Contributor

aphalo commented Nov 11, 2021

Summary

Pull request #196 solves problems with justification and angle of rotation for the initial position, as seen passing to geom_text_repel() max.iter = 0. With the default for max.iter and angles near or equal to 45, 135, 225, or 315 degrees, in some cases repulsion fails to shift the labels and the segment sometimes is attached to the wrong side of the text. It also looks like padding is not being applied. I hope not to have broken this in #196.

Minimal code example

library(ggplot2)
library(ggrepel)

my.cars <- mtcars[c(TRUE, FALSE, FALSE, FALSE), ]

p <- ggplot(my.cars, aes(wt, mpg, label = rownames(my.cars))) +
  geom_point(colour = "red") + 
  expand_limits(x = c(1, 7), y = c(12, 24))

p + geom_text(angle = 45, hjust = 1)

p + geom_text_repel(min.segment.length = 0, 
                    box.padding = 0.5, 
                    point.padding = 0.5,
                    angle = 45, 
                    hjust = 1)

p + geom_text_repel(min.segment.length = 0, 
                    box.padding = 0.5, 
                    point.padding = 0.5,
                    angle = 235, 
                    hjust = 1)

p + geom_text_repel(min.segment.length = 0, box.padding = 0.5, angle = 45, 
                    hjust = "outward", vjust = "outward")

p + geom_text_repel(min.segment.length = 0, box.padding = 0.5, angle = 45, 
                    hjust = "inward", vjust = "inward")

p + geom_text_repel(min.segment.length = 0, box.padding = 0.5, angle = -45)

p + geom_text_repel(min.segment.length = 0, box.padding = 0.5, angle = -45, 
                    hjust = "outward", vjust = "outward")

p + geom_text_repel(min.segment.length = 0, box.padding = 0.5, angle = -45, 
                    hjust = "inward", vjust = "inward")

Created on 2021-11-11 by the reprex package (v2.0.1)

Session info
sessioninfo::session_info()
#> - Session info  --------------------------------------------------------------
#>  hash: OK hand, first quarter moon face, person frowning: dark skin tone
#> 
#>  setting  value
#>  version  R version 4.1.1 (2021-08-10)
#>  os       Windows 10 x64 (build 19043)
#>  system   x86_64, mingw32
#>  ui       RTerm
#>  language (EN)
#>  collate  English_Finland.1252
#>  ctype    English_Finland.1252
#>  tz       Europe/Helsinki
#>  date     2021-11-11
#>  pandoc   2.14.0.3 @ C:/Program Files/RStudio/bin/pandoc/ (via rmarkdown)
#> 
#> - Packages -------------------------------------------------------------------
#>  package     * version    date (UTC) lib source
#>  assertthat    0.2.1      2019-03-21 [1] CRAN (R 4.1.0)
#>  backports     1.3.0      2021-10-27 [1] CRAN (R 4.1.1)
#>  cli           3.1.0      2021-10-27 [1] CRAN (R 4.1.1)
#>  colorspace    2.0-2      2021-06-24 [1] CRAN (R 4.1.0)
#>  crayon        1.4.2      2021-10-29 [1] CRAN (R 4.1.1)
#>  curl          4.3.2      2021-06-23 [1] CRAN (R 4.1.0)
#>  DBI           1.1.1      2021-01-15 [1] CRAN (R 4.1.0)
#>  digest        0.6.28     2021-09-23 [1] CRAN (R 4.1.1)
#>  dplyr         1.0.7      2021-06-18 [1] CRAN (R 4.1.0)
#>  ellipsis      0.3.2      2021-04-29 [1] CRAN (R 4.1.0)
#>  evaluate      0.14       2019-05-28 [1] CRAN (R 4.1.0)
#>  fansi         0.5.0      2021-05-25 [1] CRAN (R 4.1.0)
#>  farver        2.1.0      2021-02-28 [1] CRAN (R 4.1.0)
#>  fastmap       1.1.0      2021-01-25 [1] CRAN (R 4.1.0)
#>  fs            1.5.0      2020-07-31 [1] CRAN (R 4.1.0)
#>  generics      0.1.1      2021-10-25 [1] CRAN (R 4.1.1)
#>  ggplot2     * 3.3.5      2021-06-25 [1] CRAN (R 4.1.0)
#>  ggrepel     * 0.9.1.9999 2021-11-11 [1] local
#>  glue          1.4.2      2020-08-27 [1] CRAN (R 4.1.1)
#>  gtable        0.3.0      2019-03-25 [1] CRAN (R 4.1.0)
#>  highr         0.9        2021-04-16 [1] CRAN (R 4.1.0)
#>  htmltools     0.5.2      2021-08-25 [1] CRAN (R 4.1.1)
#>  httr          1.4.2      2020-07-20 [1] CRAN (R 4.1.0)
#>  knitr         1.36       2021-09-29 [1] CRAN (R 4.1.1)
#>  labeling      0.4.2      2020-10-20 [1] CRAN (R 4.1.0)
#>  lifecycle     1.0.1      2021-09-24 [1] CRAN (R 4.1.1)
#>  magrittr      2.0.1      2020-11-17 [1] CRAN (R 4.1.0)
#>  mime          0.12       2021-09-28 [1] CRAN (R 4.1.1)
#>  munsell       0.5.0      2018-06-12 [1] CRAN (R 4.1.0)
#>  pillar        1.6.4      2021-10-18 [1] CRAN (R 4.1.1)
#>  pkgconfig     2.0.3      2019-09-22 [1] CRAN (R 4.1.0)
#>  purrr         0.3.4      2020-04-17 [1] CRAN (R 4.1.0)
#>  R.cache       0.15.0     2021-04-30 [1] CRAN (R 4.1.0)
#>  R.methodsS3   1.8.1      2020-08-26 [1] CRAN (R 4.1.0)
#>  R.oo          1.24.0     2020-08-26 [1] CRAN (R 4.1.0)
#>  R.utils       2.11.0     2021-09-26 [1] CRAN (R 4.1.1)
#>  R6            2.5.1      2021-08-19 [1] CRAN (R 4.1.0)
#>  Rcpp          1.0.7      2021-07-07 [1] CRAN (R 4.1.0)
#>  reprex        2.0.1      2021-08-05 [1] CRAN (R 4.1.0)
#>  rlang         0.4.12     2021-10-18 [1] CRAN (R 4.1.1)
#>  rmarkdown     2.11       2021-09-14 [1] CRAN (R 4.1.1)
#>  rstudioapi    0.13       2020-11-12 [1] CRAN (R 4.1.0)
#>  scales        1.1.1      2020-05-11 [1] CRAN (R 4.1.0)
#>  sessioninfo   1.2.1      2021-11-02 [1] CRAN (R 4.1.1)
#>  stringi       1.7.5      2021-10-04 [1] CRAN (R 4.1.1)
#>  stringr       1.4.0      2019-02-10 [1] CRAN (R 4.1.0)
#>  styler        1.6.2      2021-09-23 [1] CRAN (R 4.1.1)
#>  tibble        3.1.5      2021-09-30 [1] CRAN (R 4.1.1)
#>  tidyselect    1.1.1      2021-04-30 [1] CRAN (R 4.1.0)
#>  utf8          1.2.2      2021-07-24 [1] CRAN (R 4.1.0)
#>  vctrs         0.3.8      2021-04-29 [1] CRAN (R 4.1.0)
#>  withr         2.4.2      2021-04-18 [1] CRAN (R 4.1.0)
#>  xfun          0.27       2021-10-18 [1] CRAN (R 4.1.1)
#>  xml2          1.3.2      2020-04-23 [1] CRAN (R 4.1.0)
#>  yaml          2.2.1      2020-02-01 [1] CRAN (R 4.1.0)
#> 
#>  [1] C:/Users/Aphalo/Documents/R/win-library/4.1
#>  [2] C:/Program Files/R/R-4.1.1/library
#> 
#> ------------------------------------------------------------------------------
@aphalo
Copy link
Contributor Author

aphalo commented Nov 12, 2021

@slowkow Looking at select_line_connection() the algorithm seems to assume that the text box has its edges vertical and horizontal. This could explain segments being attached to the wrong edge with rotation that is not close to a multiple of 90 degrees. To be able to get the other new features in a release to CRAN, it might be best to simply issue a warning (or error) when the value passed to angleis not a multiple of 90 degrees.

In the long run, as 'grid' (>= 4.1.0) supports the use of grobs as masks, drawing segments always from the point to the centroid of the text box and then limiting the visible part with a mask could be a good solution.

@slowkow
Copy link
Owner

slowkow commented Nov 12, 2021

@aphalo Agree! A warning is sufficient for now — could I please ask if you could update your pull request to include the warning? I feel like these issues are related.

I think the behavior we had before select_line_connection() might have been ok for rotated labels ... but I have not tested this. If it was ok, then maybe an if-statement can check for multiples of 90 and switch between the two.

@aphalo
Copy link
Contributor Author

aphalo commented Nov 12, 2021

@slowkow I included the warning in #196 in commit 275b4a7 some hours ago (without waiting for your answer). I also added some unit tests.

@slowkow slowkow closed this as completed Jan 5, 2024
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