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

Left-align text but not label boxes #200

Open
z3tt opened this issue Jul 14, 2021 · 4 comments
Open

Left-align text but not label boxes #200

z3tt opened this issue Jul 14, 2021 · 4 comments

Comments

@z3tt
Copy link

z3tt commented Jul 14, 2021

Summary

When forcing geom_text|label_repel() to draw vertical lines, one needs to use hjust = 0.5. Unfortunately, this argument applies justification to both the text and the textbox.

Minimal code example

library(dplyr)
library(ggplot2)
library(ggrepel)
set.seed(42)

## with centered justification (and thus straight, vertical lines)
mpg %>% 
  group_by(manufacturer, model) %>% 
  summarize(cty = mean(cty, na.rm = TRUE)) %>% 
  ggplot(aes(x = cty, y = 1, 
             label = paste0(manufacturer, "\n", model))) +
  geom_point(alpha = .3) +
  geom_label_repel(
    fill = "grey90",
    nudge_y      = .03,
    direction    = "y",
    hjust        = .5,
    max.iter = 1e4, max.time = 1,
    size = 1, ## tiny for reprex
    lineheight = .9
  ) +
  xlim(5, 30) +
  ylim(1, 0.95) +
  theme_void()
#> `summarise()` has grouped output by 'manufacturer'. You can override using the `.groups` argument.
#> Warning: ggrepel: 13 unlabeled data points (too many overlaps). Consider
#> increasing max.overlaps

## with left-aligned text (and thus no straight lines)
mpg %>% 
  group_by(manufacturer, model) %>% 
  summarize(cty = mean(cty, na.rm = TRUE)) %>% 
  ggplot(aes(x = cty, y = 1, 
             label = paste0(manufacturer, "\n", model))) +
  geom_point(alpha = .3) +
  geom_label_repel(
    fill = "grey90",
    nudge_y      = .03,
    direction    = "y",
    hjust        = 0,
    max.iter = 1e4, max.time = 1,
    size = 1, ## tiny for reprex
    lineheight = .9
  ) +
  xlim(5, 30) +
  ylim(1, 0.95) +
  theme_void()
#> `summarise()` has grouped output by 'manufacturer'. You can override using the `.groups` argument.
#> Warning: ggrepel: 11 unlabeled data points (too many overlaps). Consider
#> increasing max.overlaps

Created on 2021-07-14 by the reprex package (v1.0.0)

Suggestions

It would be great to have two hjust arguments to control both individually, e.g. the traditional hjust or label.hjust to justify the text and another e.g. box.hjust to adjust the placement of the text or textbox.

Version information

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

Matrix products: default

locale:
[1] LC_COLLATE=German_Germany.1252  LC_CTYPE=German_Germany.1252   
[3] LC_MONETARY=German_Germany.1252 LC_NUMERIC=C                   
[5] LC_TIME=German_Germany.1252    
system code page: 65001

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

loaded via a namespace (and not attached):
 [1] Rcpp_1.0.6        jquerylib_0.1.3   bslib_0.2.4       pillar_1.6.0     
 [5] compiler_4.0.2    highr_0.8         tools_4.0.2       digest_0.6.27    
 [9] jsonlite_1.7.2    evaluate_0.14     lifecycle_1.0.0   tibble_3.1.0     
[13] gtable_0.3.0      pkgconfig_2.0.3   rlang_0.4.10      reprex_1.0.0     
[17] DBI_1.1.1         cli_2.3.1         rstudioapi_0.13   ggrepel_0.9.1    
[21] yaml_2.2.1        xfun_0.22         dplyr_1.0.5       knitr_1.31       
[25] sass_0.3.1        generics_0.1.0    vctrs_0.3.6       fs_1.5.0         
[29] grid_4.0.2        tidyselect_1.1.0  glue_1.4.2        R6_2.5.0         
[33] processx_3.4.5    fansi_0.4.2       rmarkdown_2.9     callr_3.5.1      
[37] ggplot2_3.3.3     purrr_0.3.4       clipr_0.7.1       magrittr_2.0.1   
[41] ps_1.5.0          htmltools_0.5.1.1 scales_1.1.1      ellipsis_0.3.1   
[45] assertthat_0.2.1  colorspace_2.0-0  utf8_1.2.1        munsell_0.5.0    
[49] crayon_1.4.1  
@z3tt
Copy link
Author

z3tt commented Jul 14, 2021

Maybe related to #188 and #190.

@slowkow
Copy link
Owner

slowkow commented Jul 14, 2021

Thanks for the reprex and suggestions!

This could be a bit tricky. It might be necessary to follow your suggestion and break the one option hjust into two options hjust.text and hjust.box (not sure what the best names might be).

@z3tt
Copy link
Author

z3tt commented Jul 14, 2021

Sure, thanks for all your work on this great package!

I am also not sure about the names. hjust usually applies to text so maybe stick to it. However, it's just a box when using geom_label_repel so not sure how intuitive either hjust.box is. Maybe hjust or hjust.text and hjust.segment since it influences the segments? Difficult, all potentially confusing.

@aphalo
Copy link
Contributor

aphalo commented Nov 12, 2021

In general justification in 'ggplot2' does not shift the position of the point to which a label is referenced, it shifts the position of the label with respect to the point. I think this enhancement would make behaviour consistent with 'ggplot2' but should be optional as it could break user code... (possibly default only for direction = "x" and direction = "y").
This could be fixed, I think, by updating select_line_connection() to support justification.

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

3 participants