ggrepel icon indicating copy to clipboard operation
ggrepel copied to clipboard

Segment not reaching the label box when using hjust

Open DasHammett opened this issue 4 years ago • 3 comments

Summary

When using hjust option to justify text, segment line does not reach the label box. Issue is not always reproducible, depends on the size of the image and the position of the label boxes.

Issue is very apparent when using labels with maps (geom_sf())

Minimal code example

# Set graph output dimension for reproducibility purposes
dev.new(width = 6, height = 12, unit = "in")

ggplot() +
  geom_label_repel(
    aes(label = "testing for segment\nelongation",
        x = c(1, 1.1),
        y = c(1, 1.1)
    ),
    min.segment.length = 0,
    hjust = 1
  ) +
  xlim(c(1,100)) +
  ylim(c(1,100))

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

image

As you can see from the screenshot, the lower box segment is not reaching the box. Removing hjust option renders the segment correctly

# Set graph output dimension for reproducibility purposes
dev.new(width = 6, height = 12, unit = "in")

ggplot() +
  geom_label_repel(
    aes(label = "testing for segment\nelongation",
        x = c(1, 1.1),
        y = c(1, 1.1)
    ),
    min.segment.length = 0
  ) +
  xlim(c(1,100)) +
  ylim(c(1,100))

image

Version information

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

> sessionInfo()
R version 4.0.2 (2020-06-22)
Platform: x86_64-w64-mingw32/x64 (64-bit)
Running under: Windows 10 x64 (build 18362)

Matrix products: default

locale:
[1] LC_COLLATE=Spanish_Spain.1252      LC_CTYPE=Spanish_Spain.1252        LC_MONETARY=Spanish_Spain.1252    
[4] LC_NUMERIC=C                       LC_TIME=English_United States.1252

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

other attached packages:
[1] lubridate_1.7.9 ggrepel_0.9.0   sf_0.9-5        dplyr_1.0.0     ggplot2_3.3.2  

loaded via a namespace (and not attached):
 [1] Rcpp_1.0.5         compiler_4.0.2     pillar_1.4.6       class_7.3-17       prettyunits_1.1.1 
 [6] remotes_2.2.0      tools_4.0.2        testthat_2.3.2     digest_0.6.25      pkgbuild_1.1.0    
[11] pkgload_1.1.0      memoise_1.1.0      lifecycle_0.2.0    tibble_3.0.3       gtable_0.3.0      
[16] pkgconfig_2.0.3    rlang_0.4.7        DBI_1.1.0          cli_2.0.2          rstudioapi_0.11   
[21] curl_4.3           e1071_1.7-3        withr_2.2.0        generics_0.0.2     vctrs_0.3.2       
[26] desc_1.2.0         fs_1.5.0           devtools_2.3.1     classInt_0.4-3     tidyselect_1.1.0  
[31] rprojroot_1.3-2    grid_4.0.2         glue_1.4.1         R6_2.4.1           processx_3.4.3    
[36] fansi_0.4.1        sessioninfo_1.1.1  farver_2.0.3       purrr_0.3.4        callr_3.4.3       
[41] magrittr_1.5       units_0.6-7        backports_1.1.8    scales_1.1.1       ps_1.3.4          
[46] ellipsis_0.3.1     usethis_1.6.1      assertthat_0.2.1   colorspace_1.4-1   labeling_0.3      
[51] KernSmooth_2.23-17 munsell_0.5.0      crayon_1.3.4 

DasHammett avatar Aug 20 '20 19:08 DasHammett

Weird! Thank you for reporting this.

I never noticed this before, and I think it is a bug.

slowkow avatar Aug 20 '20 20:08 slowkow

What looks like a similar problem affecting justification can be seen in some of the examples of the new position nudge functions.

aphalo avatar Jan 24 '21 15:01 aphalo

@slowkow This problem seems to be related to an interaction between justification and box padding. It still exists in the code in pull request #196 as of commit 76d8ff4ad889ddba8950321023cbee198601809c

library(ggplot2)
library(ggrepel)
# Set graph output dimension for reproducibility purposes
#dev.new(width = 6, height = 12, unit = "in")

ggplot() +
  geom_label_repel(
    aes(x = c(1, 1.1),
        y = c(1, 1.1)
    ),
    label = "testing for segment\nelongation",
    fill = NA,
    min.segment.length = 0,
    hjust = 1
  ) +
  xlim(c(1,100)) +
  ylim(c(1,100))

ggplot() +
  geom_label_repel(
    aes(x = c(1, 1.1),
        y = c(1, 1.1)
    ),
    label = "testing for segment\nelongation",
    fill = NA,
    min.segment.length = 0,
    hjust = 1,
    box.padding = 1
  ) +
  xlim(c(1,100)) +
  ylim(c(1,100))

ggplot() +
  geom_label_repel(
    aes(x = c(1, 1.1),
        y = c(1, 1.1)
    ),
    label = "testing for segment\nelongation",
    fill = NA,
    min.segment.length = 0,
    hjust = 1,
    box.padding = 0
  ) +
  xlim(c(1,100)) +
  ylim(c(1,100))

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

Session info
sessioninfo::session_info()
#> - Session info  --------------------------------------------------------------
#>  hash: sad but relieved face, man: medium-dark skin tone, person facepalming: medium-light 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-10
#>  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-10 [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 avatar Nov 10 '21 19:11 aphalo