slowkow / ggrepel

:round_pushpin: Repel overlapping text labels away from each other in your ggplot2 figures.
https://ggrepel.slowkow.com
GNU General Public License v3.0
1.21k stars 95 forks source link

Left justifying multi-line labels #137

Closed johnhenrypezzuto closed 4 years ago

johnhenrypezzuto commented 5 years ago

Summary

How do I left justify my multi-line label? In the example below, I would like the word "very" to be underneath the word "this". Here I use the left justify setting i.e., hjust = 0.

Posted from stackoverflow - https://stackoverflow.com/questions/56380460/how-do-i-left-justify-when-using-line-breaks-using-ggrepel?noredirect=1#comment99362278_56380460

Minimal code example

library(tidyverse)
library(ggrepel)

mtcars %>% 
  ggplot(aes(hp, drat, label = "this label is so long \n very long \n very long")) +
  geom_line() +
  geom_text_repel(data = mtcars %>% 
                    filter(hp == max(hp)),
                  nudge_x = 0.4,
                  segment.color = 'transparent',
                  size = 3,
                  force =0.2,
                  hjust = 0)

image

Suggestions

I think hjust should left justify multiple lines. Not sure how to implement.

Version information

R version 3.5.3 (2019-03-11)
Platform: x86_64-apple-darwin15.6.0 (64-bit)
Running under: macOS Mojave 10.14.5

Matrix products: default
BLAS: /System/Library/Frameworks/Accelerate.framework/Versions/A/Frameworks/vecLib.framework/Versions/A/libBLAS.dylib
LAPACK: /Library/Frameworks/R.framework/Versions/3.5/Resources/lib/libRlapack.dylib

locale:
[1] en_US.UTF-8/en_US.UTF-8/en_US.UTF-8/C/en_US.UTF-8/en_US.UTF-8

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

other attached packages:
 [1] ggrepel_0.8.0         forcats_0.3.0         stringr_1.4.0         dplyr_0.8.0.1         purrr_0.3.2           readr_1.1.1           tidyr_0.8.3          
 [8] tibble_2.1.2          ggplot2_3.1.1.9000    tidyverse_1.2.1       scales_1.0.0          gridExtra_2.3         kableExtra_1.1.0.9000 knitr_1.22           
[15] magrittr_1.5          xtable_1.8-3          multcomp_1.4-8        TH.data_1.0-9         MASS_7.3-51.1         survival_2.43-3       mvtnorm_1.0-10       

loaded via a namespace (and not attached):
 [1] nlme_3.1-137      fs_1.2.6          bitops_1.0-6      usethis_1.4.0     lubridate_1.7.4   devtools_2.0.1    webshot_0.5.1     httr_1.4.0       
 [9] rprojroot_1.3-2   tools_3.5.3       backports_1.1.4   R6_2.4.0          lazyeval_0.2.2    colorspace_1.4-1  withr_2.1.2       tidyselect_0.2.5 
[17] prettyunits_1.0.2 processx_3.2.0    curl_3.3          compiler_3.5.3    cli_1.1.0         rvest_0.3.2.9000  xml2_1.2.0        desc_1.2.0       
[25] sandwich_2.5-0    labeling_0.3      caTools_1.17.1.2  ggridges_0.5.1    callr_3.0.0       askpass_1.1       digest_0.6.18     rmarkdown_1.12   
[33] wdman_0.2.4       base64enc_0.1-3   pkgconfig_2.0.2   htmltools_0.3.6   sessioninfo_1.1.1 rlang_0.3.4       ggthemes_4.0.1    readxl_1.1.0     
[41] rstudioapi_0.9.0  generics_0.0.2    zoo_1.8-4         jsonlite_1.6      Matrix_1.2-15     Rcpp_1.0.1        munsell_0.5.0     stringi_1.4.3    
[49] yaml_2.2.0        pkgbuild_1.0.2    plyr_1.8.4        grid_3.5.3        crayon_1.3.4      semver_0.2.0      lattice_0.20-38   haven_1.1.2      
[57] cowplot_0.9.4     splines_3.5.3     hms_0.4.2         ps_1.2.1          pillar_1.4.1      codetools_0.2-16  pkgload_1.0.2     XML_3.98-1.16    
[65] glue_1.3.1        evaluate_0.13     remotes_2.0.2     modelr_0.1.2      testthat_2.0.1    cellranger_1.1.0  gtable_0.2.0      openssl_1.2.2    
[73] assertthat_0.2.1  xfun_0.5          binman_0.1.1      broom_0.5.1       viridisLite_0.3.0 RSelenium_1.7.4   memoise_1.1.0    
> 
slowkow commented 5 years ago

Thanks for reporting this issue. I think this might be a bug, and a duplicate of #134

Here's my attempt at a minimal code example:

library(ggrepel)
#> Loading required package: ggplot2
#> Registered S3 methods overwritten by 'ggplot2':
#>   method         from 
#>   [.quosures     rlang
#>   c.quosures     rlang
#>   print.quosures rlang

d <- data.frame(x = 1, y = 1, label = "this\nis a\nlong label")

ggplot(d, aes(x, y, label = label)) +
  geom_text(hjust = 0)


ggplot(d, aes(x, y, label = label)) +
  geom_text_repel(hjust = 0)

Created on 2019-06-07 by the reprex package (v0.2.1)

Session info ``` r devtools::session_info() #> ─ Session info ────────────────────────────────────────────────────────── #> setting value #> version R version 3.6.0 (2019-04-26) #> os macOS High Sierra 10.13.6 #> system x86_64, darwin15.6.0 #> ui X11 #> language (EN) #> collate en_US.UTF-8 #> ctype en_US.UTF-8 #> tz America/New_York #> date 2019-06-07 #> #> ─ Packages ────────────────────────────────────────────────────────────── #> package * version date lib source #> assertthat 0.2.1 2019-03-21 [1] CRAN (R 3.6.0) #> backports 1.1.4 2019-04-10 [1] CRAN (R 3.6.0) #> callr 3.2.0 2019-03-15 [1] CRAN (R 3.6.0) #> cli 1.1.0 2019-03-19 [1] CRAN (R 3.6.0) #> colorspace 1.4-1 2019-03-18 [1] CRAN (R 3.6.0) #> crayon 1.3.4 2017-09-16 [1] CRAN (R 3.6.0) #> curl 3.3 2019-01-10 [1] CRAN (R 3.6.0) #> desc 1.2.0 2018-05-01 [1] CRAN (R 3.6.0) #> devtools 2.0.2 2019-04-08 [1] CRAN (R 3.6.0) #> digest 0.6.18 2018-10-10 [1] CRAN (R 3.6.0) #> dplyr 0.8.0.1 2019-02-15 [1] CRAN (R 3.6.0) #> evaluate 0.13 2019-02-12 [1] CRAN (R 3.6.0) #> fs 1.3.1 2019-05-06 [1] CRAN (R 3.6.0) #> ggplot2 * 3.1.1 2019-04-07 [1] CRAN (R 3.6.0) #> ggrepel * 0.8.1 2019-05-07 [1] CRAN (R 3.6.0) #> glue 1.3.1 2019-03-12 [1] CRAN (R 3.6.0) #> gtable 0.3.0 2019-03-25 [1] CRAN (R 3.6.0) #> highr 0.8 2019-03-20 [1] CRAN (R 3.6.0) #> htmltools 0.3.6 2017-04-28 [1] CRAN (R 3.6.0) #> httr 1.4.0 2018-12-11 [1] CRAN (R 3.6.0) #> knitr 1.22 2019-03-08 [1] CRAN (R 3.6.0) #> labeling 0.3 2014-08-23 [1] CRAN (R 3.6.0) #> lazyeval 0.2.2 2019-03-15 [1] CRAN (R 3.6.0) #> magrittr 1.5 2014-11-22 [1] CRAN (R 3.6.0) #> memoise 1.1.0 2017-04-21 [1] CRAN (R 3.6.0) #> mime 0.6 2018-10-05 [1] CRAN (R 3.6.0) #> munsell 0.5.0 2018-06-12 [1] CRAN (R 3.6.0) #> pillar 1.3.1 2018-12-15 [1] CRAN (R 3.6.0) #> pkgbuild 1.0.3 2019-03-20 [1] CRAN (R 3.6.0) #> pkgconfig 2.0.2 2018-08-16 [1] CRAN (R 3.6.0) #> pkgload 1.0.2 2018-10-29 [1] CRAN (R 3.6.0) #> plyr 1.8.4 2016-06-08 [1] CRAN (R 3.6.0) #> prettyunits 1.0.2 2015-07-13 [1] CRAN (R 3.6.0) #> processx 3.3.1 2019-05-08 [1] CRAN (R 3.6.0) #> ps 1.3.0 2018-12-21 [1] CRAN (R 3.6.0) #> purrr 0.3.2 2019-03-15 [1] CRAN (R 3.6.0) #> R6 2.4.0 2019-02-14 [1] CRAN (R 3.6.0) #> Rcpp 1.0.1 2019-03-17 [1] CRAN (R 3.6.0) #> remotes 2.0.4 2019-04-10 [1] CRAN (R 3.6.0) #> rlang 0.3.4 2019-04-07 [1] CRAN (R 3.6.0) #> rmarkdown 1.12 2019-03-14 [1] CRAN (R 3.6.0) #> rprojroot 1.3-2 2018-01-03 [1] CRAN (R 3.6.0) #> scales 1.0.0 2018-08-09 [1] CRAN (R 3.6.0) #> sessioninfo 1.1.1 2018-11-05 [1] CRAN (R 3.6.0) #> stringi 1.4.3 2019-03-12 [1] CRAN (R 3.6.0) #> stringr 1.4.0 2019-02-10 [1] CRAN (R 3.6.0) #> testthat 2.1.1 2019-04-23 [1] CRAN (R 3.6.0) #> tibble 2.1.1 2019-03-16 [1] CRAN (R 3.6.0) #> tidyselect 0.2.5 2018-10-11 [1] CRAN (R 3.6.0) #> usethis 1.5.0 2019-04-07 [1] CRAN (R 3.6.0) #> withr 2.1.2 2018-03-15 [1] CRAN (R 3.6.0) #> xfun 0.6 2019-04-02 [1] CRAN (R 3.6.0) #> xml2 1.2.0 2018-01-24 [1] CRAN (R 3.6.0) #> yaml 2.2.0 2018-07-25 [1] CRAN (R 3.6.0) #> #> [1] /Library/Frameworks/R.framework/Versions/3.6/Resources/library ```
slowkow commented 4 years ago

I haven’t been able to find a solution for this issue without breaking the following example.

I would be happy to review pull requests to get this working. Maybe we can try to get this feature included in a future release of ggrepel.

library(ggrepel)
#> Loading required package: ggplot2

set.seed(42)
p <- ggplot(mtcars, aes(y = wt, x = 1, label = rownames(mtcars))) +
  geom_point(color = "red") +
  ylim(1, 5.5) +
  theme(
    axis.line.x  = element_blank(),
    axis.ticks.x = element_blank(),
    axis.text.x  = element_blank(),
    axis.title.x = element_blank()
  )

p +
  xlim(1, 1.375) +
  geom_text_repel(
    force        = 0.5,
    nudge_x      = 0.15,
    direction    = "y",
    hjust        = 0,
    segment.size = 0.2
  ) +
  ggtitle("hjust = 0")

Created on 2019-11-03 by the reprex package (v0.3.0)

Session info ``` r devtools::session_info() #> ─ Session info ────────────────────────────────────────────────────────── #> setting value #> version R version 3.6.1 (2019-07-05) #> os macOS Mojave 10.14.6 #> system x86_64, darwin15.6.0 #> ui X11 #> language (EN) #> collate en_US.UTF-8 #> ctype en_US.UTF-8 #> tz America/New_York #> date 2019-11-03 #> #> ─ Packages ────────────────────────────────────────────────────────────── #> package * version date lib #> assertthat 0.2.1 2019-03-21 [1] #> backports 1.1.4 2019-04-10 [1] #> callr 3.3.2 2019-09-22 [1] #> cli 1.1.0 2019-03-19 [1] #> colorspace 1.4-1 2019-03-18 [1] #> crayon 1.3.4 2017-09-16 [1] #> curl 4.1 2019-09-16 [1] #> desc 1.2.0 2018-05-01 [1] #> devtools 2.2.0 2019-09-07 [1] #> digest 0.6.21 2019-09-20 [1] #> dplyr 0.8.3 2019-07-04 [1] #> DT 0.9 2019-09-17 [1] #> ellipsis 0.3.0 2019-09-20 [1] #> evaluate 0.14 2019-05-28 [1] #> fs 1.3.1 2019-05-06 [1] #> ggplot2 * 3.2.1.9000 2019-09-27 [1] #> ggrepel * 0.9.0 2019-11-04 [1] #> glue 1.3.1 2019-03-12 [1] #> gtable 0.3.0 2019-03-25 [1] #> highr 0.8 2019-03-20 [1] #> htmltools 0.3.6 2017-04-28 [1] #> htmlwidgets 1.3 2018-09-30 [1] #> httr 1.4.1 2019-08-05 [1] #> knitr 1.25 2019-09-18 [1] #> labeling 0.3 2014-08-23 [1] #> magrittr 1.5 2014-11-22 [1] #> memoise 1.1.0 2017-04-21 [1] #> mime 0.7 2019-06-11 [1] #> munsell 0.5.0 2018-06-12 [1] #> pillar 1.4.2 2019-06-29 [1] #> pkgbuild 1.0.5 2019-08-26 [1] #> pkgconfig 2.0.3 2019-09-22 [1] #> pkgload 1.0.2 2018-10-29 [1] #> prettyunits 1.0.2 2015-07-13 [1] #> processx 3.4.1 2019-07-18 [1] #> ps 1.3.0 2018-12-21 [1] #> purrr 0.3.2 2019-03-15 [1] #> R6 2.4.0 2019-02-14 [1] #> Rcpp 1.0.2 2019-07-25 [1] #> remotes 2.1.0 2019-06-24 [1] #> rlang 0.4.0 2019-06-25 [1] #> rmarkdown 1.15 2019-08-21 [1] #> rprojroot 1.3-2 2018-01-03 [1] #> scales 1.0.0 2018-08-09 [1] #> sessioninfo 1.1.1 2018-11-05 [1] #> stringi 1.4.3 2019-03-12 [1] #> stringr 1.4.0 2019-02-10 [1] #> testthat 2.2.1 2019-07-25 [1] #> tibble 2.1.3 2019-06-06 [1] #> tidyselect 0.2.5 2018-10-11 [1] #> usethis 1.5.1 2019-07-04 [1] #> withr 2.1.2 2018-03-15 [1] #> xfun 0.9 2019-08-21 [1] #> xml2 1.2.2 2019-08-09 [1] #> yaml 2.2.0 2018-07-25 [1] #> source #> CRAN (R 3.6.0) #> CRAN (R 3.6.0) #> CRAN (R 3.6.1) #> CRAN (R 3.6.0) #> CRAN (R 3.6.0) #> CRAN (R 3.6.0) #> CRAN (R 3.6.0) #> CRAN (R 3.6.0) #> CRAN (R 3.6.0) #> CRAN (R 3.6.0) #> CRAN (R 3.6.0) #> CRAN (R 3.6.0) #> CRAN (R 3.6.0) #> CRAN (R 3.6.0) #> CRAN (R 3.6.0) #> Github (tidyverse/ggplot2@23e3241) #> local #> CRAN (R 3.6.0) #> CRAN (R 3.6.0) #> CRAN (R 3.6.0) #> CRAN (R 3.6.0) #> CRAN (R 3.6.0) #> CRAN (R 3.6.0) #> CRAN (R 3.6.0) #> CRAN (R 3.6.0) #> CRAN (R 3.6.0) #> CRAN (R 3.6.0) #> CRAN (R 3.6.0) #> CRAN (R 3.6.0) #> CRAN (R 3.6.0) #> CRAN (R 3.6.0) #> CRAN (R 3.6.1) #> CRAN (R 3.6.0) #> CRAN (R 3.6.0) #> CRAN (R 3.6.0) #> CRAN (R 3.6.0) #> CRAN (R 3.6.0) #> CRAN (R 3.6.0) #> CRAN (R 3.6.0) #> CRAN (R 3.6.0) #> CRAN (R 3.6.0) #> CRAN (R 3.6.0) #> CRAN (R 3.6.0) #> CRAN (R 3.6.0) #> CRAN (R 3.6.0) #> CRAN (R 3.6.0) #> CRAN (R 3.6.0) #> CRAN (R 3.6.0) #> CRAN (R 3.6.0) #> CRAN (R 3.6.0) #> CRAN (R 3.6.0) #> CRAN (R 3.6.0) #> CRAN (R 3.6.0) #> CRAN (R 3.6.0) #> CRAN (R 3.6.0) #> #> [1] /Library/Frameworks/R.framework/Versions/3.6/Resources/library ```
slowkow commented 4 years ago

The current ggrepel code assumes that hjust influences only the x position (left and right) and that vjust influences only the y position (up and down). This seems to be incorrect, and instead the code should account for the angle of rotation.

If this issue goes unfixed, then I guess ggrepel is (somewhat) limited to left-justified labels. This isn't exactly true, as you can tell from the examples in the vignette where we can successfully line up text labels on the left, right, and bottom edges. The issue becomes apparent when you try to plot multi-line labels -- that's when it's clear that ggrepel is plotting centered text rather than justified text.

I think this one might take some time to figure out. I might need to explore the grobX() and grobY() functions... any tips would be more than welcome.

slowkow commented 4 years ago

I think I might have fixed this issue.

Could I please ask if you might be able to test it out with your own examples and let me know if it is working as expected?

Thanks!

library(ggrepel)
#> Loading required package: ggplot2

rstring <- function() {
  paste(
    paste0(sample(letters, sample(10), replace = TRUE), collapse = ""),
    paste0(sample(letters, sample(10), replace = TRUE), collapse = ""),
    paste0(sample(letters, sample(10), replace = TRUE), collapse = ""),
    sep = "\n"
  )
}

set.seed(42)
d <- data.frame(x = runif(10), y = runif(10), label = replicate(10, rstring()))

Here is geom_text().

p <- ggplot(d, aes(x, y, label = label)) +
  geom_point()

p + geom_text(hjust = 0)

Here is geom_text_repel(). It no longer ignores hjust = 0.

p + geom_text_repel(
  hjust = 0, min.segment.length = 0
)

I modified ggrepel’s hjust code without breaking this example!

set.seed(42)
p <- ggplot(mtcars, aes(y = wt, x = 1, label = rownames(mtcars))) +
  geom_point(color = "red") +
  ylim(1, 5.5)

p +
  xlim(1, 1.375) +
  geom_text_repel(
    seed         = 42,
    force        = 0.5,
    force_pull   = 0,
    nudge_x      = 0.15,
    direction    = "y",
    hjust        = 0,
    segment.size = 0.2,
    box.padding  = 0.2
    #max.time     = 0.5,
    #max.iter = 1e4
  ) +
  ggtitle("hjust = 0")

ggplot(mtcars, aes(x = wt, y = 1, label = rownames(mtcars))) +
  geom_point(color = "red") +
  geom_text_repel(
    seed         = 42,
    force        = 0.5,
    force_pull   = 0, # don't pull labels toward data points
    nudge_y      = 0.05,
    direction    = "x",
    angle        = 90,
    hjust        = 0,
    segment.size = 0.2
  ) +
  xlim(1, 6) +
  ylim(1, 0.8) +
  theme(
    axis.line.y  = element_blank(),
    axis.ticks.y = element_blank(),
    axis.text.y  = element_blank(),
    axis.title.y = element_blank()
  )

Created on 2019-11-18 by the reprex package (v0.3.0)

Session info ``` r devtools::session_info() #> ─ Session info ────────────────────────────────────────────────────────── #> setting value #> version R version 3.6.1 (2019-07-05) #> os macOS Mojave 10.14.6 #> system x86_64, darwin15.6.0 #> ui X11 #> language (EN) #> collate en_US.UTF-8 #> ctype en_US.UTF-8 #> tz America/New_York #> date 2019-11-18 #> #> ─ Packages ────────────────────────────────────────────────────────────── #> package * version date lib #> assertthat 0.2.1 2019-03-21 [1] #> backports 1.1.4 2019-04-10 [1] #> callr 3.3.2 2019-09-22 [1] #> cli 1.1.0 2019-03-19 [1] #> colorspace 1.4-1 2019-03-18 [1] #> crayon 1.3.4 2017-09-16 [1] #> curl 4.1 2019-09-16 [1] #> desc 1.2.0 2018-05-01 [1] #> devtools 2.2.0 2019-09-07 [1] #> digest 0.6.21 2019-09-20 [1] #> dplyr 0.8.3 2019-07-04 [1] #> DT 0.9 2019-09-17 [1] #> ellipsis 0.3.0 2019-09-20 [1] #> evaluate 0.14 2019-05-28 [1] #> fs 1.3.1 2019-05-06 [1] #> ggplot2 * 3.2.1.9000 2019-09-27 [1] #> ggrepel * 0.8.0.9000 2019-11-15 [1] #> glue 1.3.1 2019-03-12 [1] #> gtable 0.3.0 2019-03-25 [1] #> highr 0.8 2019-03-20 [1] #> htmltools 0.3.6 2017-04-28 [1] #> htmlwidgets 1.3 2018-09-30 [1] #> httr 1.4.1 2019-08-05 [1] #> knitr 1.25 2019-09-18 [1] #> labeling 0.3 2014-08-23 [1] #> magrittr 1.5 2014-11-22 [1] #> memoise 1.1.0 2017-04-21 [1] #> mime 0.7 2019-06-11 [1] #> munsell 0.5.0 2018-06-12 [1] #> pillar 1.4.2 2019-06-29 [1] #> pkgbuild 1.0.5 2019-08-26 [1] #> pkgconfig 2.0.3 2019-09-22 [1] #> pkgload 1.0.2 2018-10-29 [1] #> prettyunits 1.0.2 2015-07-13 [1] #> processx 3.4.1 2019-07-18 [1] #> ps 1.3.0 2018-12-21 [1] #> purrr 0.3.2 2019-03-15 [1] #> R6 2.4.0 2019-02-14 [1] #> Rcpp 1.0.2 2019-07-25 [1] #> remotes 2.1.0 2019-06-24 [1] #> rlang 0.4.0 2019-06-25 [1] #> rmarkdown 1.15 2019-08-21 [1] #> rprojroot 1.3-2 2018-01-03 [1] #> scales 1.0.0 2018-08-09 [1] #> sessioninfo 1.1.1 2018-11-05 [1] #> stringi 1.4.3 2019-03-12 [1] #> stringr 1.4.0 2019-02-10 [1] #> testthat 2.2.1 2019-07-25 [1] #> tibble 2.1.3 2019-06-06 [1] #> tidyselect 0.2.5 2018-10-11 [1] #> usethis 1.5.1 2019-07-04 [1] #> withr 2.1.2 2018-03-15 [1] #> xfun 0.9 2019-08-21 [1] #> xml2 1.2.2 2019-08-09 [1] #> yaml 2.2.0 2018-07-25 [1] #> source #> CRAN (R 3.6.0) #> CRAN (R 3.6.0) #> CRAN (R 3.6.1) #> CRAN (R 3.6.0) #> CRAN (R 3.6.0) #> CRAN (R 3.6.0) #> CRAN (R 3.6.0) #> CRAN (R 3.6.0) #> CRAN (R 3.6.0) #> CRAN (R 3.6.0) #> CRAN (R 3.6.0) #> CRAN (R 3.6.0) #> CRAN (R 3.6.0) #> CRAN (R 3.6.0) #> CRAN (R 3.6.0) #> Github (tidyverse/ggplot2@23e3241) #> local #> CRAN (R 3.6.0) #> CRAN (R 3.6.0) #> CRAN (R 3.6.0) #> CRAN (R 3.6.0) #> CRAN (R 3.6.0) #> CRAN (R 3.6.0) #> CRAN (R 3.6.0) #> CRAN (R 3.6.0) #> CRAN (R 3.6.0) #> CRAN (R 3.6.0) #> CRAN (R 3.6.0) #> CRAN (R 3.6.0) #> CRAN (R 3.6.0) #> CRAN (R 3.6.0) #> CRAN (R 3.6.1) #> CRAN (R 3.6.0) #> CRAN (R 3.6.0) #> CRAN (R 3.6.0) #> CRAN (R 3.6.0) #> CRAN (R 3.6.0) #> CRAN (R 3.6.0) #> CRAN (R 3.6.0) #> CRAN (R 3.6.0) #> CRAN (R 3.6.0) #> CRAN (R 3.6.0) #> CRAN (R 3.6.0) #> CRAN (R 3.6.0) #> CRAN (R 3.6.0) #> CRAN (R 3.6.0) #> CRAN (R 3.6.0) #> CRAN (R 3.6.0) #> CRAN (R 3.6.0) #> CRAN (R 3.6.0) #> CRAN (R 3.6.0) #> CRAN (R 3.6.0) #> CRAN (R 3.6.0) #> CRAN (R 3.6.0) #> CRAN (R 3.6.0) #> #> [1] /Library/Frameworks/R.framework/Versions/3.6/Resources/library ```
phalteman commented 4 years ago

Hi there,

This now works as expected with geom_text_repel() for your examples and others I've asked about.

However, it doesn't yet work with geom_label_repel(). Any possibility of expanding this fix to that function as well? See example below:

library(ggplot2)
library(ggrepel)
p <- ggplot() +
  coord_cartesian(xlim=c(0,1), ylim=c(0,1)) +
  theme_void()
p

labelInfo <- data.frame(x=c(0.45,0.55), y=c(0.5,0.5), 
                        g=c("I'd like very much to be\nright justified","And I'd like to be\nleft justified"))

p + geom_text_repel(data=labelInfo, aes(x,y,label=g), hjust=c(1,0),
                     box.padding = 0.5, point.padding = 0.75, 
                     nudge_x = c(-0.05,0.05), nudge_y = 0, direction="x",
                     arrow=arrow(length=unit(2,"mm"), ends="last", type="closed"))

p1

p + geom_label_repel(data=labelInfo, aes(x,y,label=g), hjust=c(1,0),
                     box.padding = 0.5, point.padding = 0.75, 
                     nudge_x = c(-0.05,0.05), nudge_y = 0, direction="x",
                     arrow=arrow(length=unit(2,"mm"), ends="last", type="closed"))

p2

slowkow commented 4 years ago

Thanks for the comment.

I think we might be able to get geom_label() to respect hjust in mostly the same way as geom_text().

slowkow commented 4 years ago

@phalteman I think I might have fixed geom_label_repel(). Please try it out and re-open this issue if you run into any problems.

I added your example to the documentation: https://ggrepel.slowkow.com/articles/examples.html#justify-multiple-lines-of-text-with-hjust

phalteman commented 4 years ago

Works brilliantly, thanks! The only remaining issue, which is more closely related to issue #134, is that using "outward" and "inward" for hjust when justifying multiple labels produces the opposite behaviour (than I'd expect). This isn't a major issue, though, since c(1,0), for example, can accomplish the same thing.

Thanks for the quick fix!

slowkow commented 4 years ago

@phalteman Could I please ask you to open a new issue? Don't forget to include a reprex so we can help you with your specific issue.

phalteman commented 4 years ago

Sorry - it appears I was mistaken. While issue #134 identified that hjust and vjust in the range of [0,1] work in reverse fashion relative to ggplot2, "inward" and "outward" work the same. The issue is only that the behaviour is counter-intuitive to the name in my opinion. No new issue is needed. Thanks!

JosiahParry commented 4 years ago

It seems that this isn't entirely fixed or maybe have been broken again. Please see the below reprex comparing ggplot2 to ggrepel behavior. Far from a show stopper but thought I'd bring it back up!

Thanks for the wonderful package.

library(tidyverse)

p <- tibble::tribble(
  ~Year, ~Event, ~y_pos,
  1892, "Karl Marx\nHistorical Materialism", 1,
  1900, "Max Weber\nBeginnings of Conflict Theory", -1,
  1940, "The Frankfurt School\nCritical Theory", 1,
  1955, "C. Wright Mills\nModern Conflict Theory", -1,
  1987, "Kimberlé Crenshaw\nFirst Critical Race Theory Retreat\nLegitimates CRT.", 1
) %>% 
  ggplot(aes(Year, y_pos, label = Event)) +
  geom_segment(aes(yend = 0, y = y_pos, xend = Year)) +
  geom_point(size = 2) +
  geom_hline(yintercept = 0) 

p + geom_text(hjust = 0)

p + ggrepel::geom_text_repel(hjust = 0)

Created on 2020-09-14 by the reprex package (v0.3.0)

Session Info:

R version 3.6.3 (2020-02-29)
Platform: x86_64-apple-darwin15.6.0 (64-bit)
Running under: macOS Catalina 10.15.3

Matrix products: default
BLAS:   /System/Library/Frameworks/Accelerate.framework/Versions/A/Frameworks/vecLib.framework/Versions/A/libBLAS.dylib
LAPACK: /Library/Frameworks/R.framework/Versions/3.6/Resources/lib/libRlapack.dylib

locale:
[1] en_US.UTF-8/en_US.UTF-8/en_US.UTF-8/C/en_US.UTF-8/en_US.UTF-8

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

other attached packages:
[1] forcats_0.5.0   stringr_1.4.0   dplyr_1.0.0     purrr_0.3.4     readr_1.3.1    
[6] tidyr_1.1.0     tibble_3.0.1    ggplot2_3.3.0   tidyverse_1.3.0

loaded via a namespace (and not attached):
 [1] tidyselect_1.1.0 xfun_0.13        haven_2.2.0      lattice_0.20-41  colorspace_1.4-1
 [6] vctrs_0.3.1      generics_0.0.2   htmltools_0.4.0  yaml_2.2.1       rlang_0.4.7     
[11] pillar_1.4.4     withr_2.2.0      glue_1.4.1       DBI_1.1.0        dbplyr_1.4.2    
[16] modelr_0.1.6     readxl_1.3.1     lifecycle_0.2.0  munsell_0.5.0    blogdown_0.18   
[21] gtable_0.3.0     cellranger_1.1.0 rvest_0.3.5      evaluate_0.14    labeling_0.3    
[26] knitr_1.28       callr_3.4.3      ps_1.3.2         fansi_0.4.1      broom_0.5.5     
[31] Rcpp_1.0.5       clipr_0.7.0      backports_1.1.6  scales_1.1.0     jsonlite_1.7.0  
[36] farver_2.0.3     fs_1.4.2         hms_0.5.3        packrat_0.5.0    digest_0.6.25   
[41] stringi_1.4.6    processx_3.4.2   ggrepel_0.8.2    bookdown_0.18    grid_3.6.3      
[46] cli_2.0.2        tools_3.6.3      magrittr_1.5     whisker_0.4      crayon_1.3.4    
[51] pkgconfig_2.0.3  ellipsis_0.3.1   xml2_1.3.1       reprex_0.3.0     lubridate_1.7.9 
[56] assertthat_0.2.1 rmarkdown_2.1    httr_1.4.1       rstudioapi_0.11  R6_2.4.1        
[61] nlme_3.1-147     compiler_3.6.3