Thanks to visit codestin.com
Credit goes to Github.com

Skip to content

Left-align text but not label boxes #200

@z3tt

Description

@z3tt

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  

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